Fix D-Link attributes (#86842)

* Fix D-Link attributes

* fix blocking call
This commit is contained in:
Robert Hillis 2023-01-28 22:03:58 -05:00 committed by GitHub
parent 0f6f63da64
commit 799edd90aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 12 deletions

View File

@ -19,9 +19,9 @@ class SmartPlugData:
"""Initialize the data object.""" """Initialize the data object."""
self.smartplug = smartplug self.smartplug = smartplug
self.state: str | None = None self.state: str | None = None
self.temperature: str | None = None self.temperature: str = ""
self.current_consumption = None self.current_consumption: str = ""
self.total_consumption: str | None = None self.total_consumption: str = ""
self.available = False self.available = False
self._n_tried = 0 self._n_tried = 0
self._last_tried: datetime | None = None self._last_tried: datetime | None = None

View File

@ -94,17 +94,22 @@ class SmartPlugSwitch(DLinkEntity, SwitchEntity):
@property @property
def extra_state_attributes(self) -> dict[str, Any]: def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of the device.""" """Return the state attributes of the device."""
attrs: dict[str, Any] = {} try:
if self.data.temperature and self.data.temperature.isnumeric(): temperature = self.hass.config.units.temperature(
attrs[ATTR_TEMPERATURE] = self.hass.config.units.temperature(
int(self.data.temperature), UnitOfTemperature.CELSIUS int(self.data.temperature), UnitOfTemperature.CELSIUS
) )
else: except ValueError:
attrs[ATTR_TEMPERATURE] = None temperature = None
if self.data.total_consumption and self.data.total_consumption.isnumeric():
attrs[ATTR_TOTAL_CONSUMPTION] = float(self.data.total_consumption) try:
else: total_consumption = float(self.data.total_consumption)
attrs[ATTR_TOTAL_CONSUMPTION] = None except ValueError:
total_consumption = None
attrs = {
ATTR_TOTAL_CONSUMPTION: total_consumption,
ATTR_TEMPERATURE: temperature,
}
return attrs return attrs