Skip to content

Commit 00feb9e

Browse files
authored
Merge pull request #9 from StephanU/bugfix/reading_energy_named_fields
Fixed faulty values for total energy metrics.
2 parents 3279918 + 29f9854 commit 00feb9e

File tree

5 files changed

+56
-70
lines changed

5 files changed

+56
-70
lines changed

custom_components/talent_monitor/entity.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(
2222
self, coordinator, entity: Entity, entity_suffix: str = ""
2323
):
2424
"""Initialize a TalentMonitor entity."""
25-
super().__init__(coordinator)
25+
CoordinatorEntity.__init__(self,coordinator)
2626

2727
device_id = f"{entity.entity_id}"
2828
device_name = entity.name

custom_components/talent_monitor/pyTalentMonitor/data_provider.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,17 @@ def __init__(self, entity_id: str, name: str) -> None:
7070
"""Initialize the entity."""
7171
self.entity_id = entity_id
7272
self.name = name
73+
self._data = {}
74+
75+
@property
76+
def data(self):
77+
"""Return the data of the entity."""
78+
return self._data
79+
80+
@data.setter
81+
def data(self, data):
82+
"""Set the data of the entity."""
83+
self._data = data
7384

7485
class AuthenticationError(Exception):
7586
"""AuthenticationError when connecting to the Talent API."""

custom_components/talent_monitor/pyTalentMonitor/inverter.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,6 @@ def __init__(
1616
) -> None:
1717
"""Initialize the inverter."""
1818
super().__init__(entity_id, name)
19-
self._data = {}
20-
21-
@property
22-
def data(self):
23-
"""Return the data of the inverter."""
24-
return self._data
25-
26-
@data.setter
27-
def data(self, data):
28-
"""Set the data of the inverter."""
29-
self._data = data
3019

3120

3221
class InverterDataProvider:

custom_components/talent_monitor/pyTalentMonitor/power_station.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,6 @@ def __init__(
1818
) -> None:
1919
"""Initialize the power station."""
2020
super().__init__(entity_id, name)
21-
self._data = {}
22-
23-
@property
24-
def data(self):
25-
"""Return the data of the power station."""
26-
return self._data
27-
28-
@data.setter
29-
def data(self, data):
30-
"""Set the data of the power station."""
31-
self._data = data
3221

3322

3423
class PowerStationDataProvider:
@@ -70,9 +59,6 @@ async def fetch_data(self):
7059
endpoint=f"system/station/getPowerStationByGuid?powerStationGuid={powerStationGuid}&timezone={TIMEZONE}"
7160
)
7261

73-
#dummy_data = json.loads('{"yearBattDischargeEnergy": 0.0, "yearLoadEnergyNamed": "0.00 Wh","enableFitInApp": false,"monthEnergy": 44590.0,"yearBattChargeEnergy": 0.0,"totalEnergy": 301180.0,"peakHour": 0.14634146341463414,"stationType": "1","totalActivePower": 94.4,"gridSidePower": 0.0,"lastDataUpdateTime": "2024-05-26T06:52:41","statusNamed": "Online","yearEnergyNamed": "118.06 kWh","stationTypeNamed": "Household use","yearBattChargeEnergyNamed": "0.00 Wh","timezoneOffset": "+02:00","gridSidePowerNamed": "0.00 W","layoutMeta": "{}","locationLongitude": "[redacted]","dayEnergyNamed": "120.00 Wh","totalPeakPower": 94.4,"totalPeakPowerNamed": "94.40 W","dayEnergy": 120.0,"monthBattChargeEnergyNamed": "0.00 Wh","images": [],"deptId": 101,"timeZone": "Europe/Berlin","yearGridsellEnergyNamed": "0.00 Wh","monthBattDischargeEnergy": 0.0,"monthLoadEnergy": 0.0,"yearEnergy": 118060.0,"yearBattDischargeEnergyNamed": "0.00 Wh","monthLoadEnergyNamed": "0.00 Wh","electricityGain": 0.0,"deptCode": "[redacted]","yearGridbuyEnergyNamed": "0.00 Wh","status": "ready","isFavorite": 0,"totalActivePowerNamed": "94.40 W","monthBattChargeEnergy": 0.0,"gridConnectedType": "1","monthGridbuyEnergyNamed": "0.00 Wh","co2Reduced": "300.28 KG","yearGridsellEnergy": 0.0,"treesPlanted": "0.82","buildDate": "2023-06-17","monthGridsellEnergy": 0.0,"yearLoadEnergy": 0.0,"monthEnergyNamed": "44.59 kWh","installedCapacity": 820.0,"lightingHours": "23.19K","gridConnectedTypeNamed": "Full access to the Internet","powerStationId": "[redacted]","stationName": "priwatt priWall duo","monthGridbuyEnergy": 0.0,"currency": "EUR","monthGridsellEnergyNamed": "0.00 Wh","powerStationGuid": "[redacted]","owner": "User [redacted]","monthBattDischargeEnergyNamed": "0.00 Wh","locationLatitude": "[redacted]","battSidePowerNamed": "0.00 W","userId": "[redacted]","ownerEmail": "[redacted]","totalEnergyNamed": "301.18 kWh","earnings": "0.00","lastDataUpdateTimeOffseted": "2024-05-26T08:52:41","ownerUserId": "[redacted]","installedCapacityNamed": "820.00 Wp","guests": [],"location": "[redacted]","yearGridbuyEnergy": 0.0,"businessType": "1","battSidePower": 0.0}')
74-
#power_station_info["data"] = dummy_data
75-
7662
_LOGGER.debug("Details for powerstation GUID %s: %s", powerStationGuid, json.dumps(power_station_info))
7763
if power_station_info and "data" in power_station_info:
7864
power_station.data = power_station_info["data"]

custom_components/talent_monitor/sensor.py

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from datetime import datetime
33
import logging
44
from custom_components.talent_monitor.entity import TalentMonitorEntity, TalentMonitorInverterEntity
5+
from custom_components.talent_monitor.pyTalentMonitor.data_provider import Entity
56
from custom_components.talent_monitor.pyTalentMonitor.inverter import Inverter
67
from custom_components.talent_monitor.pyTalentMonitor.power_station import PowerStation
78
from 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

Comments
 (0)