diff --git a/custom_components/talent_monitor/__init__.py b/custom_components/talent_monitor/__init__.py index 2fb5037..992e538 100644 --- a/custom_components/talent_monitor/__init__.py +++ b/custom_components/talent_monitor/__init__.py @@ -5,7 +5,6 @@ """ import asyncio -import json import logging from custom_components.talent_monitor.coordinator import ( diff --git a/custom_components/talent_monitor/pyTalentMonitor/__init__.py b/custom_components/talent_monitor/pyTalentMonitor/__init__.py index 2b3dc0f..086b7c1 100644 --- a/custom_components/talent_monitor/pyTalentMonitor/__init__.py +++ b/custom_components/talent_monitor/pyTalentMonitor/__init__.py @@ -28,16 +28,20 @@ def __init__( def get_power_stations(self) -> list[PowerStation]: + """Return the power stations.""" return self._power_station_data_provider.power_stations async def fetch_data(self): + """Fetch data from the TalentMonitor.""" await self._inverter_data_provider.fetch_data() await self._power_station_data_provider.fetch_data() async def fetch_solar_data(self): + """Fetch the solar data and return it as json.""" await self.fetch_data() async def login(self): + """Log in to the TalentMonitor API.""" await self._data_provider.login() async def main(username: str, password: str): diff --git a/custom_components/talent_monitor/pyTalentMonitor/data_provider.py b/custom_components/talent_monitor/pyTalentMonitor/data_provider.py index f31d26e..fa299aa 100644 --- a/custom_components/talent_monitor/pyTalentMonitor/data_provider.py +++ b/custom_components/talent_monitor/pyTalentMonitor/data_provider.py @@ -1,3 +1,4 @@ +"""Data Provider for accessing the TalentMonitor API.""" import logging import os @@ -8,12 +9,13 @@ BASE_URL = "https://www.talent-monitoring.com/prod-api" -class DataProvider(): - """Data provider accessing the MyGekko API""" +class DataProvider: + """Data provider accessing the TalentMonitor API.""" def __init__( self, username: str, password: str, session: ClientSession ): + """Initialize the data provider.""" self._url = BASE_URL self._username = username or os.environ.get("PYTALENT_USERNAME") self._password = password or os.environ.get("PYTALENT_PASSWORD") @@ -62,12 +64,14 @@ async def get_data(self, endpoint): return None class Entity: - """Base class for TalentMonitor entities""" + """Base class for TalentMonitor entities.""" def __init__(self, entity_id: str, name: str) -> None: + """Initialize the entity.""" self.entity_id = entity_id self.name = name class AuthenticationError(Exception): """AuthenticationError when connecting to the Talent API.""" - pass \ No newline at end of file + + pass diff --git a/custom_components/talent_monitor/pyTalentMonitor/inverter.py b/custom_components/talent_monitor/pyTalentMonitor/inverter.py index 2b3c1a2..fe04ba5 100644 --- a/custom_components/talent_monitor/pyTalentMonitor/inverter.py +++ b/custom_components/talent_monitor/pyTalentMonitor/inverter.py @@ -1,4 +1,4 @@ -"""TalentMonitor Inverter""" +"""TalentMonitor Inverter.""" import json import logging @@ -9,38 +9,47 @@ _LOGGER: logging.Logger = logging.getLogger(__name__) class Inverter(Entity): + """Class for TalentMonitor inverter.""" + def __init__( self, entity_id: str, name: str ) -> None: + """Initialize the inverter.""" super().__init__(entity_id, name) self._data = {} @property def data(self): + """Return the data of the inverter.""" return self._data @data.setter def data(self, data): + """Set the data of the inverter.""" self._data = data -class InverterDataProvider(): +class InverterDataProvider: + """Data provider for inverter.""" + def __init__( self, data_provider: DataProvider ) -> None: + """Initialize the data provider.""" self._data_provider = data_provider self._inverter = {} @property def inverters(self) -> list[Inverter]: - """Returns the inverters read from TalentMonitor""" + """Returns the inverters read from TalentMonitor.""" result: list[Inverter] = [] - for key, data in self._inverter.items(): + for data in self._inverter.values(): result.append(data) return result async def fetch_data(self): + """Fetch the data of the inverter.""" data = await self._data_provider.get_data(endpoint="tools/device/selectDeviceInverter") if data and "rows" in data: for inverter_data in data["rows"]: @@ -49,7 +58,7 @@ async def fetch_data(self): _LOGGER.debug("Data for inverter GUID %s: %s", deviceGuid, json.dumps(inverter_data)) - if not deviceGuid in self._inverters: + if deviceGuid not in self._inverters: self._inverters["deviceGuid"] = Inverter() inverter = self._inverters["deviceGuid"] @@ -60,4 +69,4 @@ async def fetch_data(self): _LOGGER.debug("Details for inverter GUID %s: %s", deviceGuid, json.dumps(inverter_info)) if inverter_info and "data" in inverter_info: - inverter.data = inverter_info["data"] \ No newline at end of file + inverter.data = inverter_info["data"] diff --git a/custom_components/talent_monitor/pyTalentMonitor/power_station.py b/custom_components/talent_monitor/pyTalentMonitor/power_station.py index bb6fa1e..7ebe285 100644 --- a/custom_components/talent_monitor/pyTalentMonitor/power_station.py +++ b/custom_components/talent_monitor/pyTalentMonitor/power_station.py @@ -1,4 +1,4 @@ -"""TalentMonitor PowerStation""" +"""TalentMonitor PowerStation.""" import json import logging @@ -11,38 +11,47 @@ TIMEZONE = "+02:00" class PowerStation(Entity): + """Class for TalentMonitor power station.""" + def __init__( self, entity_id: str, name: str ) -> None: + """Initialize the power station.""" super().__init__(entity_id, name) self._data = {} @property def data(self): + """Return the data of the power station.""" return self._data @data.setter def data(self, data): + """Set the data of the power station.""" self._data = data -class PowerStationDataProvider(): +class PowerStationDataProvider: + """Data provider for power stations.""" + def __init__( self, data_provider: DataProvider ) -> None: + """Initialize the data provider.""" self._data_provider = data_provider self._power_stations = {} @property def power_stations(self) -> list[PowerStation]: - """Returns the power stations read from TalentMonitor""" + """Returns the power stations read from TalentMonitor.""" result: list[PowerStation] = [] - for key, data in self._power_stations.items(): + for data in self._power_stations.values(): result.append(data) return result async def fetch_data(self): + """Fetch the data of the power stations.""" data = await self._data_provider.get_data(endpoint="system/station/list") if data and "rows" in data: for power_station_data in data["rows"]: @@ -52,7 +61,7 @@ async def fetch_data(self): _LOGGER.debug("Data for powerstation GUID %s: %s", powerStationGuid, json.dumps(power_station_data)) - if not powerStationGuid in self._power_stations: + if powerStationGuid not in self._power_stations: self._power_stations["powerStationGuid"] = PowerStation(powerStationGuid, powerStationName) power_station = self._power_stations["powerStationGuid"] @@ -61,6 +70,9 @@ async def fetch_data(self): endpoint=f"system/station/getPowerStationByGuid?powerStationGuid={powerStationGuid}&timezone={TIMEZONE}" ) + 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}') + _LOGGER.debug("Details for powerstation GUID %s: %s", powerStationGuid, json.dumps(power_station_info)) if power_station_info and "data" in power_station_info: - power_station.data = power_station_info["data"] + #power_station.data = power_station_info["data"] + power_station.data = dummy_data diff --git a/custom_components/talent_monitor/sensor.py b/custom_components/talent_monitor/sensor.py index a6b2cce..b783627 100644 --- a/custom_components/talent_monitor/sensor.py +++ b/custom_components/talent_monitor/sensor.py @@ -63,7 +63,7 @@ async def async_setup_entry(hass, entry, async_add_devices): coordinator = hass.data[DOMAIN][entry.entry_id] power_stations: list[PowerStation] = coordinator.api.get_power_stations() for power_station in power_stations: - for index, value in enumerate(power_station.data): + for _, value in enumerate(power_station.data): _LOGGER.debug("Iterate data for powerstation %s", value) if value and value in SENSORS: async_add_devices(