diff --git a/homeassistant/components/goodwe/sensor.py b/homeassistant/components/goodwe/sensor.py index abb29ef080f..2fbf56624cc 100644 --- a/homeassistant/components/goodwe/sensor.py +++ b/homeassistant/components/goodwe/sensor.py @@ -3,7 +3,8 @@ from __future__ import annotations from collections.abc import Callable from dataclasses import dataclass -from datetime import timedelta +from datetime import date, datetime, timedelta +from decimal import Decimal import logging from typing import Any, cast @@ -30,6 +31,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity import DeviceInfo from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.event import async_track_point_in_time +from homeassistant.helpers.typing import StateType from homeassistant.helpers.update_coordinator import ( CoordinatorEntity, DataUpdateCoordinator, @@ -131,6 +133,9 @@ DIAG_SENSOR = GoodweSensorEntityDescription( key="_", state_class=SensorStateClass.MEASUREMENT, ) +TEXT_SENSOR = GoodweSensorEntityDescription( + key="text", +) async def async_setup_entry( @@ -172,19 +177,24 @@ class InverterSensor(CoordinatorEntity, SensorEntity): self._attr_entity_category = ( EntityCategory.DIAGNOSTIC if sensor.id_ not in _MAIN_SENSORS else None ) - self.entity_description = _DESCRIPTIONS.get(sensor.unit, DIAG_SENSOR) - if not self.entity_description.native_unit_of_measurement: - self._attr_native_unit_of_measurement = sensor.unit + try: + self.entity_description = _DESCRIPTIONS[sensor.unit] + except KeyError: + if "Enum" in type(sensor).__name__ or sensor.id_ == "timestamp": + self.entity_description = TEXT_SENSOR + else: + self.entity_description = DIAG_SENSOR + self._attr_native_unit_of_measurement = sensor.unit self._attr_icon = _ICONS.get(sensor.kind) # Set the inverter SoC as main device battery sensor if sensor.id_ == BATTERY_SOC: self._attr_device_class = SensorDeviceClass.BATTERY self._sensor = sensor self._previous_value = None - self._stop_reset = None + self._stop_reset: Callable[[], None] | None = None @property - def native_value(self): + def native_value(self) -> StateType | date | datetime | Decimal: """Return the value reported by the sensor.""" value = cast(GoodweSensorEntityDescription, self.entity_description).value( self._previous_value, @@ -221,7 +231,7 @@ class InverterSensor(CoordinatorEntity, SensorEntity): self.hass, self.async_reset, next_midnight ) - async def async_added_to_hass(self): + async def async_added_to_hass(self) -> None: """Schedule reset task at midnight.""" if self._sensor.id_ in DAILY_RESET: next_midnight = dt_util.start_of_local_day( @@ -232,7 +242,7 @@ class InverterSensor(CoordinatorEntity, SensorEntity): ) await super().async_added_to_hass() - async def async_will_remove_from_hass(self): + async def async_will_remove_from_hass(self) -> None: """Remove reset task at midnight.""" if self._sensor.id_ in DAILY_RESET and self._stop_reset is not None: self._stop_reset()