2019-06-19 14:41:27 -07:00
|
|
|
"""Test Met weather entity."""
|
2021-04-25 12:27:40 +03:00
|
|
|
from homeassistant import config_entries
|
2020-09-10 07:58:40 +02:00
|
|
|
from homeassistant.components.met import DOMAIN
|
|
|
|
from homeassistant.components.weather import DOMAIN as WEATHER_DOMAIN
|
2023-02-15 10:31:43 +01:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-03-09 14:32:08 +01:00
|
|
|
from homeassistant.helpers import entity_registry as er
|
2020-09-10 07:58:40 +02:00
|
|
|
|
2019-06-19 14:41:27 -07:00
|
|
|
|
2023-08-17 19:41:11 +02:00
|
|
|
async def test_new_config_entry(hass: HomeAssistant, mock_weather) -> None:
|
|
|
|
"""Test the expected entities are created."""
|
|
|
|
registry = er.async_get(hass)
|
2019-07-31 12:25:30 -07:00
|
|
|
await hass.config_entries.flow.async_init("met", context={"source": "onboarding"})
|
2019-06-19 14:41:27 -07:00
|
|
|
await hass.async_block_till_done()
|
2020-09-10 07:58:40 +02:00
|
|
|
assert len(hass.states.async_entity_ids("weather")) == 1
|
2019-06-19 14:41:27 -07:00
|
|
|
|
2023-08-17 19:41:11 +02:00
|
|
|
entry = hass.config_entries.async_entries()[0]
|
|
|
|
assert len(er.async_entries_for_config_entry(registry, entry.entry_id)) == 1
|
|
|
|
|
|
|
|
|
|
|
|
async def test_legacy_config_entry(hass: HomeAssistant, mock_weather) -> None:
|
|
|
|
"""Test the expected entities are created."""
|
2021-03-09 14:32:08 +01:00
|
|
|
registry = er.async_get(hass)
|
2023-08-17 19:41:11 +02:00
|
|
|
registry.async_get_or_create(
|
|
|
|
WEATHER_DOMAIN,
|
|
|
|
DOMAIN,
|
|
|
|
"home-hourly",
|
|
|
|
)
|
|
|
|
await hass.config_entries.flow.async_init("met", context={"source": "onboarding"})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(hass.states.async_entity_ids("weather")) == 2
|
|
|
|
|
|
|
|
entry = hass.config_entries.async_entries()[0]
|
|
|
|
assert len(er.async_entries_for_config_entry(registry, entry.entry_id)) == 2
|
2020-09-10 07:58:40 +02:00
|
|
|
|
|
|
|
|
2023-08-17 19:41:11 +02:00
|
|
|
async def test_tracking_home(hass: HomeAssistant, mock_weather) -> None:
|
|
|
|
"""Test we track home."""
|
|
|
|
await hass.config_entries.flow.async_init("met", context={"source": "onboarding"})
|
|
|
|
await hass.async_block_till_done()
|
|
|
|
assert len(hass.states.async_entity_ids("weather")) == 1
|
|
|
|
assert len(mock_weather.mock_calls) == 4
|
2020-09-10 07:58:40 +02:00
|
|
|
|
2019-06-19 14:41:27 -07:00
|
|
|
# Test we track config
|
2019-07-31 12:25:30 -07:00
|
|
|
await hass.config.async_update(latitude=10, longitude=20)
|
2019-06-19 14:41:27 -07:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-08-11 03:22:39 +02:00
|
|
|
assert len(mock_weather.mock_calls) == 8
|
2019-06-19 14:41:27 -07:00
|
|
|
|
2021-04-07 09:27:58 +02: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 14:41:27 -07:00
|
|
|
entry = hass.config_entries.async_entries()[0]
|
|
|
|
await hass.config_entries.async_remove(entry.entry_id)
|
2021-02-08 10:45:46 +01:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert len(hass.states.async_entity_ids("weather")) == 0
|
2019-06-19 14:41:27 -07:00
|
|
|
|
|
|
|
|
2023-02-15 10:31:43 +01:00
|
|
|
async def test_not_tracking_home(hass: HomeAssistant, mock_weather) -> None:
|
2019-06-19 14:41:27 -07:00
|
|
|
"""Test when we not track home."""
|
2020-09-10 07:58:40 +02:00
|
|
|
|
2019-07-31 12:25:30 -07:00
|
|
|
await hass.config_entries.flow.async_init(
|
|
|
|
"met",
|
2021-04-25 12:27:40 +03:00
|
|
|
context={"source": config_entries.SOURCE_USER},
|
2019-07-31 12:25:30 -07:00
|
|
|
data={"name": "Somewhere", "latitude": 10, "longitude": 20, "elevation": 0},
|
|
|
|
)
|
2019-06-19 14:41:27 -07:00
|
|
|
await hass.async_block_till_done()
|
2023-08-17 19:41:11 +02:00
|
|
|
assert len(hass.states.async_entity_ids("weather")) == 1
|
2020-08-11 03:22:39 +02:00
|
|
|
assert len(mock_weather.mock_calls) == 4
|
2019-06-19 14:41:27 -07:00
|
|
|
|
|
|
|
# Test we do not track config
|
2019-07-31 12:25:30 -07:00
|
|
|
await hass.config.async_update(latitude=10, longitude=20)
|
2019-06-19 14:41:27 -07:00
|
|
|
await hass.async_block_till_done()
|
|
|
|
|
2020-08-11 03:22:39 +02:00
|
|
|
assert len(mock_weather.mock_calls) == 4
|
2019-06-19 14:41:27 -07:00
|
|
|
|
|
|
|
entry = hass.config_entries.async_entries()[0]
|
|
|
|
await hass.config_entries.async_remove(entry.entry_id)
|
2021-02-08 10:45:46 +01:00
|
|
|
await hass.async_block_till_done()
|
2019-07-31 12:25:30 -07:00
|
|
|
assert len(hass.states.async_entity_ids("weather")) == 0
|