diff --git a/homeassistant/components/dlink/data.py b/homeassistant/components/dlink/data.py index c9e3b20a0bf8..b93cd2191669 100644 --- a/homeassistant/components/dlink/data.py +++ b/homeassistant/components/dlink/data.py @@ -19,9 +19,9 @@ class SmartPlugData: """Initialize the data object.""" self.smartplug = smartplug self.state: str | None = None - self.temperature: str | None = None - self.current_consumption = None - self.total_consumption: str | None = None + self.temperature: str = "" + self.current_consumption: str = "" + self.total_consumption: str = "" self.available = False self._n_tried = 0 self._last_tried: datetime | None = None diff --git a/homeassistant/components/dlink/switch.py b/homeassistant/components/dlink/switch.py index 9827c1c13a1b..e6b9a4c7883e 100644 --- a/homeassistant/components/dlink/switch.py +++ b/homeassistant/components/dlink/switch.py @@ -94,17 +94,22 @@ class SmartPlugSwitch(DLinkEntity, SwitchEntity): @property def extra_state_attributes(self) -> dict[str, Any]: """Return the state attributes of the device.""" - attrs: dict[str, Any] = {} - if self.data.temperature and self.data.temperature.isnumeric(): - attrs[ATTR_TEMPERATURE] = self.hass.config.units.temperature( + try: + temperature = self.hass.config.units.temperature( int(self.data.temperature), UnitOfTemperature.CELSIUS ) - else: - attrs[ATTR_TEMPERATURE] = None - if self.data.total_consumption and self.data.total_consumption.isnumeric(): - attrs[ATTR_TOTAL_CONSUMPTION] = float(self.data.total_consumption) - else: - attrs[ATTR_TOTAL_CONSUMPTION] = None + except ValueError: + temperature = None + + try: + total_consumption = float(self.data.total_consumption) + except ValueError: + total_consumption = None + + attrs = { + ATTR_TOTAL_CONSUMPTION: total_consumption, + ATTR_TEMPERATURE: temperature, + } return attrs