From 7ece35cd6f36f73be43da547df9bced4b5269560 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Thu, 23 Sep 2021 22:29:12 +0200 Subject: [PATCH] Assume Fritz!Smarthome device as unavailable (#56542) --- homeassistant/components/fritzbox/__init__.py | 12 ++++++++++++ tests/components/fritzbox/__init__.py | 1 + tests/components/fritzbox/test_switch.py | 16 ++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/homeassistant/components/fritzbox/__init__.py b/homeassistant/components/fritzbox/__init__.py index d9226f36c87..8d354f655f6 100644 --- a/homeassistant/components/fritzbox/__init__.py +++ b/homeassistant/components/fritzbox/__init__.py @@ -68,6 +68,18 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: data = {} for device in devices: device.update() + + # assume device as unavailable, see #55799 + if ( + device.has_powermeter + and device.present + and hasattr(device, "voltage") + and device.voltage <= 0 + and device.power <= 0 + and device.energy <= 0 + ): + device.present = False + data[device.ain] = device return data diff --git a/tests/components/fritzbox/__init__.py b/tests/components/fritzbox/__init__.py index da6bd982d9d..dfa266dc15b 100644 --- a/tests/components/fritzbox/__init__.py +++ b/tests/components/fritzbox/__init__.py @@ -108,6 +108,7 @@ class FritzDeviceSwitchMock(FritzDeviceBaseMock): battery_level = None device_lock = "fake_locked_device" energy = 1234 + voltage = 230 fw_version = "1.2.3" has_alarm = False has_powermeter = True diff --git a/tests/components/fritzbox/test_switch.py b/tests/components/fritzbox/test_switch.py index 27461b2790f..b44a4ffc088 100644 --- a/tests/components/fritzbox/test_switch.py +++ b/tests/components/fritzbox/test_switch.py @@ -26,6 +26,7 @@ from homeassistant.const import ( SERVICE_TURN_OFF, SERVICE_TURN_ON, STATE_ON, + STATE_UNAVAILABLE, TEMP_CELSIUS, ) from homeassistant.core import HomeAssistant @@ -137,3 +138,18 @@ async def test_update_error(hass: HomeAssistant, fritz: Mock): assert device.update.call_count == 2 assert fritz().login.call_count == 2 + + +async def test_assume_device_unavailable(hass: HomeAssistant, fritz: Mock): + """Test assume device as unavailable.""" + device = FritzDeviceSwitchMock() + device.voltage = 0 + device.energy = 0 + device.power = 0 + assert await setup_config_entry( + hass, MOCK_CONFIG[FB_DOMAIN][CONF_DEVICES][0], ENTITY_ID, device, fritz + ) + + state = hass.states.get(ENTITY_ID) + assert state + assert state.state == STATE_UNAVAILABLE