From a6aff613d7f28e1a1d381c8e14b36f4322369167 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Thu, 14 Oct 2021 22:53:05 +0200 Subject: [PATCH] Optimize update calls for AVM Fritz!Smarthome devices (#57579) --- homeassistant/components/fritzbox/__init__.py | 4 ++-- tests/components/fritzbox/test_binary_sensor.py | 8 ++++---- tests/components/fritzbox/test_climate.py | 12 ++++++------ tests/components/fritzbox/test_sensor.py | 10 +++++----- tests/components/fritzbox/test_switch.py | 10 +++++----- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/homeassistant/components/fritzbox/__init__.py b/homeassistant/components/fritzbox/__init__.py index 8d354f655f63..36b51a630eac 100644 --- a/homeassistant/components/fritzbox/__init__.py +++ b/homeassistant/components/fritzbox/__init__.py @@ -66,9 +66,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: devices = fritz.get_devices() data = {} + fritz.update_devices() for device in devices: - device.update() - # assume device as unavailable, see #55799 if ( device.has_powermeter @@ -78,6 +77,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: and device.power <= 0 and device.energy <= 0 ): + LOGGER.debug("Assume device %s as unavailable", device.name) device.present = False data[device.ain] = device diff --git a/tests/components/fritzbox/test_binary_sensor.py b/tests/components/fritzbox/test_binary_sensor.py index cb76109e0ff2..f010c8499ea3 100644 --- a/tests/components/fritzbox/test_binary_sensor.py +++ b/tests/components/fritzbox/test_binary_sensor.py @@ -70,14 +70,14 @@ async def test_update(hass: HomeAssistant, fritz: Mock): hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz ) - assert device.update.call_count == 1 + assert fritz().update_devices.call_count == 1 assert fritz().login.call_count == 1 next_update = dt_util.utcnow() + timedelta(seconds=200) async_fire_time_changed(hass, next_update) await hass.async_block_till_done() - assert device.update.call_count == 2 + assert fritz().update_devices.call_count == 2 assert fritz().login.call_count == 1 @@ -89,12 +89,12 @@ async def test_update_error(hass: HomeAssistant, fritz: Mock): hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz ) - assert device.update.call_count == 1 + assert fritz().update_devices.call_count == 1 assert fritz().login.call_count == 1 next_update = dt_util.utcnow() + timedelta(seconds=200) async_fire_time_changed(hass, next_update) await hass.async_block_till_done() - assert device.update.call_count == 2 + assert fritz().update_devices.call_count == 2 assert fritz().login.call_count == 1 diff --git a/tests/components/fritzbox/test_climate.py b/tests/components/fritzbox/test_climate.py index 30ee7130fea5..627c82f617a6 100644 --- a/tests/components/fritzbox/test_climate.py +++ b/tests/components/fritzbox/test_climate.py @@ -134,7 +134,7 @@ async def test_update(hass: HomeAssistant, fritz: Mock): await hass.async_block_till_done() state = hass.states.get(ENTITY_ID) - assert device.update.call_count == 2 + assert fritz().update_devices.call_count == 2 assert state assert state.attributes[ATTR_CURRENT_TEMPERATURE] == 19 assert state.attributes[ATTR_TEMPERATURE] == 20 @@ -143,19 +143,19 @@ async def test_update(hass: HomeAssistant, fritz: Mock): async def test_update_error(hass: HomeAssistant, fritz: Mock): """Test update with error.""" device = FritzDeviceClimateMock() - device.update.side_effect = HTTPError("Boom") + fritz().update_devices.side_effect = HTTPError("Boom") assert not await setup_config_entry( hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz ) - assert device.update.call_count == 1 + assert fritz().update_devices.call_count == 1 assert fritz().login.call_count == 1 next_update = dt_util.utcnow() + timedelta(seconds=200) async_fire_time_changed(hass, next_update) await hass.async_block_till_done() - assert device.update.call_count == 2 + assert fritz().update_devices.call_count == 2 assert fritz().login.call_count == 2 @@ -299,7 +299,7 @@ async def test_preset_mode_update(hass: HomeAssistant, fritz: Mock): await hass.async_block_till_done() state = hass.states.get(ENTITY_ID) - assert device.update.call_count == 2 + assert fritz().update_devices.call_count == 2 assert state assert state.attributes[ATTR_PRESET_MODE] == PRESET_COMFORT @@ -310,6 +310,6 @@ async def test_preset_mode_update(hass: HomeAssistant, fritz: Mock): await hass.async_block_till_done() state = hass.states.get(ENTITY_ID) - assert device.update.call_count == 3 + assert fritz().update_devices.call_count == 3 assert state assert state.attributes[ATTR_PRESET_MODE] == PRESET_ECO diff --git a/tests/components/fritzbox/test_sensor.py b/tests/components/fritzbox/test_sensor.py index f7a3ef9ae2a7..42cf90bba58a 100644 --- a/tests/components/fritzbox/test_sensor.py +++ b/tests/components/fritzbox/test_sensor.py @@ -70,30 +70,30 @@ async def test_update(hass: HomeAssistant, fritz: Mock): assert await setup_config_entry( hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz ) - assert device.update.call_count == 1 + assert fritz().update_devices.call_count == 1 assert fritz().login.call_count == 1 next_update = dt_util.utcnow() + timedelta(seconds=200) async_fire_time_changed(hass, next_update) await hass.async_block_till_done() - assert device.update.call_count == 2 + assert fritz().update_devices.call_count == 2 assert fritz().login.call_count == 1 async def test_update_error(hass: HomeAssistant, fritz: Mock): """Test update with error.""" device = FritzDeviceSensorMock() - device.update.side_effect = HTTPError("Boom") + fritz().update_devices.side_effect = HTTPError("Boom") assert not await setup_config_entry( hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz ) - assert device.update.call_count == 1 + assert fritz().update_devices.call_count == 1 assert fritz().login.call_count == 1 next_update = dt_util.utcnow() + timedelta(seconds=200) async_fire_time_changed(hass, next_update) await hass.async_block_till_done() - assert device.update.call_count == 2 + assert fritz().update_devices.call_count == 2 assert fritz().login.call_count == 2 diff --git a/tests/components/fritzbox/test_switch.py b/tests/components/fritzbox/test_switch.py index fb7221262d39..73bb7a1110be 100644 --- a/tests/components/fritzbox/test_switch.py +++ b/tests/components/fritzbox/test_switch.py @@ -114,32 +114,32 @@ async def test_update(hass: HomeAssistant, fritz: Mock): assert await setup_config_entry( hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz ) - assert device.update.call_count == 1 + assert fritz().update_devices.call_count == 1 assert fritz().login.call_count == 1 next_update = dt_util.utcnow() + timedelta(seconds=200) async_fire_time_changed(hass, next_update) await hass.async_block_till_done() - assert device.update.call_count == 2 + assert fritz().update_devices.call_count == 2 assert fritz().login.call_count == 1 async def test_update_error(hass: HomeAssistant, fritz: Mock): """Test update with error.""" device = FritzDeviceSwitchMock() - device.update.side_effect = HTTPError("Boom") + fritz().update_devices.side_effect = HTTPError("Boom") assert not await setup_config_entry( hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz ) - assert device.update.call_count == 1 + assert fritz().update_devices.call_count == 1 assert fritz().login.call_count == 1 next_update = dt_util.utcnow() + timedelta(seconds=200) async_fire_time_changed(hass, next_update) await hass.async_block_till_done() - assert device.update.call_count == 2 + assert fritz().update_devices.call_count == 2 assert fritz().login.call_count == 2