Ejemplo n.º 1
0
async def test_site_cannot_connect(hass, requests_mock, legacy_patchable_time):
    """Test we handle cannot connect error."""

    requests_mock.get("/public/data/val/wxfcs/all/json/sitelist/", text="")
    requests_mock.get("/public/data/val/wxfcs/all/json/354107?res=3hourly",
                      text="")
    requests_mock.get("/public/data/val/wxfcs/all/json/354107?res=daily",
                      text="")

    entry = MockConfigEntry(
        domain=DOMAIN,
        data=METOFFICE_CONFIG_WAVERTREE,
    )
    entry.add_to_hass(hass)
    await hass.config_entries.async_setup(entry.entry_id)
    await hass.async_block_till_done()

    dev_reg = get_dev_reg(hass)
    assert len(dev_reg.devices) == 0

    assert hass.states.get("weather.met_office_wavertree_3hourly") is None
    assert hass.states.get("weather.met_office_wavertree_daily") is None
    for sensor_id in WAVERTREE_SENSOR_RESULTS:
        sensor_name, _ = WAVERTREE_SENSOR_RESULTS[sensor_id]
        sensor = hass.states.get(f"sensor.wavertree_{sensor_name}")
        assert sensor is None
Ejemplo n.º 2
0
async def test_webostv_turn_on_trigger_device_id(hass, calls, client):
    """Test for turn_on triggers by device_id firing."""
    await setup_webostv(hass)

    device_reg = get_dev_reg(hass)
    device = device_reg.async_get_device(identifiers={(DOMAIN, FAKE_UUID)})

    assert await async_setup_component(
        hass,
        automation.DOMAIN,
        {
            automation.DOMAIN: [
                {
                    "trigger": {
                        "platform": "webostv.turn_on",
                        "device_id": device.id,
                    },
                    "action": {
                        "service": "test.automation",
                        "data_template": {
                            "some": device.id,
                            "id": "{{ trigger.id }}",
                        },
                    },
                },
            ],
        },
    )

    await hass.services.async_call(
        "media_player",
        "turn_on",
        {"entity_id": ENTITY_ID},
        blocking=True,
    )

    await hass.async_block_till_done()
    assert len(calls) == 1
    assert calls[0].data["some"] == device.id
    assert calls[0].data["id"] == 0

    with patch("homeassistant.config.load_yaml", return_value={}):
        await hass.services.async_call(automation.DOMAIN,
                                       SERVICE_RELOAD,
                                       blocking=True)

    calls.clear()

    with pytest.raises(HomeAssistantError):
        await hass.services.async_call(
            "media_player",
            "turn_on",
            {"entity_id": ENTITY_ID},
            blocking=True,
        )

    await hass.async_block_till_done()
    assert len(calls) == 0
async def test_if_fires_on_turn_on_request(hass, calls, client):
    """Test for turn_on and turn_off triggers firing."""
    await setup_webostv(hass)

    device_reg = get_dev_reg(hass)
    device = device_reg.async_get_device(identifiers={(DOMAIN, FAKE_UUID)})

    assert await async_setup_component(
        hass,
        automation.DOMAIN,
        {
            automation.DOMAIN: [
                {
                    "trigger": {
                        "platform": "device",
                        "domain": DOMAIN,
                        "device_id": device.id,
                        "type": "webostv.turn_on",
                    },
                    "action": {
                        "service": "test.automation",
                        "data_template": {
                            "some": "{{ trigger.device_id }}",
                            "id": "{{ trigger.id }}",
                        },
                    },
                },
                {
                    "trigger": {
                        "platform": "webostv.turn_on",
                        "entity_id": ENTITY_ID,
                    },
                    "action": {
                        "service": "test.automation",
                        "data_template": {
                            "some": ENTITY_ID,
                            "id": "{{ trigger.id }}",
                        },
                    },
                },
            ],
        },
    )

    await hass.services.async_call(
        "media_player",
        "turn_on",
        {"entity_id": ENTITY_ID},
        blocking=True,
    )

    await hass.async_block_till_done()
    assert len(calls) == 2
    assert calls[0].data["some"] == device.id
    assert calls[0].data["id"] == 0
    assert calls[1].data["some"] == ENTITY_ID
    assert calls[1].data["id"] == 0
Ejemplo n.º 4
0
async def async_remove_devices(hass, entity, entry_id) -> None:
    """Get item that is removed from session."""
    dev_registry = get_dev_reg(hass)
    device = dev_registry.async_get_device(identifiers={(DOMAIN,
                                                         entity.device_id)},
                                           connections=set())
    if device is not None:
        dev_registry.async_update_device(device.id,
                                         remove_config_entry_id=entry_id)
async def test_failure_scenarios(hass, client):
    """Test failure scenarios."""
    await setup_webostv(hass)

    # Test wrong trigger platform type
    with pytest.raises(HomeAssistantError):
        await device_trigger.async_attach_trigger(
            hass, {
                "type": "wrong.type",
                "device_id": "invalid_device_id"
            }, None, {})

    # Test invalid device id
    with pytest.raises(InvalidDeviceAutomationConfig):
        await device_trigger.async_validate_trigger_config(
            hass,
            {
                "platform": "device",
                "domain": DOMAIN,
                "type": "webostv.turn_on",
                "device_id": "invalid_device_id",
            },
        )

    entry = MockConfigEntry(domain="fake",
                            state=ConfigEntryState.LOADED,
                            data={})
    entry.add_to_hass(hass)
    device_reg = get_dev_reg(hass)

    device = device_reg.async_get_or_create(config_entry_id=entry.entry_id,
                                            identifiers={("fake", "fake")})

    config = {
        "platform": "device",
        "domain": DOMAIN,
        "device_id": device.id,
        "type": "webostv.turn_on",
    }

    # Test that device id from non webostv domain raises exception
    with pytest.raises(InvalidDeviceAutomationConfig):
        await device_trigger.async_validate_trigger_config(hass, config)

    # Test no exception if device is not loaded
    await hass.config_entries.async_unload(entry.entry_id)
    assert await device_trigger.async_validate_trigger_config(hass,
                                                              config) == config
Ejemplo n.º 6
0
async def test_get_triggers(hass, client):
    """Test we get the expected triggers."""
    await setup_webostv(hass, "fake-uuid")

    device_reg = get_dev_reg(hass)
    device = device_reg.async_get_device(identifiers={(DOMAIN, "fake-uuid")})

    turn_on_trigger = {
        "platform": "device",
        "domain": DOMAIN,
        "type": "webostv.turn_on",
        "device_id": device.id,
    }

    triggers = await async_get_device_automations(hass, "trigger", device.id)
    assert turn_on_trigger in triggers
Ejemplo n.º 7
0
async def test_one_sensor_site_running(hass, requests_mock,
                                       legacy_patchable_time):
    """Test the Met Office sensor platform."""
    # all metoffice test data encapsulated in here
    mock_json = json.loads(load_fixture("metoffice.json"))
    all_sites = json.dumps(mock_json["all_sites"])
    wavertree_hourly = json.dumps(mock_json["wavertree_hourly"])
    wavertree_daily = json.dumps(mock_json["wavertree_daily"])

    requests_mock.get("/public/data/val/wxfcs/all/json/sitelist/",
                      text=all_sites)
    requests_mock.get(
        "/public/data/val/wxfcs/all/json/354107?res=3hourly",
        text=wavertree_hourly,
    )
    requests_mock.get(
        "/public/data/val/wxfcs/all/json/354107?res=daily",
        text=wavertree_daily,
    )

    entry = MockConfigEntry(
        domain=DOMAIN,
        data=METOFFICE_CONFIG_WAVERTREE,
    )
    entry.add_to_hass(hass)
    await hass.config_entries.async_setup(entry.entry_id)
    await hass.async_block_till_done()

    dev_reg = get_dev_reg(hass)
    assert len(dev_reg.devices) == 1
    device_wavertree = dev_reg.async_get_device(
        identifiers=DEVICE_KEY_WAVERTREE)
    assert device_wavertree.name == "Met Office Wavertree"

    running_sensor_ids = hass.states.async_entity_ids("sensor")
    assert len(running_sensor_ids) > 0
    for running_id in running_sensor_ids:
        sensor = hass.states.get(running_id)
        sensor_id = sensor.attributes.get("sensor_id")
        _, sensor_value = WAVERTREE_SENSOR_RESULTS[sensor_id]

        assert sensor.state == sensor_value
        assert sensor.attributes.get(
            "last_update").isoformat() == TEST_DATETIME_STRING
        assert sensor.attributes.get("site_id") == "354107"
        assert sensor.attributes.get("site_name") == TEST_SITE_NAME_WAVERTREE
        assert sensor.attributes.get("attribution") == ATTRIBUTION
Ejemplo n.º 8
0
async def test_get_triggers(hass, client):
    """Test we get the expected triggers."""
    await setup_webostv(hass)

    device_reg = get_dev_reg(hass)
    device = device_reg.async_get_device(identifiers={(DOMAIN, FAKE_UUID)})

    turn_on_trigger = {
        "platform": "device",
        "domain": DOMAIN,
        "type": "webostv.turn_on",
        "device_id": device.id,
        "metadata": {},
    }

    triggers = await async_get_device_automations(hass,
                                                  DeviceAutomationType.TRIGGER,
                                                  device.id)
    assert turn_on_trigger in triggers
Ejemplo n.º 9
0
async def test_two_weather_sites_running(hass, requests_mock,
                                         legacy_patchable_time):
    """Test we handle two different weather sites both running."""

    # all metoffice test data encapsulated in here
    mock_json = json.loads(load_fixture("metoffice.json"))
    all_sites = json.dumps(mock_json["all_sites"])
    wavertree_hourly = json.dumps(mock_json["wavertree_hourly"])
    wavertree_daily = json.dumps(mock_json["wavertree_daily"])
    kingslynn_hourly = json.dumps(mock_json["kingslynn_hourly"])
    kingslynn_daily = json.dumps(mock_json["kingslynn_daily"])

    requests_mock.get("/public/data/val/wxfcs/all/json/sitelist/",
                      text=all_sites)
    requests_mock.get("/public/data/val/wxfcs/all/json/354107?res=3hourly",
                      text=wavertree_hourly)
    requests_mock.get("/public/data/val/wxfcs/all/json/354107?res=daily",
                      text=wavertree_daily)
    requests_mock.get("/public/data/val/wxfcs/all/json/322380?res=3hourly",
                      text=kingslynn_hourly)
    requests_mock.get("/public/data/val/wxfcs/all/json/322380?res=daily",
                      text=kingslynn_daily)

    entry = MockConfigEntry(
        domain=DOMAIN,
        data=METOFFICE_CONFIG_WAVERTREE,
    )
    entry.add_to_hass(hass)
    await hass.config_entries.async_setup(entry.entry_id)
    entry2 = MockConfigEntry(
        domain=DOMAIN,
        data=METOFFICE_CONFIG_KINGSLYNN,
    )
    entry2.add_to_hass(hass)
    await hass.config_entries.async_setup(entry2.entry_id)
    await hass.async_block_till_done()

    dev_reg = get_dev_reg(hass)
    assert len(dev_reg.devices) == 2
    device_kingslynn = dev_reg.async_get_device(
        identifiers=DEVICE_KEY_KINGSLYNN)
    assert device_kingslynn.name == "Met Office King's Lynn"
    device_wavertree = dev_reg.async_get_device(
        identifiers=DEVICE_KEY_WAVERTREE)
    assert device_wavertree.name == "Met Office Wavertree"

    # Wavertree 3-hourly weather platform expected results
    weather = hass.states.get("weather.met_office_wavertree_3_hourly")
    assert weather

    assert weather.state == "sunny"
    assert weather.attributes.get("temperature") == 17
    assert weather.attributes.get("wind_speed") == 9
    assert weather.attributes.get("wind_bearing") == "SSE"
    assert weather.attributes.get("visibility") == "Good - 10-20"
    assert weather.attributes.get("humidity") == 50

    # Forecasts added - just pick out 1 entry to check
    assert len(weather.attributes.get("forecast")) == 35

    assert (weather.attributes.get("forecast")[18]["datetime"] ==
            "2020-04-27T21:00:00+00:00")
    assert weather.attributes.get("forecast")[18]["condition"] == "clear-night"
    assert weather.attributes.get(
        "forecast")[18]["precipitation_probability"] == 1
    assert weather.attributes.get("forecast")[18]["temperature"] == 9
    assert weather.attributes.get("forecast")[18]["wind_speed"] == 4
    assert weather.attributes.get("forecast")[18]["wind_bearing"] == "NW"

    # Wavertree daily weather platform expected results
    weather = hass.states.get("weather.met_office_wavertree_daily")
    assert weather

    assert weather.state == "sunny"
    assert weather.attributes.get("temperature") == 19
    assert weather.attributes.get("wind_speed") == 9
    assert weather.attributes.get("wind_bearing") == "SSE"
    assert weather.attributes.get("visibility") == "Good - 10-20"
    assert weather.attributes.get("humidity") == 50

    # Also has Forecasts added - again, just pick out 1 entry to check
    assert len(weather.attributes.get("forecast")) == 8

    assert (weather.attributes.get("forecast")[7]["datetime"] ==
            "2020-04-29T12:00:00+00:00")
    assert weather.attributes.get("forecast")[7]["condition"] == "rainy"
    assert weather.attributes.get(
        "forecast")[7]["precipitation_probability"] == 59
    assert weather.attributes.get("forecast")[7]["temperature"] == 13
    assert weather.attributes.get("forecast")[7]["wind_speed"] == 13
    assert weather.attributes.get("forecast")[7]["wind_bearing"] == "SE"

    # King's Lynn 3-hourly weather platform expected results
    weather = hass.states.get("weather.met_office_king_s_lynn_3_hourly")
    assert weather

    assert weather.state == "sunny"
    assert weather.attributes.get("temperature") == 14
    assert weather.attributes.get("wind_speed") == 2
    assert weather.attributes.get("wind_bearing") == "E"
    assert weather.attributes.get("visibility") == "Very Good - 20-40"
    assert weather.attributes.get("humidity") == 60

    # Also has Forecast added - just pick out 1 entry to check
    assert len(weather.attributes.get("forecast")) == 35

    assert (weather.attributes.get("forecast")[18]["datetime"] ==
            "2020-04-27T21:00:00+00:00")
    assert weather.attributes.get("forecast")[18]["condition"] == "cloudy"
    assert weather.attributes.get(
        "forecast")[18]["precipitation_probability"] == 9
    assert weather.attributes.get("forecast")[18]["temperature"] == 10
    assert weather.attributes.get("forecast")[18]["wind_speed"] == 7
    assert weather.attributes.get("forecast")[18]["wind_bearing"] == "SE"

    # King's Lynn daily weather platform expected results
    weather = hass.states.get("weather.met_office_king_s_lynn_daily")
    assert weather

    assert weather.state == "cloudy"
    assert weather.attributes.get("temperature") == 9
    assert weather.attributes.get("wind_speed") == 4
    assert weather.attributes.get("wind_bearing") == "ESE"
    assert weather.attributes.get("visibility") == "Very Good - 20-40"
    assert weather.attributes.get("humidity") == 75

    # All should have Forecast added - again, just picking out 1 entry to check
    assert len(weather.attributes.get("forecast")) == 8

    assert (weather.attributes.get("forecast")[5]["datetime"] ==
            "2020-04-28T12:00:00+00:00")
    assert weather.attributes.get("forecast")[5]["condition"] == "cloudy"
    assert weather.attributes.get(
        "forecast")[5]["precipitation_probability"] == 14
    assert weather.attributes.get("forecast")[5]["temperature"] == 11
    assert weather.attributes.get("forecast")[5]["wind_speed"] == 7
    assert weather.attributes.get("forecast")[5]["wind_bearing"] == "ESE"
Ejemplo n.º 10
0
async def test_one_weather_site_running(hass, requests_mock,
                                        legacy_patchable_time):
    """Test the Met Office weather platform."""

    # all metoffice test data encapsulated in here
    mock_json = json.loads(load_fixture("metoffice.json"))
    all_sites = json.dumps(mock_json["all_sites"])
    wavertree_hourly = json.dumps(mock_json["wavertree_hourly"])
    wavertree_daily = json.dumps(mock_json["wavertree_daily"])

    requests_mock.get("/public/data/val/wxfcs/all/json/sitelist/",
                      text=all_sites)
    requests_mock.get(
        "/public/data/val/wxfcs/all/json/354107?res=3hourly",
        text=wavertree_hourly,
    )
    requests_mock.get(
        "/public/data/val/wxfcs/all/json/354107?res=daily",
        text=wavertree_daily,
    )

    entry = MockConfigEntry(
        domain=DOMAIN,
        data=METOFFICE_CONFIG_WAVERTREE,
    )
    entry.add_to_hass(hass)
    await hass.config_entries.async_setup(entry.entry_id)
    await hass.async_block_till_done()

    dev_reg = get_dev_reg(hass)
    assert len(dev_reg.devices) == 1
    device_wavertree = dev_reg.async_get_device(
        identifiers=DEVICE_KEY_WAVERTREE)
    assert device_wavertree.name == "Met Office Wavertree"

    # Wavertree 3-hourly weather platform expected results
    weather = hass.states.get("weather.met_office_wavertree_3_hourly")
    assert weather

    assert weather.state == "sunny"
    assert weather.attributes.get("temperature") == 17
    assert weather.attributes.get("wind_speed") == 9
    assert weather.attributes.get("wind_bearing") == "SSE"
    assert weather.attributes.get("visibility") == "Good - 10-20"
    assert weather.attributes.get("humidity") == 50

    # Forecasts added - just pick out 1 entry to check
    assert len(weather.attributes.get("forecast")) == 35

    assert (weather.attributes.get("forecast")[26]["datetime"] ==
            "2020-04-28T21:00:00+00:00")
    assert weather.attributes.get("forecast")[26]["condition"] == "cloudy"
    assert weather.attributes.get(
        "forecast")[26]["precipitation_probability"] == 9
    assert weather.attributes.get("forecast")[26]["temperature"] == 10
    assert weather.attributes.get("forecast")[26]["wind_speed"] == 4
    assert weather.attributes.get("forecast")[26]["wind_bearing"] == "NNE"

    # Wavertree daily weather platform expected results
    weather = hass.states.get("weather.met_office_wavertree_daily")
    assert weather

    assert weather.state == "sunny"
    assert weather.attributes.get("temperature") == 19
    assert weather.attributes.get("wind_speed") == 9
    assert weather.attributes.get("wind_bearing") == "SSE"
    assert weather.attributes.get("visibility") == "Good - 10-20"
    assert weather.attributes.get("humidity") == 50

    # Also has Forecasts added - again, just pick out 1 entry to check
    assert len(weather.attributes.get("forecast")) == 8

    assert (weather.attributes.get("forecast")[7]["datetime"] ==
            "2020-04-29T12:00:00+00:00")
    assert weather.attributes.get("forecast")[7]["condition"] == "rainy"
    assert weather.attributes.get(
        "forecast")[7]["precipitation_probability"] == 59
    assert weather.attributes.get("forecast")[7]["temperature"] == 13
    assert weather.attributes.get("forecast")[7]["wind_speed"] == 13
    assert weather.attributes.get("forecast")[7]["wind_bearing"] == "SE"
Ejemplo n.º 11
0
async def test_two_sensor_sites_running(hass, requests_mock):
    """Test we handle two sets of sensors running for two different sites."""

    # all metoffice test data encapsulated in here
    mock_json = json.loads(load_fixture("metoffice.json"))
    all_sites = json.dumps(mock_json["all_sites"])
    wavertree_hourly = json.dumps(mock_json["wavertree_hourly"])
    wavertree_daily = json.dumps(mock_json["wavertree_daily"])
    kingslynn_hourly = json.dumps(mock_json["kingslynn_hourly"])
    kingslynn_daily = json.dumps(mock_json["kingslynn_daily"])

    requests_mock.get("/public/data/val/wxfcs/all/json/sitelist/", text=all_sites)
    requests_mock.get(
        "/public/data/val/wxfcs/all/json/354107?res=3hourly", text=wavertree_hourly
    )
    requests_mock.get(
        "/public/data/val/wxfcs/all/json/354107?res=daily", text=wavertree_daily
    )
    requests_mock.get(
        "/public/data/val/wxfcs/all/json/322380?res=3hourly", text=kingslynn_hourly
    )
    requests_mock.get(
        "/public/data/val/wxfcs/all/json/322380?res=daily", text=kingslynn_daily
    )

    entry = MockConfigEntry(
        domain=DOMAIN,
        data=METOFFICE_CONFIG_WAVERTREE,
    )
    entry.add_to_hass(hass)
    await hass.config_entries.async_setup(entry.entry_id)
    entry2 = MockConfigEntry(
        domain=DOMAIN,
        data=METOFFICE_CONFIG_KINGSLYNN,
    )
    entry2.add_to_hass(hass)
    await hass.config_entries.async_setup(entry2.entry_id)
    await hass.async_block_till_done()

    dev_reg = get_dev_reg(hass)
    assert len(dev_reg.devices) == 2
    device_kingslynn = dev_reg.async_get_device(identifiers=DEVICE_KEY_KINGSLYNN)
    assert device_kingslynn.name == "Met Office King's Lynn"
    device_wavertree = dev_reg.async_get_device(identifiers=DEVICE_KEY_WAVERTREE)
    assert device_wavertree.name == "Met Office Wavertree"

    running_sensor_ids = hass.states.async_entity_ids("sensor")
    assert len(running_sensor_ids) > 0
    for running_id in running_sensor_ids:
        sensor = hass.states.get(running_id)
        sensor_id = sensor.attributes.get("sensor_id")
        if sensor.attributes.get("site_id") == "354107":
            _, sensor_value = WAVERTREE_SENSOR_RESULTS[sensor_id]
            assert sensor.state == sensor_value
            assert (
                sensor.attributes.get("last_update").isoformat() == TEST_DATETIME_STRING
            )
            assert sensor.attributes.get("sensor_id") == sensor_id
            assert sensor.attributes.get("site_id") == "354107"
            assert sensor.attributes.get("site_name") == TEST_SITE_NAME_WAVERTREE
            assert sensor.attributes.get("attribution") == ATTRIBUTION

        else:
            _, sensor_value = KINGSLYNN_SENSOR_RESULTS[sensor_id]
            assert sensor.state == sensor_value
            assert (
                sensor.attributes.get("last_update").isoformat() == TEST_DATETIME_STRING
            )
            assert sensor.attributes.get("sensor_id") == sensor_id
            assert sensor.attributes.get("site_id") == "322380"
            assert sensor.attributes.get("site_name") == TEST_SITE_NAME_KINGSLYNN
            assert sensor.attributes.get("attribution") == ATTRIBUTION
Ejemplo n.º 12
0
async def async_get_device_id(hass, device_id):
    """Get device id from device registry."""
    dev_registry = get_dev_reg(hass)
    device = dev_registry.async_get_device(identifiers={(DOMAIN, device_id)},
                                           connections=set())
    return device.id if device is not None else None