mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Use freezegun in AEMET tests (#99253)
This commit is contained in:
parent
5006244f4c
commit
2e0a22fdaf
@ -2,6 +2,7 @@
|
|||||||
from unittest.mock import AsyncMock, MagicMock, patch
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
from aemet_opendata.exceptions import AuthError
|
from aemet_opendata.exceptions import AuthError
|
||||||
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant import data_entry_flow
|
from homeassistant import data_entry_flow
|
||||||
@ -9,7 +10,6 @@ from homeassistant.components.aemet.const import CONF_STATION_UPDATES, DOMAIN
|
|||||||
from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
|
from homeassistant.config_entries import SOURCE_USER, ConfigEntryState
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.util.dt as dt_util
|
|
||||||
|
|
||||||
from .util import mock_api_call
|
from .util import mock_api_call
|
||||||
|
|
||||||
@ -59,13 +59,15 @@ async def test_form(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> None:
|
|||||||
assert len(mock_setup_entry.mock_calls) == 1
|
assert len(mock_setup_entry.mock_calls) == 1
|
||||||
|
|
||||||
|
|
||||||
async def test_form_options(hass: HomeAssistant) -> None:
|
async def test_form_options(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
) -> None:
|
||||||
"""Test the form options."""
|
"""Test the form options."""
|
||||||
|
|
||||||
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
|
hass.config.set_time_zone("UTC")
|
||||||
with patch("homeassistant.util.dt.now", return_value=now), patch(
|
freezer.move_to("2021-01-09 12:00:00+00:00")
|
||||||
"homeassistant.util.dt.utcnow", return_value=now
|
with patch(
|
||||||
), patch(
|
|
||||||
"homeassistant.components.aemet.AEMET.api_call",
|
"homeassistant.components.aemet.AEMET.api_call",
|
||||||
side_effect=mock_api_call,
|
side_effect=mock_api_call,
|
||||||
):
|
):
|
||||||
@ -116,13 +118,15 @@ async def test_form_options(hass: HomeAssistant) -> None:
|
|||||||
assert entry.state is ConfigEntryState.LOADED
|
assert entry.state is ConfigEntryState.LOADED
|
||||||
|
|
||||||
|
|
||||||
async def test_form_duplicated_id(hass: HomeAssistant) -> None:
|
async def test_form_duplicated_id(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
) -> None:
|
||||||
"""Test setting up duplicated entry."""
|
"""Test setting up duplicated entry."""
|
||||||
|
|
||||||
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
|
hass.config.set_time_zone("UTC")
|
||||||
with patch("homeassistant.util.dt.now", return_value=now), patch(
|
freezer.move_to("2021-01-09 12:00:00+00:00")
|
||||||
"homeassistant.util.dt.utcnow", return_value=now
|
with patch(
|
||||||
), patch(
|
|
||||||
"homeassistant.components.aemet.AEMET.api_call",
|
"homeassistant.components.aemet.AEMET.api_call",
|
||||||
side_effect=mock_api_call,
|
side_effect=mock_api_call,
|
||||||
):
|
):
|
||||||
|
@ -7,7 +7,6 @@ from homeassistant.components.aemet.const import DOMAIN
|
|||||||
from homeassistant.config_entries import ConfigEntryState
|
from homeassistant.config_entries import ConfigEntryState
|
||||||
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_NAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.util.dt as dt_util
|
|
||||||
|
|
||||||
from .util import mock_api_call
|
from .util import mock_api_call
|
||||||
|
|
||||||
@ -21,13 +20,15 @@ CONFIG = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async def test_unload_entry(hass: HomeAssistant) -> None:
|
async def test_unload_entry(
|
||||||
"""Test that the options form."""
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
) -> None:
|
||||||
|
"""Test (un)loading the AEMET integration."""
|
||||||
|
|
||||||
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
|
hass.config.set_time_zone("UTC")
|
||||||
with patch("homeassistant.util.dt.now", return_value=now), patch(
|
freezer.move_to("2021-01-09 12:00:00+00:00")
|
||||||
"homeassistant.util.dt.utcnow", return_value=now
|
with patch(
|
||||||
), patch(
|
|
||||||
"homeassistant.components.aemet.AEMET.api_call",
|
"homeassistant.components.aemet.AEMET.api_call",
|
||||||
side_effect=mock_api_call,
|
side_effect=mock_api_call,
|
||||||
):
|
):
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
"""The sensor tests for the AEMET OpenData platform."""
|
"""The sensor tests for the AEMET OpenData platform."""
|
||||||
from unittest.mock import patch
|
|
||||||
|
from freezegun.api import FrozenDateTimeFactory
|
||||||
|
|
||||||
from homeassistant.components.weather import (
|
from homeassistant.components.weather import (
|
||||||
ATTR_CONDITION_PARTLYCLOUDY,
|
ATTR_CONDITION_PARTLYCLOUDY,
|
||||||
@ -12,14 +13,14 @@ import homeassistant.util.dt as dt_util
|
|||||||
from .util import async_init_integration
|
from .util import async_init_integration
|
||||||
|
|
||||||
|
|
||||||
async def test_aemet_forecast_create_sensors(hass: HomeAssistant) -> None:
|
async def test_aemet_forecast_create_sensors(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
) -> None:
|
||||||
"""Test creation of forecast sensors."""
|
"""Test creation of forecast sensors."""
|
||||||
|
|
||||||
hass.config.set_time_zone("UTC")
|
hass.config.set_time_zone("UTC")
|
||||||
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
|
freezer.move_to("2021-01-09 12:00:00+00:00")
|
||||||
with patch("homeassistant.util.dt.now", return_value=now), patch(
|
|
||||||
"homeassistant.util.dt.utcnow", return_value=now
|
|
||||||
):
|
|
||||||
await async_init_integration(hass)
|
await async_init_integration(hass)
|
||||||
|
|
||||||
state = hass.states.get("sensor.aemet_daily_forecast_condition")
|
state = hass.states.get("sensor.aemet_daily_forecast_condition")
|
||||||
@ -73,13 +74,14 @@ async def test_aemet_forecast_create_sensors(hass: HomeAssistant) -> None:
|
|||||||
assert state is None
|
assert state is None
|
||||||
|
|
||||||
|
|
||||||
async def test_aemet_weather_create_sensors(hass: HomeAssistant) -> None:
|
async def test_aemet_weather_create_sensors(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
) -> None:
|
||||||
"""Test creation of weather sensors."""
|
"""Test creation of weather sensors."""
|
||||||
|
|
||||||
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
|
hass.config.set_time_zone("UTC")
|
||||||
with patch("homeassistant.util.dt.now", return_value=now), patch(
|
freezer.move_to("2021-01-09 12:00:00+00:00")
|
||||||
"homeassistant.util.dt.utcnow", return_value=now
|
|
||||||
):
|
|
||||||
await async_init_integration(hass)
|
await async_init_integration(hass)
|
||||||
|
|
||||||
state = hass.states.get("sensor.aemet_condition")
|
state = hass.states.get("sensor.aemet_condition")
|
||||||
|
@ -40,14 +40,14 @@ from .util import async_init_integration, mock_api_call
|
|||||||
from tests.typing import WebSocketGenerator
|
from tests.typing import WebSocketGenerator
|
||||||
|
|
||||||
|
|
||||||
async def test_aemet_weather(hass: HomeAssistant) -> None:
|
async def test_aemet_weather(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
) -> None:
|
||||||
"""Test states of the weather."""
|
"""Test states of the weather."""
|
||||||
|
|
||||||
hass.config.set_time_zone("UTC")
|
hass.config.set_time_zone("UTC")
|
||||||
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
|
freezer.move_to("2021-01-09 12:00:00+00:00")
|
||||||
with patch("homeassistant.util.dt.now", return_value=now), patch(
|
|
||||||
"homeassistant.util.dt.utcnow", return_value=now
|
|
||||||
):
|
|
||||||
await async_init_integration(hass)
|
await async_init_integration(hass)
|
||||||
|
|
||||||
state = hass.states.get("weather.aemet")
|
state = hass.states.get("weather.aemet")
|
||||||
@ -76,8 +76,11 @@ async def test_aemet_weather(hass: HomeAssistant) -> None:
|
|||||||
assert state is None
|
assert state is None
|
||||||
|
|
||||||
|
|
||||||
async def test_aemet_weather_legacy(hass: HomeAssistant) -> None:
|
async def test_aemet_weather_legacy(
|
||||||
"""Test states of the weather."""
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
|
) -> None:
|
||||||
|
"""Test states of legacy weather."""
|
||||||
|
|
||||||
registry = er.async_get(hass)
|
registry = er.async_get(hass)
|
||||||
registry.async_get_or_create(
|
registry.async_get_or_create(
|
||||||
@ -87,10 +90,7 @@ async def test_aemet_weather_legacy(hass: HomeAssistant) -> None:
|
|||||||
)
|
)
|
||||||
|
|
||||||
hass.config.set_time_zone("UTC")
|
hass.config.set_time_zone("UTC")
|
||||||
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
|
freezer.move_to("2021-01-09 12:00:00+00:00")
|
||||||
with patch("homeassistant.util.dt.now", return_value=now), patch(
|
|
||||||
"homeassistant.util.dt.utcnow", return_value=now
|
|
||||||
):
|
|
||||||
await async_init_integration(hass)
|
await async_init_integration(hass)
|
||||||
|
|
||||||
state = hass.states.get("weather.aemet_daily")
|
state = hass.states.get("weather.aemet_daily")
|
||||||
@ -121,14 +121,13 @@ async def test_aemet_weather_legacy(hass: HomeAssistant) -> None:
|
|||||||
|
|
||||||
async def test_forecast_service(
|
async def test_forecast_service(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
freezer: FrozenDateTimeFactory,
|
||||||
snapshot: SnapshotAssertion,
|
snapshot: SnapshotAssertion,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Test multiple forecast."""
|
"""Test multiple forecast."""
|
||||||
|
|
||||||
hass.config.set_time_zone("UTC")
|
hass.config.set_time_zone("UTC")
|
||||||
now = dt_util.parse_datetime("2021-01-09 12:00:00+00:00")
|
freezer.move_to("2021-01-09 12:00:00+00:00")
|
||||||
with patch("homeassistant.util.dt.now", return_value=now), patch(
|
|
||||||
"homeassistant.util.dt.utcnow", return_value=now
|
|
||||||
):
|
|
||||||
await async_init_integration(hass)
|
await async_init_integration(hass)
|
||||||
|
|
||||||
response = await hass.services.async_call(
|
response = await hass.services.async_call(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user