2019-06-19 21:41:27 +00:00
|
|
|
"""Test Met weather entity."""
|
|
|
|
|
2021-04-25 09:27:40 +00:00
|
|
|
from homeassistant import config_entries
|
2020-09-10 05:58:40 +00:00
|
|
|
from homeassistant.components.met import DOMAIN
|
|
|
|
from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN
|
2021-03-09 13:32:08 +00:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2020-09-10 05:58:40 +00:00
|
|
|
|
2019-06-19 21:41:27 +00:00
|
|
|
|
|
|
|
async def test_tracking_home(hass, mock_weather):
|
|
|
|
"""Test we track home."""
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.config_entries.flow.async_init("met", context={"source": "onboarding"})
|
2019-06-19 21:41:27 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-09-10 05:58:40 +00:00
|
|
|
assert len(hass.states.async_entity_ids("weather")) == 1
|
2020-08-11 01:22:39 +00:00
|
|
|
assert len(mock_weather.mock_calls) == 4
|
2019-06-19 21:41:27 +00:00
|
|
|
|
2020-09-10 05:58:40 +00:00
|
|
|
# Test the hourly sensor is disabled by default
|
2021-03-09 13:32:08 +00:00
|
|
|
registry = er.async_get(hass)
|
2020-09-10 05:58:40 +00:00
|
|
|
|
2022-07-10 20:13:22 +00:00
|
|
|
state = hass.states.get("weather.forecast_test_home_hourly")
|
2020-09-10 05:58:40 +00:00
|
|
|
assert state is None
|
|
|
|
|
2022-07-10 20:13:22 +00:00
|
|
|
entry = registry.async_get("weather.forecast_test_home_hourly")
|
2020-09-10 05:58:40 +00:00
|
|
|
assert entry
|
|
|
|
assert entry.disabled
|
2021-12-15 21:25:40 +00:00
|
|
|
assert entry.disabled_by is er.RegistryEntryDisabler.INTEGRATION
|
2020-09-10 05:58:40 +00:00
|
|
|
|
2019-06-19 21:41:27 +00:00
|
|
|
# Test we track config
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.config.async_update(latitude=10, longitude=20)
|
2019-06-19 21:41:27 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-08-11 01:22:39 +00:00
|
|
|
assert len(mock_weather.mock_calls) == 8
|
2019-06-19 21:41:27 +00:00
|
|
|
|
2021-04-07 07:27:58 +00:00
|
|
|
# Same coordinates again should not trigger any new requests to met.no
|
|
|
|
await hass.config.async_update(latitude=10, longitude=20)
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(mock_weather.mock_calls) == 8
|
|
|
|
|
2019-06-19 21:41:27 +00:00
|
|
|
entry = hass.config_entries.async_entries()[0]
|
|
|
|
await hass.config_entries.async_remove(entry.entry_id)
|
2021-02-08 09:45:46 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert len(hass.states.async_entity_ids("weather")) == 0
|
2019-06-19 21:41:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def test_not_tracking_home(hass, mock_weather):
|
|
|
|
"""Test when we not track home."""
|
2020-09-10 05:58:40 +00:00
|
|
|
|
|
|
|
# Pre-create registry entry for disabled by default hourly weather
|
2021-03-09 13:32:08 +00:00
|
|
|
registry = er.async_get(hass)
|
2020-09-10 05:58:40 +00:00
|
|
|
registry.async_get_or_create(
|
|
|
|
WEATHER_DOMAIN,
|
|
|
|
DOMAIN,
|
|
|
|
"10-20-hourly",
|
2022-07-10 20:13:22 +00:00
|
|
|
suggested_object_id="forecast_somewhere_hourly",
|
2020-09-10 05:58:40 +00:00
|
|
|
disabled_by=None,
|
|
|
|
)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.config_entries.flow.async_init(
|
|
|
|
"met",
|
2021-04-25 09:27:40 +00:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2019-07-31 19:25:30 +00:00
|
|
|
data={"name": "Somewhere", "latitude": 10, "longitude": 20, "elevation": 0},
|
|
|
|
)
|
2019-06-19 21:41:27 +00:00
|
|
|
await hass.async_block_till_done()
|
2020-08-11 01:22:39 +00:00
|
|
|
assert len(hass.states.async_entity_ids("weather")) == 2
|
|
|
|
assert len(mock_weather.mock_calls) == 4
|
2019-06-19 21:41:27 +00:00
|
|
|
|
|
|
|
# Test we do not track config
|
2019-07-31 19:25:30 +00:00
|
|
|
await hass.config.async_update(latitude=10, longitude=20)
|
2019-06-19 21:41:27 +00:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-08-11 01:22:39 +00:00
|
|
|
assert len(mock_weather.mock_calls) == 4
|
2019-06-19 21:41:27 +00:00
|
|
|
|
|
|
|
entry = hass.config_entries.async_entries()[0]
|
|
|
|
await hass.config_entries.async_remove(entry.entry_id)
|
2021-02-08 09:45:46 +00:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 19:25:30 +00:00
|
|
|
assert len(hass.states.async_entity_ids("weather")) == 0
|