Skip to content

Commit

Permalink
Add gateway time fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
rianadon committed Jan 7, 2024
1 parent 3e383ce commit 96ddfd8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/tedpy/ted.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Base class for TED energy meters."""
import logging
from datetime import datetime
from typing import Any, List

import httpx
Expand Down Expand Up @@ -51,6 +52,10 @@ def gateway_description(self) -> str:
"""Return the description for the gateway."""
return ""

def gateway_time(self) -> datetime:
"""Return the current time of the gateway."""
raise NotImplementedError()

@property
def system_type(self) -> SystemType:
"""Return the system type of the gateway."""
Expand Down Expand Up @@ -113,6 +118,8 @@ async def _async_fetch_with_retry(self, url: str, **kwargs: Any) -> Any:
def print_to_console(self) -> None:
"""Print all the settings and energy yield values to the console."""
print("Gateway id:", self.gateway_id)
print("Gateway description:", self.gateway_description)
print("Gateway time:", self.gateway_time())
print("Net Energy:", format_energy_yield(self.energy()))
print(" Consumed:", format_energy_yield(self.consumption()))
print(" Produced:", format_energy_yield(self.production()))
Expand Down
11 changes: 11 additions & 0 deletions src/tedpy/ted5000.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Implementation for the TED5000 meter."""
import asyncio
from datetime import datetime
from typing import Any

import httpx
Expand Down Expand Up @@ -46,6 +47,16 @@ def gateway_description(self) -> str:
"GatewayDescription"
]

def gateway_time(self) -> datetime:
data = self.endpoint_data_results["LiveData"]
hour = int(data["GatewayTime"]["Hour"])
minute = int(data["GatewayTime"]["Minute"])
second = int(data["GatewayTime"]["Second"])
month = int(data["GatewayTime"]["Month"])
day = int(data["GatewayTime"]["Day"])
year = int(data["GatewayTime"]["Year"]) + 2000
return datetime(year, month, day, hour, minute, second)

def energy(self) -> EnergyYield:
"""Return energy yield information for the whole system."""
data = self.endpoint_data_results["LiveData"]
Expand Down
8 changes: 8 additions & 0 deletions src/tedpy/ted6000.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Implementation for the TED6000 meter."""
import asyncio
from datetime import datetime
from typing import Any, Dict

import httpx
Expand All @@ -17,6 +18,7 @@
from .ted import TED

ENDPOINT_URL_SETTINGS = "http://{}/api/SystemSettings.xml"
ENDPOINT_URL_RATE = "http://{}/api/Rate.xml"
ENDPOINT_URL_DASHBOARD = "http://{}/api/DashData.xml?T=0&D={}&M=0"
ENDPOINT_URL_MTUDASHBOARD = "http://{}/api/DashData.xml?T=0&D=255&M={}"
ENDPOINT_URL_MTU = "http://{}/api/SystemOverview.xml?T=0&D=0&M=0"
Expand All @@ -31,6 +33,7 @@ def __init__(self, host: str, async_client: httpx.AsyncClient = None) -> None:
super().__init__(host, async_client)

self.endpoint_settings_results: Any = None
self.endpoint_rate_results: Any = None
self.endpoint_mtu_results: Any = None
self.endpoint_spyder_results: Any = None
self.endpoint_dash_results: Dict[int, Any] = dict()
Expand All @@ -40,6 +43,7 @@ async def update(self) -> None:
"""Fetch settings and power data from the endpoints."""
await asyncio.gather(
self._update_endpoint("endpoint_settings_results", ENDPOINT_URL_SETTINGS),
self._update_endpoint("endpoint_rate_results", ENDPOINT_URL_RATE),
)

self._parse_mtus()
Expand Down Expand Up @@ -76,6 +80,10 @@ def gateway_description(self) -> str:
"GatewayDescription"
]

def gateway_time(self) -> datetime:
timestamp = int(self.endpoint_rate_results["Rate"]["Time"])
return datetime.fromtimestamp(timestamp)

@property
def polling_delay(self) -> int:
"""Return the delay between successive polls of MTU data."""
Expand Down

0 comments on commit 96ddfd8

Please sign in to comment.