Skip to content
This repository was archived by the owner on Dec 3, 2022. It is now read-only.

Commit c7ac99c

Browse files
committed
Merge branch 'dev' into main
2 parents 4e6a425 + 06f94af commit c7ac99c

2 files changed

Lines changed: 114 additions & 97 deletions

File tree

custom_components/hildebrandglow_dcc/glow.py

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Classes for interacting with the Glowmarkt API."""
22
import logging
3+
import time
34
from datetime import datetime
4-
from pprint import pprint
55
from typing import Any, Dict, List
66

77
import requests
@@ -49,7 +49,8 @@ def authenticate(cls, app_id: str, username: str, password: str) -> Dict[str, An
4949

5050
if data["valid"]:
5151
return data
52-
pprint(data)
52+
53+
_LOGGER.debug("Invalid data\n%s", data)
5354
raise InvalidAuth
5455

5556
@classmethod
@@ -89,29 +90,26 @@ def retrieve_resources(self) -> List[Dict[str, Any]]:
8990
data = response.json()
9091
return data
9192

92-
def current_usage(self, resource: Dict[str, Any]) -> Dict[str, Any]:
93-
"""Retrieve the current usage for a specified resource."""
94-
# Get today's date
95-
current_time = datetime.now()
96-
current_date = current_time.strftime("%Y-%m-%d")
97-
98-
# Need to pull updated data from DCC first
99-
catchup_url = f"{self.BASE_URL}/resource/{resource}/catchup"
100-
101-
url = (
102-
f"{self.BASE_URL}/resource/{resource}/readings?from="
103-
+ current_date
104-
+ "T00:00:00&to="
105-
+ current_date
106-
+ "T23:59:59&period=P1D&offset=-60&function=sum"
107-
)
93+
def _current_data(
94+
self, resource: Dict[str, Any], url: str, catchup: bool
95+
) -> Dict[str, Any]:
96+
"""Retrieve the current data for a specified resource."""
10897
headers = {"applicationId": self.app_id, "token": self.token}
10998

11099
try:
111-
response = requests.get(catchup_url, headers=headers)
100+
if catchup:
101+
catchup_url = f"{self.BASE_URL}/resource/{resource}/catchup"
102+
response = requests.get(catchup_url, headers=headers)
103+
112104
response = requests.get(url, headers=headers)
113-
except requests.Timeout as _timeout:
114-
raise CannotConnect from _timeout
105+
106+
except requests.Timeout as err:
107+
_LOGGER.warning("Timeout connecting to Glow %s", err)
108+
return None
109+
110+
except requests.RequestException as err:
111+
_LOGGER.warning("Error connecting to Glow %s", err)
112+
return None
115113

116114
if response.status_code != 200:
117115
if response.json()["error"] == "incorrect elements -from in the future":
@@ -128,33 +126,52 @@ def current_usage(self, resource: Dict[str, Any]) -> Dict[str, Any]:
128126
raise InvalidAuth
129127

130128
status = str(response.status_code)
131-
_LOGGER.error("Response Status Code: %s (%s)", status, url)
129+
_LOGGER.error("Glow response status code: %s (%s)", status, url)
130+
return None
132131

133-
data = response.json()
134-
return data
132+
return response.json()
133+
134+
def current_usage(self, resource: Dict[str, Any]) -> Dict[str, Any]:
135+
"""Retrieve the current usage for a specified resource."""
136+
# Get today's date
137+
current_time = datetime.utcnow()
138+
current_date = current_time.strftime("%Y-%m-%d")
139+
if time.daylight and (time.localtime().tm_isdst > 0):
140+
utc_offset = time.altzone
141+
else:
142+
utc_offset = time.timezone
143+
144+
if utc_offset != 0:
145+
utc_offset = int(utc_offset / 60)
146+
utc_str = f"&offset={utc_offset}"
147+
else:
148+
utc_str = ""
149+
150+
# Need to pull updated data from DCC first
151+
152+
url = (
153+
f"{self.BASE_URL}/resource/{resource}/readings?from="
154+
+ current_date
155+
+ "T00:00:00&to="
156+
+ current_date
157+
+ "T23:59:59&period=P1D"
158+
+ utc_str
159+
+ "&function=sum"
160+
)
161+
162+
return self._current_data(resource, url, True)
135163

136164
def current_tariff(self, resource: Dict[str, Any]) -> Dict[str, Any]:
137165
"""Retrieve the current tariff for a specified resource."""
138166
url = f"{self.BASE_URL}/resource/{resource}/tariff"
139-
headers = {"applicationId": self.app_id, "token": self.token}
140167

141-
try:
142-
response = requests.get(url, headers=headers)
143-
except requests.Timeout as _timeout:
144-
raise CannotConnect from _timeout
145-
146-
if response.status_code != 200:
147-
if response.status_code == 401:
148-
raise InvalidAuth
149-
if response.status_code == 404:
150-
_LOGGER.debug("Tariff 404 error - treating as 401: %s", url)
151-
raise InvalidAuth
168+
return self._current_data(resource, url, False)
152169

153-
status = str(response.status_code)
154-
_LOGGER.error("Tariff Response Status Code: %s (%s)", status, url)
170+
def usage_now(self, resource: Dict[str, Any]) -> Dict[str, Any]:
171+
"""Retrieve the usage now specified resource."""
172+
url = f"{self.BASE_URL}/resource/{resource}/current"
155173

156-
data = response.json()
157-
return data
174+
return self._current_data(resource, url, False)
158175

159176

160177
class CannotConnect(exceptions.HomeAssistantError):

0 commit comments

Comments
 (0)