Fix goodwe text(enum) sensors device class (#87914)

This commit is contained in:
mletenay 2023-02-18 13:39:38 +01:00 committed by GitHub
parent 1279868bf5
commit 728f0d5b3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

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