-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed broken days value for usage sensor, updated README and tidied u…
…p a bit
- Loading branch information
1 parent
2a168f7
commit 90d1bb0
Showing
5 changed files
with
196 additions
and
237 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
"""Greenely API""" | ||
import logging | ||
from datetime import datetime, timedelta | ||
import requests | ||
import json | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
class GreenelyApi: | ||
def __init__(self, email, password): | ||
self._jwt = '' | ||
self._url_check_auth = 'https://api2.greenely.com/v1/checkauth' | ||
self._url_login = 'https://api2.greenely.com/v1/login' | ||
self._url_retail = 'https://api2.greenely.com/v2/retail/overview' | ||
self._url_data = 'https://api2.greenely.com/v3/data/' | ||
self._url_sold = 'https://api2.greenely.com/v1/facilities/' | ||
self._url_spot_price = 'https://api2.greenely.com/v1/facilities/' | ||
self._url_facilities = 'https://api2.greenely.com/v1/facilities/primary?includes=retail_state&includes=consumption_limits&includes=parameters' | ||
self._headers = {'Accept-Language':'sv-SE', | ||
'User-Agent':'Android 2 111', | ||
'Content-Type': 'application/json; charset=utf-8', | ||
'Authorization':self._jwt} | ||
self._email = email | ||
self._password = password | ||
self._facility_id = '' | ||
|
||
def get_price_data(self): | ||
response = requests.get(self._url_retail, headers = self._headers) | ||
data = {} | ||
if response.status_code == requests.codes.ok: | ||
data = response.json() | ||
return data['data'] | ||
else: | ||
_LOGGER.error('Failed to get price data, %s', response.text) | ||
return data | ||
|
||
def get_spot_price(self): | ||
today = datetime.today() | ||
yesterday = today - timedelta(days = 1) | ||
tomorrow = today + timedelta(days = 2) | ||
start = "?from=" + str(yesterday.year) + "-" + yesterday.strftime("%m") + "-" + yesterday.strftime("%d") | ||
end = "&to=" + str(tomorrow.year) + "-" + tomorrow.strftime("%m") + "-" + tomorrow.strftime("%d") | ||
url = self._url_spot_price + self._facility_id + "/spot-price" + start + end + "&resolution=hourly" | ||
response = requests.get(url, headers = self._headers) | ||
data = {} | ||
if response.status_code == requests.codes.ok: | ||
data = response.json() | ||
return data | ||
else: | ||
_LOGGER.error('Failed to get price data, %s', response.text) | ||
return data | ||
|
||
def get_usage(self, year, month, day): | ||
url = self._url_data + year + "/" + month + "/" + day + "/usage" | ||
response = requests.get(url, headers = self._headers) | ||
data = {} | ||
if response.status_code == requests.codes.ok: | ||
data = response.json() | ||
return data['data'] | ||
else: | ||
_LOGGER.error('Failed to fetch usage data for %s/%s/%s, %s', year, month, day, response.text) | ||
return data | ||
|
||
def get_facility_id(self): | ||
result = requests.get(self._url_facilities, headers = self._headers) | ||
if result.status_code == requests.codes.ok: | ||
_LOGGER.debug('jwt is valid!') | ||
data = result.json() | ||
self._facility_id = str(data['data']['parameters']['facility_id']) | ||
else: | ||
_LOGGER.error('Failed to fetch facility id %s', result.text) | ||
|
||
def check_auth(self): | ||
"""Check to see if our jwt is valid.""" | ||
result = requests.get(self._url_check_auth, headers = self._headers) | ||
if result.status_code == requests.codes.ok: | ||
_LOGGER.debug('jwt is valid!') | ||
return True | ||
else: | ||
if self.login() == False: | ||
_LOGGER.debug(result.text) | ||
return False | ||
return True | ||
|
||
def login(self): | ||
"""Login to the Greenely API.""" | ||
result = False | ||
loginInfo = {'email':self._email, | ||
'password':self._password} | ||
loginResult = requests.post(self._url_login, headers = self._headers, data = json.dumps(loginInfo)) | ||
if loginResult.status_code == requests.codes.ok: | ||
jsonResult = loginResult.json() | ||
self._jwt = "JWT " + jsonResult['jwt'] | ||
self._headers['Authorization'] = self._jwt | ||
_LOGGER.debug('Successfully logged in and updated jwt') | ||
self.get_facility_id() | ||
result = True | ||
else: | ||
_LOGGER.error(loginResult.text) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Constants for the greenely sensors""" | ||
|
||
SENSOR_USAGE_NAME = 'Greenely Usage' | ||
SENSOR_SOLD_NAME = 'Greenely Sold' | ||
SENSOR_PRICES_NAME = 'Greenely Prices' | ||
|
||
DOMAIN = 'greenely' | ||
|
||
CONF_PRICES = 'prices' | ||
CONF_USAGE = 'usage' | ||
CONF_SOLD = 'sold' | ||
CONF_SOLD_MEASURE = 'sold_measure' | ||
CONF_SOLD_DAILY = 'sold_daily' | ||
CONF_USAGE_DAYS = 'usage_days' | ||
CONF_SHOW_HOURLY = 'show_hourly' | ||
CONF_DATE_FORMAT = 'date_format' | ||
CONF_TIME_FORMAT = 'time_format' | ||
CONF_HOURLY_OFFSET_DAYS = 'hourly_offset_days' | ||
|
||
MONITORED_CONDITIONS_DEFAULT = [ | ||
'is_retail_customer', | ||
'current_price', | ||
'referral_discount_in_kr', | ||
'has_unpaid_invoices', | ||
'yearly_savings_in_kr', | ||
'timezone', | ||
'retail_termination_date', | ||
'current_day', | ||
'next_day', | ||
'current_month' | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.