Skip to content

Commit

Permalink
Merge pull request #12 from StephanU/bugfix/update_read_data
Browse files Browse the repository at this point in the history
Fixed reading/updating the data.
  • Loading branch information
StephanU authored Jun 13, 2024
2 parents 00feb9e + 638a30f commit 7d53d27
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
2 changes: 0 additions & 2 deletions .devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
"editor.tabSize": 4,
"python.pythonPath": "/usr/bin/python3",
"python.analysis.autoSearchPaths": false,
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"editor.formatOnPaste": false,
Expand Down
14 changes: 7 additions & 7 deletions custom_components/talent_monitor/pyTalentMonitor/inverter.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ async def fetch_data(self):
if data and "rows" in data:
for index, inverter_data in enumerate(data["rows"], start=1):
if "deviceGuid" in inverter_data:
deviceGuid = inverter_data["deviceGuid"]
device_guid = inverter_data["deviceGuid"]
inverter_name = "Inverter" #TODO get a better name from inverter_data

_LOGGER.debug("Data for inverter GUID %s: %s", deviceGuid, json.dumps(inverter_data))
_LOGGER.debug("Data for inverter GUID %s: %s", device_guid, json.dumps(inverter_data))

if deviceGuid not in self._inverters:
self._inverters["deviceGuid"] = Inverter(deviceGuid, inverter_name + " " + str(index))
if device_guid not in self._inverters:
self._inverters[device_guid] = Inverter(device_guid, inverter_name + " " + str(index))

inverter = self._inverters["deviceGuid"]
inverter = self._inverters[device_guid]

inverter_info = await self._data_provider.get_data(
endpoint=f"tools/device/selectDeviceInverterInfo?deviceGuid={deviceGuid}"
endpoint=f"tools/device/selectDeviceInverterInfo?deviceGuid={device_guid}"
)

_LOGGER.debug("Details for inverter GUID %s: %s", deviceGuid, json.dumps(inverter_info))
_LOGGER.debug("Details for inverter GUID %s: %s", device_guid, json.dumps(inverter_info))
if inverter_info and "data" in inverter_info:
inverter.data = inverter_info["data"]
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,20 @@ async def fetch_data(self):
if data and "rows" in data:
for power_station_data in data["rows"]:
if "powerStationGuid" in power_station_data:
powerStationGuid = power_station_data["powerStationGuid"]
powerStationName = power_station_data["stationName"]
power_station_guid = power_station_data["powerStationGuid"]
power_station_name = power_station_data["stationName"]

_LOGGER.debug("Data for powerstation GUID %s: %s", powerStationGuid, json.dumps(power_station_data))
_LOGGER.debug("Data for powerstation GUID %s: %s", power_station_guid, json.dumps(power_station_data))

if powerStationGuid not in self._power_stations:
self._power_stations["powerStationGuid"] = PowerStation(powerStationGuid, powerStationName)
if power_station_guid not in self._power_stations:
self._power_stations[power_station_guid] = PowerStation(power_station_guid, power_station_name)

power_station = self._power_stations["powerStationGuid"]
power_station = self._power_stations[power_station_guid]

power_station_info = await self._data_provider.get_data(
endpoint=f"system/station/getPowerStationByGuid?powerStationGuid={powerStationGuid}&timezone={TIMEZONE}"
endpoint=f"system/station/getPowerStationByGuid?powerStationGuid={power_station_guid}&timezone={TIMEZONE}"
)

_LOGGER.debug("Details for powerstation GUID %s: %s", powerStationGuid, json.dumps(power_station_info))
_LOGGER.debug("Details for powerstation GUID %s: %s", power_station_guid, json.dumps(power_station_info))
if power_station_info and "data" in power_station_info:
power_station.data = power_station_info["data"]
15 changes: 11 additions & 4 deletions custom_components/talent_monitor/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.components.sensor import SensorStateClass
from homeassistant.const import UnitOfEnergy
from homeassistant.const import UnitOfTemperature
from homeassistant.const import UnitOfPower
from homeassistant.core import callback

from .const import DOMAIN

Expand Down Expand Up @@ -51,14 +53,14 @@
SensorEntityDescription(
key="lastDataUpdateTime",
translation_key="talentmonitor_powerstation_last_data_update_time",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.DATE,
),
SensorEntityDescription(
key="inverterTemp",
translation_key="talentmonitor_inverter_temp",
state_class=SensorStateClass.MEASUREMENT,
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=UnitOfTemperature.CELSIUS
),
)

Expand Down Expand Up @@ -116,9 +118,15 @@ def __init__(
"""Initialize a TalentMonitor sensor."""
self._entity = entity

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
self.async_write_ha_state()

@property
def native_value(self):
"""Return the state of the sensor."""

if (self.entity_description.key == "lastDataUpdateTime"):
return datetime.fromisoformat(self._entity.data[self.entity_description.key])
else:
Expand All @@ -145,8 +153,7 @@ def native_unit_of_measurement(self) -> str | None:
if (value_split and len(value_split) == 2):
unit = value_split[1]
return SENSOR_UNIT_MAPPING[unit]

return None
return self.entity_description.native_unit_of_measurement

class TalentMonitorPowerStationSensor(TalentMonitorEntity, TalentMonitorSensor):
"""TalentMonitor PowerStation Sensor class."""
Expand All @@ -163,7 +170,7 @@ def __init__(
power_station,
sensorEntityDescription.key
)
TalentMonitorSensor.__init__(self,power_station)
TalentMonitorSensor.__init__(self, power_station)

self.entity_description = sensorEntityDescription

Expand Down

0 comments on commit 7d53d27

Please sign in to comment.