Fix crash when AVM FRITZ!SmartHome devices are unreachable (#53809)

pull/53867/head
Michael 2021-08-02 16:13:54 +02:00 committed by GitHub
parent 3296772bd3
commit 4241c4ca5b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 13 additions and 16 deletions

View File

@ -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."""

View File

@ -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]

View File

@ -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."""

View File

@ -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:

View File

@ -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."""

View File

@ -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):