22from datetime import datetime
33import logging
44from custom_components .talent_monitor .entity import TalentMonitorEntity , TalentMonitorInverterEntity
5+ from custom_components .talent_monitor .pyTalentMonitor .data_provider import Entity
56from custom_components .talent_monitor .pyTalentMonitor .inverter import Inverter
67from custom_components .talent_monitor .pyTalentMonitor .power_station import PowerStation
78from homeassistant .components .sensor import SensorDeviceClass
@@ -104,48 +105,70 @@ async def async_setup_entry(hass, entry, async_add_devices):
104105 ]
105106 )
106107
107- class TalentMonitorPowerStationSensor (TalentMonitorEntity , SensorEntity ):
108- """TalentMonitor PowerStation Sensor class."""
108+
109+ class TalentMonitorSensor (SensorEntity ):
110+ """TalentMonitor Sensor class."""
109111
110112 def __init__ (
111113 self ,
112- coordinator ,
113- power_station : PowerStation ,
114- sensorEntityDescription : SensorEntityDescription ,
114+ entity : Entity ,
115115 ):
116- """Initialize a TalentMonitor PowerStation sensor."""
117- super ().__init__ (
118- coordinator ,
119- power_station ,
120- sensorEntityDescription .key
121- )
122-
123- self ._power_station = power_station
124- self .entity_description = sensorEntityDescription
116+ """Initialize a TalentMonitor sensor."""
117+ self ._entity = entity
125118
126119 @property
127120 def native_value (self ):
128121 """Return the state of the sensor."""
129122 if (self .entity_description .key == "lastDataUpdateTime" ):
130- return datetime .fromisoformat (self ._power_station .data [self .entity_description .key ])
123+ return datetime .fromisoformat (self ._entity .data [self .entity_description .key ])
131124 else :
132- return self ._power_station .data [self .entity_description .key ]
125+ key_for_value_with_unit = self .entity_description .key + "Named"
126+
127+ if (key_for_value_with_unit in self ._entity .data and self ._entity .data [key_for_value_with_unit ]):
128+ value_split = self ._entity .data [key_for_value_with_unit ].split (" " )
129+
130+ if (value_split and len (value_split ) == 2 ):
131+ value = value_split [0 ]
132+ return value
133+ else :
134+ return self ._entity .data [self .entity_description .key ]
135+ else :
136+ return self ._entity .data [self .entity_description .key ]
133137
134138 @property
135139 def native_unit_of_measurement (self ) -> str | None :
136140 """Return the unit of measurement."""
137141 key_for_value_with_unit = self .entity_description .key + "Named"
138142
139- if (key_for_value_with_unit in self ._power_station .data and self ._power_station .data [key_for_value_with_unit ]):
140- value_split = self ._power_station .data [key_for_value_with_unit ].split (" " )
143+ if (key_for_value_with_unit in self ._entity .data and self ._entity .data [key_for_value_with_unit ]):
144+ value_split = self ._entity .data [key_for_value_with_unit ].split (" " )
141145 if (value_split and len (value_split ) == 2 ):
142146 unit = value_split [1 ]
143147 return SENSOR_UNIT_MAPPING [unit ]
144148
145149 return None
146150
151+ class TalentMonitorPowerStationSensor (TalentMonitorEntity , TalentMonitorSensor ):
152+ """TalentMonitor PowerStation Sensor class."""
147153
148- class TalentMonitorInverterSensor (TalentMonitorInverterEntity , SensorEntity ):
154+ def __init__ (
155+ self ,
156+ coordinator ,
157+ power_station : PowerStation ,
158+ sensorEntityDescription : SensorEntityDescription ,
159+ ):
160+ """Initialize a TalentMonitor PowerStation sensor."""
161+ TalentMonitorEntity .__init__ (self ,
162+ coordinator ,
163+ power_station ,
164+ sensorEntityDescription .key
165+ )
166+ TalentMonitorSensor .__init__ (self ,power_station )
167+
168+ self .entity_description = sensorEntityDescription
169+
170+
171+ class TalentMonitorInverterSensor (TalentMonitorInverterEntity , TalentMonitorSensor ):
149172 """TalentMonitor Inverter Sensor class."""
150173
151174 def __init__ (
@@ -155,36 +178,13 @@ def __init__(
155178 sensorEntityDescription : SensorEntityDescription ,
156179 ):
157180 """Initialize a TalentMonitor Inverter sensor."""
158- super () .__init__ (
181+ TalentMonitorInverterEntity .__init__ (self ,
159182 coordinator ,
160183 inverter ,
161184 sensorEntityDescription .key
162185 )
186+ TalentMonitorSensor .__init__ (self , inverter )
163187
164- self ._inverter = inverter
165188 self .entity_description = sensorEntityDescription
166189
167- @property
168- def native_value (self ):
169- """Return the state of the sensor."""
170- if (self .entity_description .key == "lastDataUpdateTime" ):
171- return datetime .fromisoformat (self ._inverter .data [self .entity_description .key ])
172- else :
173- return self ._inverter .data [self .entity_description .key ]
174-
175- @property
176- def native_unit_of_measurement (self ) -> str | None :
177- """Return the unit of measurement."""
178- _LOGGER .debug ('native_unit_of_measurement for %s' , self .entity_description .key )
179- key_for_value_with_unit = self .entity_description .key + "Named"
180-
181- if (key_for_value_with_unit in self ._inverter .data and self ._inverter .data [key_for_value_with_unit ]):
182- value_split = self ._inverter .data [key_for_value_with_unit ].split (" " )
183-
184- _LOGGER .debug ('native_unit_of_measurement for %s' , self ._inverter .data [key_for_value_with_unit ])
185- if (value_split and len (value_split ) == 2 ):
186- unit = value_split [1 ]
187- return SENSOR_UNIT_MAPPING [unit ]
188-
189- return None
190190
0 commit comments