From cbe58913eaf6e5240b57f5a51c21919ccabcdd0f Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Sun, 12 Dec 2021 23:09:45 +0100 Subject: [PATCH] Fix freedompro tests (#61012) --- tests/components/freedompro/conftest.py | 9 +++ .../freedompro/test_binary_sensor.py | 24 +++---- tests/components/freedompro/test_climate.py | 23 ++++--- tests/components/freedompro/test_cover.py | 56 +++++++++++---- tests/components/freedompro/test_fan.py | 69 +++++++++++++++---- tests/components/freedompro/test_lock.py | 44 +++++++++--- tests/components/freedompro/test_sensor.py | 20 +++--- tests/components/freedompro/test_switch.py | 45 +++++++++--- 8 files changed, 212 insertions(+), 78 deletions(-) diff --git a/tests/components/freedompro/conftest.py b/tests/components/freedompro/conftest.py index 36070c1a0d5..804dc6d1933 100644 --- a/tests/components/freedompro/conftest.py +++ b/tests/components/freedompro/conftest.py @@ -1,4 +1,8 @@ """Fixtures for Freedompro integration tests.""" +from __future__ import annotations + +from copy import deepcopy +from typing import Any from unittest.mock import patch import pytest @@ -71,3 +75,8 @@ async def init_integration_no_state(hass) -> MockConfigEntry: await hass.async_block_till_done() return entry + + +def get_states_response_for_uid(uid: str) -> list[dict[str, Any]]: + """Return a deepcopy of the device state list for specific uid.""" + return deepcopy([resp for resp in DEVICES_STATE if resp["uid"] == uid]) diff --git a/tests/components/freedompro/test_binary_sensor.py b/tests/components/freedompro/test_binary_sensor.py index 785a6b03212..459484dbc45 100644 --- a/tests/components/freedompro/test_binary_sensor.py +++ b/tests/components/freedompro/test_binary_sensor.py @@ -9,7 +9,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util.dt import utcnow from tests.common import async_fire_time_changed -from tests.components.freedompro.const import DEVICES_STATE +from tests.components.freedompro.conftest import get_states_response_for_uid @pytest.mark.parametrize( @@ -84,20 +84,18 @@ async def test_binary_sensor_get_state( assert state.state == STATE_OFF - get_states_response = list(DEVICES_STATE) - for state_response in get_states_response: - if state_response["uid"] == uid: - if state_response["type"] == "smokeSensor": - state_response["state"]["smokeDetected"] = True - if state_response["type"] == "occupancySensor": - state_response["state"]["occupancyDetected"] = True - if state_response["type"] == "motionSensor": - state_response["state"]["motionDetected"] = True - if state_response["type"] == "contactSensor": - state_response["state"]["contactSensorState"] = True + states_response = get_states_response_for_uid(uid) + if states_response[0]["type"] == "smokeSensor": + states_response[0]["state"]["smokeDetected"] = True + elif states_response[0]["type"] == "occupancySensor": + states_response[0]["state"]["occupancyDetected"] = True + elif states_response[0]["type"] == "motionSensor": + states_response[0]["state"]["motionDetected"] = True + elif states_response[0]["type"] == "contactSensor": + states_response[0]["state"]["contactSensorState"] = True with patch( "homeassistant.components.freedompro.get_states", - return_value=get_states_response, + return_value=states_response, ): async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) diff --git a/tests/components/freedompro/test_climate.py b/tests/components/freedompro/test_climate.py index 36ec3309d24..95c13a46766 100644 --- a/tests/components/freedompro/test_climate.py +++ b/tests/components/freedompro/test_climate.py @@ -25,7 +25,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util.dt import utcnow from tests.common import async_fire_time_changed -from tests.components.freedompro.const import DEVICES_STATE +from tests.components.freedompro.conftest import get_states_response_for_uid uid = "3WRRJR6RCZQZSND8VP0YTO3YXCSOFPKBMW8T51TU-LQ*TWMYQKL3UVED4HSIIB9GXJWJZBQCXG-9VE-N2IUAIWI" @@ -64,14 +64,12 @@ async def test_climate_get_state(hass, init_integration): assert entry assert entry.unique_id == uid - get_states_response = list(DEVICES_STATE) - for state_response in get_states_response: - if state_response["uid"] == uid: - state_response["state"]["currentTemperature"] = 20 - state_response["state"]["targetTemperature"] = 21 + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["currentTemperature"] = 20 + states_response[0]["state"]["targetTemperature"] = 21 with patch( "homeassistant.components.freedompro.get_states", - return_value=get_states_response, + return_value=states_response, ): async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) await hass.async_block_till_done() @@ -172,7 +170,16 @@ async def test_climate_set_temperature(hass, init_integration): ANY, ANY, ANY, '{"heatingCoolingState": 0, "targetTemperature": 25.0}' ) - await hass.async_block_till_done() + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["currentTemperature"] = 20 + states_response[0]["state"]["targetTemperature"] = 21 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) assert state.attributes[ATTR_TEMPERATURE] == 21 diff --git a/tests/components/freedompro/test_cover.py b/tests/components/freedompro/test_cover.py index d0338dec82c..11dc35b374c 100644 --- a/tests/components/freedompro/test_cover.py +++ b/tests/components/freedompro/test_cover.py @@ -17,7 +17,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util.dt import utcnow from tests.common import async_fire_time_changed -from tests.components.freedompro.const import DEVICES_STATE +from tests.components.freedompro.conftest import get_states_response_for_uid @pytest.mark.parametrize( @@ -55,13 +55,11 @@ async def test_cover_get_state( assert entry assert entry.unique_id == uid - get_states_response = list(DEVICES_STATE) - for state_response in get_states_response: - if state_response["uid"] == uid: - state_response["state"]["position"] = 100 + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["position"] = 100 with patch( "homeassistant.components.freedompro.get_states", - return_value=get_states_response, + return_value=states_response, ): async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) await hass.async_block_till_done() @@ -97,7 +95,7 @@ async def test_cover_set_position( state = hass.states.get(entity_id) assert state - assert state.state == STATE_OPEN + assert state.state == STATE_CLOSED assert state.attributes.get("friendly_name") == name entry = registry.async_get(entity_id) @@ -113,9 +111,18 @@ async def test_cover_set_position( ) mock_put_state.assert_called_once_with(ANY, ANY, ANY, '{"position": 33}') - await hass.async_block_till_done() + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["position"] = 33 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) assert state.state == STATE_OPEN + assert state.attributes["current_position"] == 33 @pytest.mark.parametrize( @@ -136,6 +143,16 @@ async def test_cover_close( init_integration registry = er.async_get(hass) + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["position"] = 100 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + await hass.helpers.entity_component.async_update_entity(entity_id) + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) assert state assert state.state == STATE_OPEN @@ -154,9 +171,16 @@ async def test_cover_close( ) mock_put_state.assert_called_once_with(ANY, ANY, ANY, '{"position": 0}') - await hass.async_block_till_done() + states_response[0]["state"]["position"] = 0 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) - assert state.state == STATE_OPEN + assert state.state == STATE_CLOSED @pytest.mark.parametrize( @@ -179,7 +203,7 @@ async def test_cover_open( state = hass.states.get(entity_id) assert state - assert state.state == STATE_OPEN + assert state.state == STATE_CLOSED assert state.attributes.get("friendly_name") == name entry = registry.async_get(entity_id) @@ -195,6 +219,14 @@ async def test_cover_open( ) mock_put_state.assert_called_once_with(ANY, ANY, ANY, '{"position": 100}') - await hass.async_block_till_done() + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["position"] = 100 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) assert state.state == STATE_OPEN diff --git a/tests/components/freedompro/test_fan.py b/tests/components/freedompro/test_fan.py index 6bf4bbe1e04..3404c5d17e4 100644 --- a/tests/components/freedompro/test_fan.py +++ b/tests/components/freedompro/test_fan.py @@ -13,7 +13,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util.dt import utcnow from tests.common import async_fire_time_changed -from tests.components.freedompro.const import DEVICES_STATE +from tests.components.freedompro.conftest import get_states_response_for_uid uid = "3WRRJR6RCZQZSND8VP0YTO3YXCSOFPKBMW8T51TU-LQ*ILYH1E3DWZOVMNEUIMDYMNLOW-LFRQFDPWWJOVHVDOS" @@ -42,14 +42,12 @@ async def test_fan_get_state(hass, init_integration): assert entry assert entry.unique_id == uid - get_states_response = list(DEVICES_STATE) - for state_response in get_states_response: - if state_response["uid"] == uid: - state_response["state"]["on"] = True - state_response["state"]["rotationSpeed"] = 50 + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["on"] = True + states_response[0]["state"]["rotationSpeed"] = 50 with patch( "homeassistant.components.freedompro.get_states", - return_value=get_states_response, + return_value=states_response, ): async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) await hass.async_block_till_done() @@ -72,6 +70,18 @@ async def test_fan_set_off(hass, init_integration): registry = er.async_get(hass) entity_id = "fan.bedroom" + + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["on"] = True + states_response[0]["state"]["rotationSpeed"] = 50 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + await hass.helpers.entity_component.async_update_entity(entity_id) + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) assert state assert state.state == STATE_ON @@ -91,10 +101,20 @@ async def test_fan_set_off(hass, init_integration): ) mock_put_state.assert_called_once_with(ANY, ANY, ANY, '{"on": false}') + states_response[0]["state"]["on"] = False + states_response[0]["state"]["rotationSpeed"] = 0 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + await hass.helpers.entity_component.async_update_entity(entity_id) + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + await hass.async_block_till_done() state = hass.states.get(entity_id) - assert state.attributes[ATTR_PERCENTAGE] == 50 - assert state.state == STATE_ON + assert state.attributes[ATTR_PERCENTAGE] == 0 + assert state.state == STATE_OFF async def test_fan_set_on(hass, init_integration): @@ -105,8 +125,8 @@ async def test_fan_set_on(hass, init_integration): entity_id = "fan.bedroom" state = hass.states.get(entity_id) assert state - assert state.state == STATE_ON - assert state.attributes[ATTR_PERCENTAGE] == 50 + assert state.state == STATE_OFF + assert state.attributes[ATTR_PERCENTAGE] == 0 assert state.attributes.get("friendly_name") == "bedroom" entry = registry.async_get(entity_id) @@ -122,7 +142,16 @@ async def test_fan_set_on(hass, init_integration): ) mock_put_state.assert_called_once_with(ANY, ANY, ANY, '{"on": true}') - await hass.async_block_till_done() + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["on"] = True + states_response[0]["state"]["rotationSpeed"] = 50 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) assert state.attributes[ATTR_PERCENTAGE] == 50 assert state.state == STATE_ON @@ -136,8 +165,8 @@ async def test_fan_set_percent(hass, init_integration): entity_id = "fan.bedroom" state = hass.states.get(entity_id) assert state - assert state.state == STATE_ON - assert state.attributes[ATTR_PERCENTAGE] == 50 + assert state.state == STATE_OFF + assert state.attributes[ATTR_PERCENTAGE] == 0 assert state.attributes.get("friendly_name") == "bedroom" entry = registry.async_get(entity_id) @@ -153,7 +182,17 @@ async def test_fan_set_percent(hass, init_integration): ) mock_put_state.assert_called_once_with(ANY, ANY, ANY, '{"rotationSpeed": 40}') + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["on"] = True + states_response[0]["state"]["rotationSpeed"] = 40 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + await hass.async_block_till_done() state = hass.states.get(entity_id) - assert state.attributes[ATTR_PERCENTAGE] == 50 + assert state.attributes[ATTR_PERCENTAGE] == 40 assert state.state == STATE_ON diff --git a/tests/components/freedompro/test_lock.py b/tests/components/freedompro/test_lock.py index 5c30909e081..e0d25ce91d9 100644 --- a/tests/components/freedompro/test_lock.py +++ b/tests/components/freedompro/test_lock.py @@ -12,7 +12,7 @@ from homeassistant.helpers import device_registry as dr, entity_registry as er from homeassistant.util.dt import utcnow from tests.common import async_fire_time_changed -from tests.components.freedompro.const import DEVICES_STATE +from tests.components.freedompro.conftest import get_states_response_for_uid uid = "2WRRJR6RCZQZSND8VP0YTO3YXCSOFPKBMW8T51TU-LQ*2VAS3HTWINNZ5N6HVEIPDJ6NX85P2-AM-GSYWUCNPU0" @@ -40,13 +40,11 @@ async def test_lock_get_state(hass, init_integration): assert entry assert entry.unique_id == uid - get_states_response = list(DEVICES_STATE) - for state_response in get_states_response: - if state_response["uid"] == uid: - state_response["state"]["lock"] = 1 + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["lock"] = 1 with patch( "homeassistant.components.freedompro.get_states", - return_value=get_states_response, + return_value=states_response, ): async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) await hass.async_block_till_done() @@ -68,6 +66,17 @@ async def test_lock_set_unlock(hass, init_integration): registry = er.async_get(hass) entity_id = "lock.lock" + + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["lock"] = 1 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + await hass.helpers.entity_component.async_update_entity(entity_id) + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) assert state assert state.state == STATE_LOCKED @@ -86,9 +95,17 @@ async def test_lock_set_unlock(hass, init_integration): ) mock_put_state.assert_called_once_with(ANY, ANY, ANY, '{"lock": 0}') - await hass.async_block_till_done() + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["lock"] = 0 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) - assert state.state == STATE_LOCKED + assert state.state == STATE_UNLOCKED async def test_lock_set_lock(hass, init_integration): @@ -99,7 +116,7 @@ async def test_lock_set_lock(hass, init_integration): entity_id = "lock.lock" state = hass.states.get(entity_id) assert state - assert state.state == STATE_LOCKED + assert state.state == STATE_UNLOCKED assert state.attributes.get("friendly_name") == "lock" entry = registry.async_get(entity_id) @@ -115,6 +132,15 @@ async def test_lock_set_lock(hass, init_integration): ) mock_put_state.assert_called_once_with(ANY, ANY, ANY, '{"lock": 1}') + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["lock"] = 1 + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + await hass.async_block_till_done() state = hass.states.get(entity_id) assert state.state == STATE_LOCKED diff --git a/tests/components/freedompro/test_sensor.py b/tests/components/freedompro/test_sensor.py index b6f809569f1..d73506e2c6f 100644 --- a/tests/components/freedompro/test_sensor.py +++ b/tests/components/freedompro/test_sensor.py @@ -8,7 +8,7 @@ from homeassistant.helpers import entity_registry as er from homeassistant.util.dt import utcnow from tests.common import async_fire_time_changed -from tests.components.freedompro.const import DEVICES_STATE +from tests.components.freedompro.conftest import get_states_response_for_uid @pytest.mark.parametrize( @@ -48,18 +48,16 @@ async def test_sensor_get_state( assert state.state == "0" - get_states_response = list(DEVICES_STATE) - for state_response in get_states_response: - if state_response["uid"] == uid: - if state_response["type"] == "lightSensor": - state_response["state"]["currentAmbientLightLevel"] = "1" - if state_response["type"] == "temperatureSensor": - state_response["state"]["currentTemperature"] = "1" - if state_response["type"] == "humiditySensor": - state_response["state"]["currentRelativeHumidity"] = "1" + states_response = get_states_response_for_uid(uid) + if states_response[0]["type"] == "lightSensor": + states_response[0]["state"]["currentAmbientLightLevel"] = "1" + elif states_response[0]["type"] == "temperatureSensor": + states_response[0]["state"]["currentTemperature"] = "1" + elif states_response[0]["type"] == "humiditySensor": + states_response[0]["state"]["currentRelativeHumidity"] = "1" with patch( "homeassistant.components.freedompro.get_states", - return_value=get_states_response, + return_value=states_response, ): async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) diff --git a/tests/components/freedompro/test_switch.py b/tests/components/freedompro/test_switch.py index 4674b684c41..6b62e028d72 100644 --- a/tests/components/freedompro/test_switch.py +++ b/tests/components/freedompro/test_switch.py @@ -8,7 +8,7 @@ from homeassistant.helpers import entity_registry as er from homeassistant.util.dt import utcnow from tests.common import async_fire_time_changed -from tests.components.freedompro.const import DEVICES_STATE +from tests.components.freedompro.conftest import get_states_response_for_uid uid = "3WRRJR6RCZQZSND8VP0YTO3YXCSOFPKBMW8T51TU-LQ*1JKU1MVWHQL-Z9SCUS85VFXMRGNDCDNDDUVVDKBU31W" @@ -28,13 +28,11 @@ async def test_switch_get_state(hass, init_integration): assert entry assert entry.unique_id == uid - get_states_response = list(DEVICES_STATE) - for state_response in get_states_response: - if state_response["uid"] == uid: - state_response["state"]["on"] = True + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["on"] = True with patch( "homeassistant.components.freedompro.get_states", - return_value=get_states_response, + return_value=states_response, ): async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) await hass.async_block_till_done() @@ -56,6 +54,17 @@ async def test_switch_set_off(hass, init_integration): registry = er.async_get(hass) entity_id = "switch.irrigation_switch" + + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["on"] = True + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + await hass.helpers.entity_component.async_update_entity(entity_id) + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) assert state assert state.state == STATE_ON @@ -76,9 +85,17 @@ async def test_switch_set_off(hass, init_integration): ) mock_put_state.assert_called_once_with(ANY, ANY, ANY, '{"on": false}') - await hass.async_block_till_done() + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["on"] = False + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) - assert state.state == STATE_ON + assert state.state == STATE_OFF async def test_switch_set_on(hass, init_integration): @@ -89,7 +106,7 @@ async def test_switch_set_on(hass, init_integration): entity_id = "switch.irrigation_switch" state = hass.states.get(entity_id) assert state - assert state.state == STATE_ON + assert state.state == STATE_OFF assert state.attributes.get("friendly_name") == "Irrigation switch" entry = registry.async_get(entity_id) @@ -107,6 +124,14 @@ async def test_switch_set_on(hass, init_integration): ) mock_put_state.assert_called_once_with(ANY, ANY, ANY, '{"on": true}') - await hass.async_block_till_done() + states_response = get_states_response_for_uid(uid) + states_response[0]["state"]["on"] = True + with patch( + "homeassistant.components.freedompro.get_states", + return_value=states_response, + ): + async_fire_time_changed(hass, utcnow() + timedelta(hours=2)) + await hass.async_block_till_done() + state = hass.states.get(entity_id) assert state.state == STATE_ON