diff --git a/homeassistant/components/fritzbox/__init__.py b/homeassistant/components/fritzbox/__init__.py index 087faeb2be9..cef325a61f3 100644 --- a/homeassistant/components/fritzbox/__init__.py +++ b/homeassistant/components/fritzbox/__init__.py @@ -143,6 +143,11 @@ class FritzBoxEntity(CoordinatorEntity): self._device_class = entity_info[ATTR_DEVICE_CLASS] self._attr_state_class = entity_info[ATTR_STATE_CLASS] + @property + def available(self) -> bool: + """Return if entity is available.""" + return super().available and self.device.present + @property def device(self) -> FritzhomeDevice: """Return device object from coordinator.""" diff --git a/homeassistant/components/fritzbox/binary_sensor.py b/homeassistant/components/fritzbox/binary_sensor.py index f6dbaed97cf..5514408cb3c 100644 --- a/homeassistant/components/fritzbox/binary_sensor.py +++ b/homeassistant/components/fritzbox/binary_sensor.py @@ -54,6 +54,4 @@ class FritzboxBinarySensor(FritzBoxEntity, BinarySensorEntity): @property def is_on(self) -> bool: """Return true if sensor is on.""" - if not self.device.present: - return False return self.device.alert_state # type: ignore [no-any-return] diff --git a/homeassistant/components/fritzbox/climate.py b/homeassistant/components/fritzbox/climate.py index 0551c5e0455..4baa1b3b81a 100644 --- a/homeassistant/components/fritzbox/climate.py +++ b/homeassistant/components/fritzbox/climate.py @@ -93,11 +93,6 @@ class FritzboxThermostat(FritzBoxEntity, ClimateEntity): """Return the list of supported features.""" return SUPPORT_FLAGS - @property - def available(self) -> bool: - """Return if thermostat is available.""" - return self.device.present # type: ignore [no-any-return] - @property def temperature_unit(self) -> str: """Return the unit of measurement that is used.""" diff --git a/homeassistant/components/fritzbox/sensor.py b/homeassistant/components/fritzbox/sensor.py index d325e592faf..9d78afca4de 100644 --- a/homeassistant/components/fritzbox/sensor.py +++ b/homeassistant/components/fritzbox/sensor.py @@ -121,7 +121,9 @@ class FritzBoxPowerSensor(FritzBoxEntity, SensorEntity): @property def state(self) -> float | None: """Return the state of the sensor.""" - return self.device.power / 1000 # type: ignore [no-any-return] + if power := self.device.power: + return power / 1000 # type: ignore [no-any-return] + return 0.0 class FritzBoxEnergySensor(FritzBoxEntity, SensorEntity): @@ -130,7 +132,9 @@ class FritzBoxEnergySensor(FritzBoxEntity, SensorEntity): @property def state(self) -> float | None: """Return the state of the sensor.""" - return (self.device.energy or 0.0) / 1000 + if energy := self.device.energy: + return energy / 1000 # type: ignore [no-any-return] + return 0.0 @property def last_reset(self) -> datetime: diff --git a/homeassistant/components/fritzbox/switch.py b/homeassistant/components/fritzbox/switch.py index 62d1cb1ecf8..133db92feda 100644 --- a/homeassistant/components/fritzbox/switch.py +++ b/homeassistant/components/fritzbox/switch.py @@ -56,11 +56,6 @@ async def async_setup_entry( class FritzboxSwitch(FritzBoxEntity, SwitchEntity): """The switch class for FRITZ!SmartHome switches.""" - @property - def available(self) -> bool: - """Return if switch is available.""" - return self.device.present # type: ignore [no-any-return] - @property def is_on(self) -> bool: """Return true if the switch is on.""" diff --git a/tests/components/fritzbox/test_binary_sensor.py b/tests/components/fritzbox/test_binary_sensor.py index f4e32fbe3df..cb76109e0ff 100644 --- a/tests/components/fritzbox/test_binary_sensor.py +++ b/tests/components/fritzbox/test_binary_sensor.py @@ -14,8 +14,8 @@ from homeassistant.const import ( ATTR_UNIT_OF_MEASUREMENT, CONF_DEVICES, PERCENTAGE, - STATE_OFF, STATE_ON, + STATE_UNAVAILABLE, ) from homeassistant.core import HomeAssistant import homeassistant.util.dt as dt_util @@ -60,7 +60,7 @@ async def test_is_off(hass: HomeAssistant, fritz: Mock): state = hass.states.get(ENTITY_ID) assert state - assert state.state == STATE_OFF + assert state.state == STATE_UNAVAILABLE async def test_update(hass: HomeAssistant, fritz: Mock):