11"""Sensor platform for TalentMonitor."""
2+ from datetime import datetime
3+ import logging
24from custom_components .talent_monitor .entity import TalentMonitorEntity
35from custom_components .talent_monitor .pyTalentMonitor .power_station import PowerStation
46from homeassistant .components .sensor import SensorDeviceClass
1012
1113from .const import DOMAIN
1214
15+
16+ _LOGGER : logging .Logger = logging .getLogger (__name__ )
17+
1318SENSOR_TYPES : tuple [SensorEntityDescription , ...] = (
1419 SensorEntityDescription (
1520 key = "totalActivePower" ,
1621 translation_key = "talentmonitor_powerstation_totalActivePower" ,
1722 state_class = SensorStateClass .MEASUREMENT ,
1823 device_class = SensorDeviceClass .POWER ,
19- )
24+ ),
25+ SensorEntityDescription (
26+ key = "dayEnergy" ,
27+ translation_key = "talentmonitor_powerstation_dayEnergy" ,
28+ state_class = SensorStateClass .TOTAL_INCREASING ,
29+ device_class = SensorDeviceClass .ENERGY ,
30+ ),
31+ SensorEntityDescription (
32+ key = "monthEnergy" ,
33+ translation_key = "talentmonitor_powerstation_monthEnergy" ,
34+ state_class = SensorStateClass .TOTAL_INCREASING ,
35+ device_class = SensorDeviceClass .ENERGY ,
36+ ),
37+ SensorEntityDescription (
38+ key = "yearEnergy" ,
39+ translation_key = "talentmonitor_powerstation_yearEnergy" ,
40+ state_class = SensorStateClass .TOTAL_INCREASING ,
41+ device_class = SensorDeviceClass .ENERGY ,
42+ ),
43+ SensorEntityDescription (
44+ key = "lastDataUpdateTime" ,
45+ translation_key = "talentmonitor_powerstation_lastDataUpdateTime" ,
46+ state_class = SensorStateClass .MEASUREMENT ,
47+ device_class = SensorDeviceClass .DATE ,
48+ ),
2049)
2150
2251SENSORS = {desc .key : desc for desc in SENSOR_TYPES }
@@ -35,6 +64,7 @@ async def async_setup_entry(hass, entry, async_add_devices):
3564 power_stations : list [PowerStation ] = coordinator .api .get_power_stations ()
3665 for power_station in power_stations :
3766 for index , value in enumerate (power_station .data ):
67+ _LOGGER .debug ("Iterate data for powerstation %s" , value )
3868 if value and value in SENSORS :
3969 async_add_devices (
4070 [
@@ -59,6 +89,7 @@ def __init__(
5989 super ().__init__ (
6090 coordinator ,
6191 power_station ,
92+ sensorEntityDescription .key
6293 )
6394
6495 self ._power_station = power_station
@@ -67,5 +98,21 @@ def __init__(
6798 @property
6899 def native_value (self ):
69100 """Return the state of the sensor."""
70- return self ._power_station .data [self .entity_description .key ]
101+ if (self .entity_description .key == "lastDataUpdateTime" ):
102+ return datetime .fromisoformat (self ._power_station .data [self .entity_description .key ])
103+ else :
104+ return self ._power_station .data [self .entity_description .key ]
105+
106+ @property
107+ def native_unit_of_measurement (self ) -> str | None :
108+ """Return the unit of measurement."""
109+ key_for_value_with_unit = self .entity_description .key + "Named"
110+
111+ if (key_for_value_with_unit in self ._power_station .data and self ._power_station .data [key_for_value_with_unit ]):
112+ value_split = self ._power_station .data [key_for_value_with_unit ].split (" " )
113+ if (value_split and len (value_split ) == 2 ):
114+ unit = value_split [1 ]
115+ return SENSOR_UNIT_MAPPING [unit ]
116+
117+ return None
71118
0 commit comments