Skip to content

Commit

Permalink
Handling no primary facility set
Browse files Browse the repository at this point in the history
  • Loading branch information
linsvensson committed May 12, 2024
1 parent e875ae7 commit 2aebe96
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions custom_components/greenely/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Greenely API"""
import logging
from datetime import datetime, timedelta
import calendar
import requests
import json

Expand All @@ -24,9 +23,9 @@ def __init__(self, email, password, facility_id):

def get_price_data(self):
today = datetime.today()
nextMonth = (today.replace(day=1) + timedelta(days=32)).replace(day=1)
start = "?from=" + str(today.year) + "-" + today.strftime("%m") + "-01"
endOfMonth = calendar.monthrange(today.year, today.month)[1]
end = "&to=" + str(today.year) + "-" + today.strftime("%m") + "-" + str(endOfMonth);
end = "&to=" + str(nextMonth.year) + "-" + nextMonth.strftime("%m") + "-01"
url = self._url_facilities_base + self._facility_id + '/consumption' + start + end + "&resolution=daily&unit=currency&operation=sum"
response = requests.get(url, headers = self._headers)
data = {}
Expand Down Expand Up @@ -68,13 +67,17 @@ def get_usage(self, startDate, endDate, showHourly):
return data

def get_facility_id(self):
result = requests.get(self._url_facilities_base + 'primary?includes=retail_state&includes=consumption_limits&includes=parameters', headers = self._headers)
result = requests.get(self._url_facilities_base, headers = self._headers)
if result.status_code == requests.codes.ok:
data = result.json()
facility = next((f for f in data if f['is_primary'] == True), None)
if facility == None:
_LOGGER.debug('Found no primary facility, using the first one in the list!')
facility = data[0]
self._facility_id = str(data['data']['parameters']['facility_id'])
_LOGGER.debug('Fetched facility id %s', self._facility_id)
else:
_LOGGER.error('Failed to fetch facility id %s', result.text)
_LOGGER.error('Failed to fetch facility id %s', result.reason)

def check_auth(self):
"""Check to see if our jwt is valid."""
Expand Down
2 changes: 1 addition & 1 deletion custom_components/greenely/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"domain": "greenely",
"name": "Greenely Sensors",
"version": "1.0.6",
"version": "1.0.7",
"documentation": "https://github.com/linsvensson/sensor.greenely",
"issue_tracker": "https://github.com/linsvensson/sensor.greenely/issues",
"codeowners": ["@linsvensson"],
Expand Down

0 comments on commit 2aebe96

Please sign in to comment.