Skip to content

Commit

Permalink
Fix LastUpdatedSensor reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
jannickfahlbusch committed Aug 13, 2024
1 parent c19bf59 commit 9ff7c5f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
10 changes: 7 additions & 3 deletions custom_components/ppc_smgw/ppc_smgw.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import urllib3
from typing import List
import httpx
from datetime import datetime

from .reading import Reading
from .errors import SessionCookieStillPresentError
Expand Down Expand Up @@ -116,11 +117,14 @@ async def get_data(self) -> List[Reading]:

# The SMGW returns the meter values in two rows, one for the consumption and one for the feed-in
# We need to store the timestamp of the first row and use it for the second row
current_timestamp = row.find(id="table_metervalues_col_timestamp")
if current_timestamp is None:
row_timestamp = row.find(id="table_metervalues_col_timestamp")
if row_timestamp is None:
self.logger.debug(f"Timestamp not found, using previous: {current_timestamp}")
current_timestamp = timestamp
else:
timestamp = current_timestamp.string
self.logger.debug(f"Found timestamp: {row_timestamp.string}")
current_timestamp = datetime.strptime(row_timestamp.string, "%Y-%m-%d %H:%M:%S")
timestamp = current_timestamp

readings.append(Reading(
value = row.find(id="table_metervalues_col_wert").string,
Expand Down
3 changes: 2 additions & 1 deletion custom_components/ppc_smgw/reading.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from dataclasses import dataclass
from datetime import datetime

@dataclass
class Reading:
value: str
unit: str
timestamp: str
timestamp: datetime
isvalid: str
name: str
obis: str
6 changes: 3 additions & 3 deletions custom_components/ppc_smgw/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities):
coordinator = hass.data[DOMAIN][config_entry.entry_id]
entities = []
entities = [
LastUpdatedSensor(coordinator, LastUpdatedSensorDescription)
]
available_sensors = None
if hasattr(coordinator, 'ppc_smgw'):
if hasattr(coordinator.ppc_smgw, '_readings'):
Expand All @@ -41,8 +43,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, asyn
entity = PPC_SMGWSensor(coordinator, description)
entities.append(entity)

# Last update sensor
entities.append(LastUpdatedSensor(coordinator, LastUpdatedSensorDescription))

async_add_entities(entities)

Expand Down

0 comments on commit 9ff7c5f

Please sign in to comment.