Skip to content

Commit

Permalink
handle_failed_auth fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lee-BS-AMS committed Sep 18, 2021
1 parent d4a8c6f commit 48c6c07
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
15 changes: 11 additions & 4 deletions custom_components/hildebrandglow_dcc/glow.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ def authenticate(cls, app_id: str, username: str, password: str) -> Dict[str, An
pprint(data)
raise InvalidAuth

async def handle_failed_auth(self, config: ConfigEntry, hass: HomeAssistant) -> None:
@staticmethod
async def handle_failed_auth(config: ConfigEntry, hass: HomeAssistant) -> None:
"""Attempt to refresh the current Glow token."""
glow_auth = await hass.async_add_executor_job(
Glow.authenticate,
Expand Down Expand Up @@ -89,8 +90,13 @@ def current_usage(self, resource: Dict[str, Any]) -> Dict[str, Any]:
# Need to pull updated data from DCC first
catchup_url = f"{self.BASE_URL}/resource/{resource}/catchup"

url = f"{self.BASE_URL}/resource/{resource}/readings?from=" + current_date + \
"T00:00:00&to=" + current_date + "T23:59:59&period=P1D&offset=-60&function=sum"
url = (
f"{self.BASE_URL}/resource/{resource}/readings?from="
+ current_date
+ "T00:00:00&to="
+ current_date
+ "T23:59:59&period=P1D&offset=-60&function=sum"
)
headers = {"applicationId": self.app_id, "token": self.token}

try:
Expand All @@ -102,7 +108,8 @@ def current_usage(self, resource: Dict[str, Any]) -> Dict[str, Any]:
if response.status_code != 200:
if response.json()["error"] == "incorrect elements -from in the future":
_LOGGER.info(
"Attempted to load data from the future - expected if the day has just changed")
"Attempted to load data from the future - expected if the day has just changed"
)
elif response.status_code == 401:
raise InvalidAuth
else:
Expand Down
1 change: 1 addition & 0 deletions custom_components/hildebrandglow_dcc/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ async def async_setup_entry(
except InvalidAuth:
try:
await Glow.handle_failed_auth(config, hass)

except InvalidAuth:
return False

Expand Down

0 comments on commit 48c6c07

Please sign in to comment.