diff --git a/tests/components/sensibo/conftest.py b/tests/components/sensibo/conftest.py index 980c761a36958eb2531afda3d56713266639c65b..d853a15bfc3d4619ad62d89e048b5b0cd54f0749 100644 --- a/tests/components/sensibo/conftest.py +++ b/tests/components/sensibo/conftest.py @@ -7,7 +7,7 @@ import json from typing import Any from unittest.mock import AsyncMock, MagicMock, patch -from pysensibo import APIV1, APIV2, SensiboClient, SensiboData +from pysensibo import SensiboClient, SensiboData import pytest from homeassistant.components.sensibo.const import DOMAIN, PLATFORMS @@ -62,17 +62,21 @@ async def load_int( @pytest.fixture(name="mock_client") async def get_client( hass: HomeAssistant, - get_data: tuple[SensiboData, dict[str, Any]], + get_data: tuple[SensiboData, dict[str, Any], dict[str, Any]], ) -> AsyncGenerator[MagicMock]: """Mock SensiboClient.""" - with patch( - "homeassistant.components.sensibo.coordinator.SensiboClient", - autospec=True, - ) as mock_client: + with ( + patch( + "homeassistant.components.sensibo.coordinator.SensiboClient", + autospec=True, + ) as mock_client, + patch("homeassistant.components.sensibo.util.SensiboClient", new=mock_client), + ): client = mock_client.return_value client.async_get_devices_data.return_value = get_data[0] client.async_get_me.return_value = get_data[1] + client.async_get_devices.return_value = get_data[2] yield client @@ -81,24 +85,23 @@ async def get_data_from_library( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, load_json: tuple[dict[str, Any], dict[str, Any]], -) -> AsyncGenerator[tuple[SensiboData, dict[str, Any]]]: +) -> AsyncGenerator[tuple[SensiboData, dict[str, Any], dict[str, Any]]]: """Get data from api.""" - aioclient_mock.request( - "GET", - url=APIV1 + "/users/me", - params={"apiKey": "1234567890"}, - json=load_json[1], - ) - aioclient_mock.request( - "GET", - url=APIV2 + "/users/me/pods", - params={"apiKey": "1234567890", "fields": "*"}, - json=load_json[0], - ) client = SensiboClient("1234567890", aioclient_mock.create_session(hass.loop)) - me = await client.async_get_me() - data = await client.async_get_devices_data() - yield (data, me) + with patch.object( + client, + "async_get_me", + return_value=load_json[1], + ): + me = await client.async_get_me() + with patch.object( + client, + "async_get_devices", + return_value=load_json[0], + ): + data = await client.async_get_devices_data() + raw_data = await client.async_get_devices() + yield (data, me, raw_data) await client._session.close() diff --git a/tests/components/sensibo/test_binary_sensor.py b/tests/components/sensibo/test_binary_sensor.py index 292fe52de40492c623d575ac6a43b7a767742ab5..771b4a0b6b500e86a9428f2364ee73cf17828821 100644 --- a/tests/components/sensibo/test_binary_sensor.py +++ b/tests/components/sensibo/test_binary_sensor.py @@ -3,11 +3,9 @@ from __future__ import annotations from datetime import timedelta -from typing import Any -from unittest.mock import patch +from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory -from pysensibo import SensiboData import pytest from syrupy.assertion import SnapshotAssertion @@ -27,8 +25,7 @@ from tests.common import async_fire_time_changed, snapshot_platform async def test_binary_sensor( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, - get_data: tuple[SensiboData, dict[str, Any]], + mock_client: MagicMock, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, freezer: FrozenDateTimeFactory, @@ -37,17 +34,13 @@ async def test_binary_sensor( await snapshot_platform(hass, entity_registry, snapshot, load_int.entry_id) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"].motion_sensors["AABBCC"], "motion", False - ) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].motion_sensors[ + "AABBCC" + ].motion = False - with patch( - "homeassistant.components.sensibo.coordinator.SensiboClient.async_get_devices_data", - return_value=get_data, - ): - freezer.tick(timedelta(minutes=5)) - async_fire_time_changed(hass) - await hass.async_block_till_done() + freezer.tick(timedelta(minutes=5)) + async_fire_time_changed(hass) + await hass.async_block_till_done() assert ( hass.states.get("binary_sensor.hallway_motion_sensor_connectivity").state diff --git a/tests/components/sensibo/test_button.py b/tests/components/sensibo/test_button.py index 556c7ac344d97ba5bfed3f9e0fe5d229cc063f71..2d93fd81ed7728a17d540cfda59b8d763a5e0130 100644 --- a/tests/components/sensibo/test_button.py +++ b/tests/components/sensibo/test_button.py @@ -3,12 +3,10 @@ from __future__ import annotations from datetime import timedelta -from typing import Any from unittest.mock import MagicMock from freezegun import freeze_time from freezegun.api import FrozenDateTimeFactory -from pysensibo import SensiboData import pytest from syrupy.assertion import SnapshotAssertion @@ -54,9 +52,7 @@ async def test_button( async def test_button_update( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo button press.""" @@ -85,12 +81,12 @@ async def test_button_update( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "filter_clean", False) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "filter_last_reset", - today, - ) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].filter_clean = False + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].filter_last_reset = today freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) diff --git a/tests/components/sensibo/test_climate.py b/tests/components/sensibo/test_climate.py index 6bcfee656359e79904de5e2815304f3998b6ef54..089d9e107b05bbd676ce40ce2c9cd2b4b2a4fd7a 100644 --- a/tests/components/sensibo/test_climate.py +++ b/tests/components/sensibo/test_climate.py @@ -3,11 +3,9 @@ from __future__ import annotations from datetime import datetime, timedelta -from typing import Any from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory -from pysensibo.model import SensiboData import pytest from syrupy.assertion import SnapshotAssertion from voluptuous import MultipleInvalid @@ -109,9 +107,7 @@ async def test_climate( async def test_climate_fan( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate fan service.""" @@ -119,21 +115,21 @@ async def test_climate_fan( state = hass.states.get("climate.hallway") assert state.attributes["fan_mode"] == "high" - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "fan_modes", - ["quiet", "low", "medium", "not_in_ha"], - ) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "fan_modes_translated", - { - "low": "low", - "medium": "medium", - "quiet": "quiet", - "not_in_ha": "not_in_ha", - }, - ) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].fan_modes = [ + "quiet", + "low", + "medium", + "not_in_ha", + ] + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].fan_modes_translated = { + "low": "low", + "medium": "medium", + "quiet": "quiet", + "not_in_ha": "not_in_ha", + } + with pytest.raises( HomeAssistantError, match="Climate fan mode not_in_ha is not supported by the integration", @@ -159,19 +155,17 @@ async def test_climate_fan( state = hass.states.get("climate.hallway") assert state.attributes["fan_mode"] == "low" - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "active_features", - [ - "timestamp", - "on", - "mode", - "swing", - "targetTemperature", - "horizontalSwing", - "light", - ], - ) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].active_features = [ + "timestamp", + "on", + "mode", + "swing", + "targetTemperature", + "horizontalSwing", + "light", + ] freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -192,9 +186,7 @@ async def test_climate_fan( async def test_climate_swing( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate swing service.""" @@ -202,21 +194,21 @@ async def test_climate_swing( state = hass.states.get("climate.hallway") assert state.attributes["swing_mode"] == "stopped" - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "swing_modes", - ["stopped", "fixedtop", "fixedmiddletop", "not_in_ha"], - ) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "swing_modes_translated", - { - "fixedmiddletop": "fixedMiddleTop", - "fixedtop": "fixedTop", - "stopped": "stopped", - "not_in_ha": "not_in_ha", - }, - ) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].swing_modes = [ + "stopped", + "fixedtop", + "fixedmiddletop", + "not_in_ha", + ] + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].swing_modes_translated = { + "fixedmiddletop": "fixedMiddleTop", + "fixedtop": "fixedTop", + "stopped": "stopped", + "not_in_ha": "not_in_ha", + } + freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) await hass.async_block_till_done() @@ -246,18 +238,16 @@ async def test_climate_swing( state = hass.states.get("climate.hallway") assert state.attributes["swing_mode"] == "fixedtop" - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "active_features", - [ - "timestamp", - "on", - "mode", - "targetTemperature", - "horizontalSwing", - "light", - ], - ) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].active_features = [ + "timestamp", + "on", + "mode", + "targetTemperature", + "horizontalSwing", + "light", + ] freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) await hass.async_block_till_done() @@ -277,9 +267,7 @@ async def test_climate_swing( async def test_climate_temperatures( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate temperature service.""" @@ -370,18 +358,16 @@ async def test_climate_temperatures( state = hass.states.get("climate.hallway") assert state.attributes["temperature"] == 20 - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "active_features", - [ - "timestamp", - "on", - "mode", - "swing", - "horizontalSwing", - "light", - ], - ) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].active_features = [ + "timestamp", + "on", + "mode", + "swing", + "horizontalSwing", + "light", + ] freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -402,32 +388,24 @@ async def test_climate_temperatures( async def test_climate_temperature_is_none( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate temperature service no temperature provided.""" - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "active_features", - [ - "timestamp", - "on", - "mode", - "fanLevel", - "targetTemperature", - "swing", - "horizontalSwing", - "light", - ], - ) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "target_temp", - 25, - ) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].active_features = [ + "timestamp", + "on", + "mode", + "fanLevel", + "targetTemperature", + "swing", + "horizontalSwing", + "light", + ] + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].target_temp = 25 freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -455,27 +433,23 @@ async def test_climate_temperature_is_none( async def test_climate_hvac_mode( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate hvac mode service.""" - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "active_features", - [ - "timestamp", - "on", - "mode", - "fanLevel", - "targetTemperature", - "swing", - "horizontalSwing", - "light", - ], - ) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].active_features = [ + "timestamp", + "on", + "mode", + "fanLevel", + "targetTemperature", + "swing", + "horizontalSwing", + "light", + ] freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -498,7 +472,9 @@ async def test_climate_hvac_mode( state = hass.states.get("climate.hallway") assert state.state == HVACMode.OFF - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "device_on", False) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].device_on = False freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -518,15 +494,15 @@ async def test_climate_hvac_mode( async def test_climate_on_off( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate on/off service.""" - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "hvac_mode", "heat") - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "device_on", True) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].hvac_mode = "heat" + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].device_on = True freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -563,15 +539,15 @@ async def test_climate_on_off( async def test_climate_service_failed( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate service failed.""" - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "hvac_mode", "heat") - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "device_on", True) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].hvac_mode = "heat" + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].device_on = True freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -599,15 +575,15 @@ async def test_climate_service_failed( async def test_climate_assumed_state( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate assumed state service.""" - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "hvac_mode", "heat") - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "device_on", True) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].hvac_mode = "heat" + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].device_on = True freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -634,8 +610,7 @@ async def test_climate_assumed_state( async def test_climate_no_fan_no_swing( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, - get_data: tuple[SensiboData, dict[str, Any]], + mock_client: MagicMock, freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate fan.""" @@ -644,10 +619,14 @@ async def test_climate_no_fan_no_swing( assert state.attributes["fan_mode"] == "high" assert state.attributes["swing_mode"] == "stopped" - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "fan_mode", None) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "swing_mode", None) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "fan_modes", None) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "swing_modes", None) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].fan_mode = None + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].swing_mode = None + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].fan_modes = None + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].swing_modes = None freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -664,9 +643,7 @@ async def test_climate_no_fan_no_swing( async def test_climate_set_timer( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate Set Timer service.""" @@ -716,14 +693,16 @@ async def test_climate_set_timer( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "timer_on", True) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "timer_id", "SzTGE4oZ4D") - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "timer_state_on", False) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "timer_time", - datetime(2022, 6, 6, 12, 00, 00, tzinfo=dt_util.UTC), - ) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].timer_on = True + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].timer_id = "SzTGE4oZ4D" + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].timer_state_on = False + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].timer_time = datetime(2022, 6, 6, 12, 00, 00, tzinfo=dt_util.UTC) freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -739,9 +718,7 @@ async def test_climate_set_timer( async def test_climate_pure_boost( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate pure boost service.""" @@ -795,12 +772,18 @@ async def test_climate_pure_boost( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["AAZZAAZZ"], "pure_boost_enabled", True) - monkeypatch.setattr(get_data[0].parsed["AAZZAAZZ"], "pure_sensitivity", "s") - monkeypatch.setattr( - get_data[0].parsed["AAZZAAZZ"], "pure_measure_integration", True - ) - monkeypatch.setattr(get_data[0].parsed["AAZZAAZZ"], "pure_prime_integration", True) + mock_client.async_get_devices_data.return_value.parsed[ + "AAZZAAZZ" + ].pure_boost_enabled = True + mock_client.async_get_devices_data.return_value.parsed[ + "AAZZAAZZ" + ].pure_sensitivity = "s" + mock_client.async_get_devices_data.return_value.parsed[ + "AAZZAAZZ" + ].pure_measure_integration = True + mock_client.async_get_devices_data.return_value.parsed[ + "AAZZAAZZ" + ].pure_prime_integration = True freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -826,9 +809,7 @@ async def test_climate_pure_boost( async def test_climate_climate_react( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate react custom service.""" @@ -917,42 +898,40 @@ async def test_climate_climate_react( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "smart_on", True) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "smart_type", "temperature") - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], "smart_low_temp_threshold", 5.5 - ) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], "smart_high_temp_threshold", 30.5 - ) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "smart_low_state", - { - "on": True, - "targetTemperature": 25, - "temperatureUnit": "C", - "mode": "heat", - "fanLevel": "low", - "swing": "stopped", - "horizontalSwing": "stopped", - "light": "on", - }, - ) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "smart_high_state", - { - "on": True, - "targetTemperature": 15, - "temperatureUnit": "C", - "mode": "cool", - "fanLevel": "high", - "swing": "stopped", - "horizontalSwing": "stopped", - "light": "on", - }, - ) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].smart_on = True + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_type = "temperature" + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_low_temp_threshold = 5.5 + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_high_temp_threshold = 30.5 + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_low_state = { + "on": True, + "targetTemperature": 25, + "temperatureUnit": "C", + "mode": "heat", + "fanLevel": "low", + "swing": "stopped", + "horizontalSwing": "stopped", + "light": "on", + } + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_high_state = { + "on": True, + "targetTemperature": 15, + "temperatureUnit": "C", + "mode": "cool", + "fanLevel": "high", + "swing": "stopped", + "horizontalSwing": "stopped", + "light": "on", + } freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -974,9 +953,7 @@ async def test_climate_climate_react( async def test_climate_climate_react_fahrenheit( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate react custom service with fahrenheit.""" @@ -1050,40 +1027,40 @@ async def test_climate_climate_react_fahrenheit( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "smart_on", True) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "smart_type", "temperature") - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "smart_low_temp_threshold", 0) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], "smart_high_temp_threshold", 25 - ) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "smart_low_state", - { - "on": True, - "targetTemperature": 85, - "temperatureUnit": "F", - "mode": "heat", - "fanLevel": "low", - "swing": "stopped", - "horizontalSwing": "stopped", - "light": "on", - }, - ) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "smart_high_state", - { - "on": True, - "targetTemperature": 65, - "temperatureUnit": "F", - "mode": "cool", - "fanLevel": "high", - "swing": "stopped", - "horizontalSwing": "stopped", - "light": "on", - }, - ) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].smart_on = True + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_type = "temperature" + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_low_temp_threshold = 0 + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_high_temp_threshold = 25 + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_low_state = { + "on": True, + "targetTemperature": 85, + "temperatureUnit": "F", + "mode": "heat", + "fanLevel": "low", + "swing": "stopped", + "horizontalSwing": "stopped", + "light": "on", + } + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_high_state = { + "on": True, + "targetTemperature": 65, + "temperatureUnit": "F", + "mode": "cool", + "fanLevel": "high", + "swing": "stopped", + "horizontalSwing": "stopped", + "light": "on", + } freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -1105,9 +1082,7 @@ async def test_climate_climate_react_fahrenheit( async def test_climate_full_ac_state( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo climate Full AC state service.""" @@ -1149,15 +1124,23 @@ async def test_climate_full_ac_state( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "hvac_mode", "cool") - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "device_on", True) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "target_temp", 22) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "fan_mode", "high") - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "swing_mode", "stopped") - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], "horizontal_swing_mode", "stopped" - ) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "light_mode", "on") + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].hvac_mode = "cool" + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].device_on = True + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].target_temp = 22 + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].fan_mode = "high" + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].swing_mode = "stopped" + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].horizontal_swing_mode = "stopped" + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].light_mode = "on" freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) diff --git a/tests/components/sensibo/test_config_flow.py b/tests/components/sensibo/test_config_flow.py index 7a2ffee0067654fd2ad492bda5f4e289564e9c3e..179c57167c81f53be624996fc11e717da7f91a24 100644 --- a/tests/components/sensibo/test_config_flow.py +++ b/tests/components/sensibo/test_config_flow.py @@ -3,9 +3,9 @@ from __future__ import annotations from typing import Any -from unittest.mock import AsyncMock, MagicMock, patch +from unittest.mock import AsyncMock, MagicMock -from pysensibo import AuthenticationError, SensiboError +from pysensibo import AuthenticationError, SensiboData, SensiboError import pytest from homeassistant import config_entries @@ -37,7 +37,6 @@ async def test_basic_setup( CONF_API_KEY: "1234567890", }, ) - await hass.async_block_till_done() assert result["type"] is FlowResultType.CREATE_ENTRY assert result["version"] == 2 @@ -69,19 +68,19 @@ async def test_flow_fails( assert result["type"] is FlowResultType.FORM assert result["step_id"] == config_entries.SOURCE_USER - with patch( - "homeassistant.components.sensibo.util.SensiboClient.async_get_devices", - side_effect=error_message, - ): - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={ - CONF_API_KEY: "1234567890", - }, - ) + mock_client.async_get_devices.side_effect = error_message + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_API_KEY: "1234567890", + }, + ) assert result["errors"] == {"base": p_error} + mock_client.async_get_devices.side_effect = None + result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ @@ -96,7 +95,11 @@ async def test_flow_fails( } -async def test_flow_get_no_devices(hass: HomeAssistant, mock_client: MagicMock) -> None: +async def test_flow_get_no_devices( + hass: HomeAssistant, + mock_client: MagicMock, + get_data: tuple[SensiboData, dict[str, Any], dict[str, Any]], +) -> None: """Test config flow get no devices from api.""" result = await hass.config_entries.flow.async_init( @@ -106,19 +109,19 @@ async def test_flow_get_no_devices(hass: HomeAssistant, mock_client: MagicMock) assert result["type"] is FlowResultType.FORM assert result["step_id"] == config_entries.SOURCE_USER - with patch( - "homeassistant.components.sensibo.util.SensiboClient.async_get_devices", - return_value={"result": {}}, - ): - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={ - CONF_API_KEY: "1234567890", - }, - ) + mock_client.async_get_devices.return_value = {"result": []} + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_API_KEY: "1234567890", + }, + ) assert result["errors"] == {"base": "no_devices"} + mock_client.async_get_devices.return_value = get_data[2] + result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ @@ -134,7 +137,9 @@ async def test_flow_get_no_devices(hass: HomeAssistant, mock_client: MagicMock) async def test_flow_get_no_username( - hass: HomeAssistant, mock_client: MagicMock + hass: HomeAssistant, + mock_client: MagicMock, + get_data: tuple[SensiboData, dict[str, Any], dict[str, Any]], ) -> None: """Test config flow get no username from api.""" @@ -145,19 +150,19 @@ async def test_flow_get_no_username( assert result["type"] is FlowResultType.FORM assert result["step_id"] == config_entries.SOURCE_USER - with patch( - "homeassistant.components.sensibo.util.SensiboClient.async_get_me", - return_value={"result": {}}, - ): - result2 = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={ - CONF_API_KEY: "1234567890", - }, - ) + mock_client.async_get_me.return_value = {"result": {}} + + result2 = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_API_KEY: "1234567890", + }, + ) assert result2["errors"] == {"base": "no_username"} + mock_client.async_get_me.return_value = get_data[1] + result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={ @@ -191,7 +196,6 @@ async def test_reauth_flow(hass: HomeAssistant, mock_client: MagicMock) -> None: result["flow_id"], {CONF_API_KEY: "1234567890"}, ) - await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reauth_successful" @@ -219,25 +223,23 @@ async def test_reauth_flow_error( result = await entry.start_reauth_flow(hass) - with patch( - "homeassistant.components.sensibo.util.SensiboClient.async_get_devices", - side_effect=sideeffect, - ): - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - {CONF_API_KEY: "1234567890"}, - ) - await hass.async_block_till_done() + mock_client.async_get_devices.side_effect = sideeffect + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_API_KEY: "1234567890"}, + ) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": p_error} + mock_client.async_get_devices.side_effect = None + result = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_API_KEY: "1234567890"}, ) - await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reauth_successful" @@ -270,6 +272,7 @@ async def test_flow_reauth_no_username_or_device( get_me: dict[str, Any], p_error: str, mock_client: MagicMock, + get_data: tuple[SensiboData, dict[str, Any], dict[str, Any]], ) -> None: """Test reauth flow with errors from api.""" entry = MockConfigEntry( @@ -285,33 +288,27 @@ async def test_flow_reauth_no_username_or_device( assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reauth_confirm" - with ( - patch( - "homeassistant.components.sensibo.util.SensiboClient.async_get_devices", - return_value=get_devices, - ), - patch( - "homeassistant.components.sensibo.util.SensiboClient.async_get_me", - return_value=get_me, - ), - ): - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={ - CONF_API_KEY: "1234567890", - }, - ) - await hass.async_block_till_done() + mock_client.async_get_devices.return_value = get_devices + mock_client.async_get_me.return_value = get_me + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_API_KEY: "1234567890", + }, + ) assert result["step_id"] == "reauth_confirm" assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": p_error} + mock_client.async_get_devices.return_value = get_data[2] + mock_client.async_get_me.return_value = get_data[1] + result = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_API_KEY: "1234567890"}, ) - await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reauth_successful" @@ -337,7 +334,6 @@ async def test_reconfigure_flow(hass: HomeAssistant, mock_client: MagicMock) -> result["flow_id"], {CONF_API_KEY: "1234567890"}, ) - await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reconfigure_successful" @@ -352,7 +348,10 @@ async def test_reconfigure_flow(hass: HomeAssistant, mock_client: MagicMock) -> ], ) async def test_reconfigure_flow_error( - hass: HomeAssistant, sideeffect: Exception, p_error: str, mock_client: MagicMock + hass: HomeAssistant, + sideeffect: Exception, + p_error: str, + mock_client: MagicMock, ) -> None: """Test a reconfigure flow with error.""" entry = MockConfigEntry( @@ -365,25 +364,23 @@ async def test_reconfigure_flow_error( result = await entry.start_reconfigure_flow(hass) - with patch( - "homeassistant.components.sensibo.util.SensiboClient.async_get_devices", - side_effect=sideeffect, - ): - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - {CONF_API_KEY: "1234567890"}, - ) - await hass.async_block_till_done() + mock_client.async_get_devices.side_effect = sideeffect + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + {CONF_API_KEY: "1234567890"}, + ) assert result["step_id"] == "reconfigure" assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": p_error} + mock_client.async_get_devices.side_effect = None + result = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_API_KEY: "1234567890"}, ) - await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reconfigure_successful" @@ -416,6 +413,7 @@ async def test_flow_reconfigure_no_username_or_device( get_me: dict[str, Any], p_error: str, mock_client: MagicMock, + get_data: tuple[SensiboData, dict[str, Any], dict[str, Any]], ) -> None: """Test reconfigure flow with errors from api.""" entry = MockConfigEntry( @@ -431,33 +429,27 @@ async def test_flow_reconfigure_no_username_or_device( assert result["type"] is FlowResultType.FORM assert result["step_id"] == "reconfigure" - with ( - patch( - "homeassistant.components.sensibo.util.SensiboClient.async_get_devices", - return_value=get_devices, - ), - patch( - "homeassistant.components.sensibo.util.SensiboClient.async_get_me", - return_value=get_me, - ), - ): - result = await hass.config_entries.flow.async_configure( - result["flow_id"], - user_input={ - CONF_API_KEY: "1234567890", - }, - ) - await hass.async_block_till_done() + mock_client.async_get_devices.return_value = get_devices + mock_client.async_get_me.return_value = get_me + + result = await hass.config_entries.flow.async_configure( + result["flow_id"], + user_input={ + CONF_API_KEY: "1234567890", + }, + ) assert result["step_id"] == "reconfigure" assert result["type"] is FlowResultType.FORM assert result["errors"] == {"base": p_error} + mock_client.async_get_devices.return_value = get_data[2] + mock_client.async_get_me.return_value = get_data[1] + result = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_API_KEY: "1234567890"}, ) - await hass.async_block_till_done() assert result["type"] is FlowResultType.ABORT assert result["reason"] == "reconfigure_successful" diff --git a/tests/components/sensibo/test_coordinator.py b/tests/components/sensibo/test_coordinator.py index 62dd2f919979835843b36f3f1fcf4c32dd347d53..6cb8e6fe923041fdb8aaea435bd8050d3c0107ef 100644 --- a/tests/components/sensibo/test_coordinator.py +++ b/tests/components/sensibo/test_coordinator.py @@ -9,7 +9,6 @@ from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory from pysensibo.exceptions import AuthenticationError, SensiboError from pysensibo.model import SensiboData -import pytest from homeassistant.components.climate import HVACMode from homeassistant.components.sensibo.const import DOMAIN @@ -23,9 +22,8 @@ from tests.common import MockConfigEntry, async_fire_time_changed async def test_coordinator( hass: HomeAssistant, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], + get_data: tuple[SensiboData, dict[str, Any], dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo coordinator with errors.""" @@ -39,8 +37,10 @@ async def test_coordinator( config_entry.add_to_hass(hass) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "hvac_mode", "heat") - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "device_on", True) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].hvac_mode = "heat" + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].device_on = True mock_data = mock_client.async_get_devices_data mock_data.return_value = get_data[0] @@ -70,9 +70,6 @@ async def test_coordinator( assert state.state == STATE_UNAVAILABLE mock_data.reset_mock() - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "hvac_mode", "heat") - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "device_on", True) - mock_data.return_value = get_data[0] mock_data.side_effect = None freezer.tick(timedelta(minutes=1)) diff --git a/tests/components/sensibo/test_init.py b/tests/components/sensibo/test_init.py index b472272d1cf72404aa3df78039b2a08e7f114b42..78eee6ceba0a2758a0f0f81881bf9f93a7065739 100644 --- a/tests/components/sensibo/test_init.py +++ b/tests/components/sensibo/test_init.py @@ -2,7 +2,7 @@ from __future__ import annotations -from unittest.mock import MagicMock, patch +from unittest.mock import MagicMock from homeassistant.components.sensibo.const import DOMAIN from homeassistant.components.sensibo.util import NoUsernameError @@ -71,12 +71,10 @@ async def test_migrate_entry_fails(hass: HomeAssistant, mock_client: MagicMock) ) entry.add_to_hass(hass) - with patch( - "homeassistant.components.sensibo.util.SensiboClient.async_get_me", - side_effect=NoUsernameError("No username returned"), - ): - await hass.config_entries.async_setup(entry.entry_id) - await hass.async_block_till_done() + mock_client.async_get_me.side_effect = NoUsernameError("No username returned") + + await hass.config_entries.async_setup(entry.entry_id) + await hass.async_block_till_done() assert entry.state is ConfigEntryState.MIGRATION_ERROR assert entry.version == 1 diff --git a/tests/components/sensibo/test_number.py b/tests/components/sensibo/test_number.py index 9abb636abfbfac6171f2a7080ffe138f194b0a2f..ff5347a02d3e4f58cc471f1fa18724d906fad4f0 100644 --- a/tests/components/sensibo/test_number.py +++ b/tests/components/sensibo/test_number.py @@ -3,11 +3,9 @@ from __future__ import annotations from datetime import timedelta -from typing import Any from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory -from pysensibo.model import SensiboData import pytest from syrupy.assertion import SnapshotAssertion @@ -33,8 +31,7 @@ from tests.common import async_fire_time_changed, snapshot_platform async def test_number( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, - get_data: tuple[SensiboData, dict[str, Any]], + mock_client: MagicMock, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, freezer: FrozenDateTimeFactory, @@ -43,7 +40,9 @@ async def test_number( await snapshot_platform(hass, entity_registry, snapshot, load_int.entry_id) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "calibration_temp", 0.2) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].calibration_temp = 0.2 freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) diff --git a/tests/components/sensibo/test_select.py b/tests/components/sensibo/test_select.py index 002e2a62540d7ddd9196d80d2ec5d01b9079dc8a..5e1c3f68e41987e556688f07f44a306db7ff8b7d 100644 --- a/tests/components/sensibo/test_select.py +++ b/tests/components/sensibo/test_select.py @@ -3,11 +3,9 @@ from __future__ import annotations from datetime import timedelta -from typing import Any from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory -from pysensibo.model import SensiboData import pytest from syrupy.assertion import SnapshotAssertion @@ -32,8 +30,7 @@ from tests.common import async_fire_time_changed, snapshot_platform async def test_select( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, - get_data: tuple[SensiboData, dict[str, Any]], + mock_client: MagicMock, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, freezer: FrozenDateTimeFactory, @@ -42,9 +39,9 @@ async def test_select( await snapshot_platform(hass, entity_registry, snapshot, load_int.entry_id) - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], "horizontal_swing_mode", "fixedleft" - ) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].horizontal_swing_mode = "fixedleft" freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -57,24 +54,20 @@ async def test_select( async def test_select_set_option( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo select service.""" - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "active_features", - [ - "timestamp", - "on", - "mode", - "targetTemperature", - "light", - ], - ) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].active_features = [ + "timestamp", + "on", + "mode", + "targetTemperature", + "light", + ] freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -100,18 +93,16 @@ async def test_select_set_option( state = hass.states.get("select.hallway_horizontal_swing") assert state.state == "stopped" - monkeypatch.setattr( - get_data[0].parsed["ABC999111"], - "active_features", - [ - "timestamp", - "on", - "mode", - "targetTemperature", - "horizontalSwing", - "light", - ], - ) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].active_features = [ + "timestamp", + "on", + "mode", + "targetTemperature", + "horizontalSwing", + "light", + ] freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) diff --git a/tests/components/sensibo/test_sensor.py b/tests/components/sensibo/test_sensor.py index 57bb3f93a048b333f0194caf8cd5653c218b027c..8ea760361231df96198ca309dba0e4686c30b002 100644 --- a/tests/components/sensibo/test_sensor.py +++ b/tests/components/sensibo/test_sensor.py @@ -3,10 +3,10 @@ from __future__ import annotations from datetime import timedelta -from typing import Any +from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory -from pysensibo.model import PureAQI, SensiboData +from pysensibo.model import PureAQI import pytest from syrupy.assertion import SnapshotAssertion @@ -26,8 +26,7 @@ from tests.common import async_fire_time_changed, snapshot_platform async def test_sensor( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, - get_data: tuple[SensiboData, dict[str, Any]], + mock_client: MagicMock, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, freezer: FrozenDateTimeFactory, @@ -36,7 +35,9 @@ async def test_sensor( await snapshot_platform(hass, entity_registry, snapshot, load_int.entry_id) - monkeypatch.setattr(get_data[0].parsed["AAZZAAZZ"], "pm25_pure", PureAQI(2)) + mock_client.async_get_devices_data.return_value.parsed[ + "AAZZAAZZ" + ].pm25_pure = PureAQI(2) freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) diff --git a/tests/components/sensibo/test_switch.py b/tests/components/sensibo/test_switch.py index 6f77c60a63d009de52abf8c03e28fb95fc657802..43d87c3153576d4cbc1fb25195f9b9b8158b40f1 100644 --- a/tests/components/sensibo/test_switch.py +++ b/tests/components/sensibo/test_switch.py @@ -3,11 +3,9 @@ from __future__ import annotations from datetime import timedelta -from typing import Any from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory -from pysensibo.model import SensiboData import pytest from syrupy.assertion import SnapshotAssertion @@ -46,9 +44,7 @@ async def test_switch( async def test_switch_timer( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo switch timer.""" @@ -72,9 +68,13 @@ async def test_switch_timer( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "timer_on", True) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "timer_id", "SzTGE4oZ4D") - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "timer_state_on", False) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].timer_on = True + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].timer_id = "SzTGE4oZ4D" + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].timer_state_on = False freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -99,7 +99,7 @@ async def test_switch_timer( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "timer_on", False) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].timer_on = False freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -112,9 +112,7 @@ async def test_switch_timer( async def test_switch_pure_boost( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo switch pure boost.""" @@ -133,10 +131,12 @@ async def test_switch_pure_boost( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["AAZZAAZZ"], "pure_boost_enabled", True) - monkeypatch.setattr( - get_data[0].parsed["AAZZAAZZ"], "pure_measure_integration", None - ) + mock_client.async_get_devices_data.return_value.parsed[ + "AAZZAAZZ" + ].pure_boost_enabled = True + mock_client.async_get_devices_data.return_value.parsed[ + "AAZZAAZZ" + ].pure_measure_integration = None freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -154,7 +154,9 @@ async def test_switch_pure_boost( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["AAZZAAZZ"], "pure_boost_enabled", False) + mock_client.async_get_devices_data.return_value.parsed[ + "AAZZAAZZ" + ].pure_boost_enabled = False freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -203,9 +205,7 @@ async def test_switch_command_failure( async def test_switch_climate_react( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, mock_client: MagicMock, - get_data: tuple[SensiboData, dict[str, Any]], freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo switch for climate react.""" @@ -224,7 +224,7 @@ async def test_switch_climate_react( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "smart_on", True) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].smart_on = True freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -242,7 +242,7 @@ async def test_switch_climate_react( blocking=True, ) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "smart_on", False) + mock_client.async_get_devices_data.return_value.parsed["ABC999111"].smart_on = False freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) @@ -255,13 +255,14 @@ async def test_switch_climate_react( async def test_switch_climate_react_no_data( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, - get_data: tuple[SensiboData, dict[str, Any]], + mock_client: MagicMock, freezer: FrozenDateTimeFactory, ) -> None: """Test the Sensibo switch for climate react with no data.""" - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "smart_type", None) + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].smart_type = None freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass) diff --git a/tests/components/sensibo/test_update.py b/tests/components/sensibo/test_update.py index 1a6dd9b2fc5c113bd7e811e60b542c04deed9bc4..08ee68fdb8ed422cc4f40f5c8e80089e92c56775 100644 --- a/tests/components/sensibo/test_update.py +++ b/tests/components/sensibo/test_update.py @@ -3,10 +3,9 @@ from __future__ import annotations from datetime import timedelta -from typing import Any +from unittest.mock import MagicMock from freezegun.api import FrozenDateTimeFactory -from pysensibo.model import SensiboData import pytest from syrupy.assertion import SnapshotAssertion @@ -26,8 +25,7 @@ from tests.common import async_fire_time_changed, snapshot_platform async def test_update( hass: HomeAssistant, load_int: ConfigEntry, - monkeypatch: pytest.MonkeyPatch, - get_data: tuple[SensiboData, dict[str, Any]], + mock_client: MagicMock, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion, freezer: FrozenDateTimeFactory, @@ -36,7 +34,9 @@ async def test_update( await snapshot_platform(hass, entity_registry, snapshot, load_int.entry_id) - monkeypatch.setattr(get_data[0].parsed["ABC999111"], "fw_ver", "SKY30048") + mock_client.async_get_devices_data.return_value.parsed[ + "ABC999111" + ].fw_ver = "SKY30048" freezer.tick(timedelta(minutes=5)) async_fire_time_changed(hass)