mirror of
https://github.com/CCOSTAN/Home-AssistantConfig.git
synced 2025-11-06 17:51:36 +00:00
Initial Configuration Push
This commit is contained in:
1
deps/forecastio/__init__.py
vendored
Normal file
1
deps/forecastio/__init__.py
vendored
Normal file
@@ -0,0 +1 @@
|
||||
from forecastio.api import load_forecast, manual
|
||||
BIN
deps/forecastio/__pycache__/__init__.cpython-34.pyc
vendored
Normal file
BIN
deps/forecastio/__pycache__/__init__.cpython-34.pyc
vendored
Normal file
Binary file not shown.
BIN
deps/forecastio/__pycache__/api.cpython-34.pyc
vendored
Normal file
BIN
deps/forecastio/__pycache__/api.cpython-34.pyc
vendored
Normal file
Binary file not shown.
BIN
deps/forecastio/__pycache__/models.cpython-34.pyc
vendored
Normal file
BIN
deps/forecastio/__pycache__/models.cpython-34.pyc
vendored
Normal file
Binary file not shown.
BIN
deps/forecastio/__pycache__/utils.cpython-34.pyc
vendored
Normal file
BIN
deps/forecastio/__pycache__/utils.cpython-34.pyc
vendored
Normal file
Binary file not shown.
69
deps/forecastio/api.py
vendored
Normal file
69
deps/forecastio/api.py
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
import requests
|
||||
import threading
|
||||
|
||||
from forecastio.models import Forecast
|
||||
|
||||
|
||||
def load_forecast(key, lat, lng, time=None, units="auto", lazy=False,
|
||||
callback=None):
|
||||
"""
|
||||
This function builds the request url and loads some or all of the
|
||||
needed json depending on lazy is True
|
||||
|
||||
inLat: The latitude of the forecast
|
||||
inLong: The longitude of the forecast
|
||||
time: A datetime.datetime object representing the desired time of
|
||||
the forecast. If no timezone is present, the API assumes local
|
||||
time at the provided latitude and longitude.
|
||||
units: A string of the preferred units of measurement, "auto" id
|
||||
default. also us,ca,uk,si is available
|
||||
lazy: Defaults to false. The function will only request the json
|
||||
data as it is needed. Results in more requests, but
|
||||
probably a faster response time (I haven't checked)
|
||||
"""
|
||||
|
||||
if time is None:
|
||||
url = 'https://api.darksky.net/forecast/%s/%s,%s' \
|
||||
'?units=%s' % (key, lat, lng, units,)
|
||||
else:
|
||||
url_time = time.replace(microsecond=0).isoformat() # API returns 400 for microseconds
|
||||
url = 'https://api.darksky.net/forecast/%s/%s,%s,%s' \
|
||||
'?units=%s' % (key, lat, lng, url_time,
|
||||
units,)
|
||||
|
||||
if lazy is True:
|
||||
baseURL = "%s&exclude=%s" % (url,
|
||||
'minutely,currently,hourly,'
|
||||
'daily,alerts,flags')
|
||||
else:
|
||||
baseURL = url
|
||||
|
||||
return manual(baseURL, callback=callback)
|
||||
|
||||
|
||||
def manual(requestURL, callback=None):
|
||||
"""
|
||||
This function is used by load_forecast OR by users to manually
|
||||
construct the URL for an API call.
|
||||
"""
|
||||
|
||||
if callback is None:
|
||||
return get_forecast(requestURL)
|
||||
else:
|
||||
thread = threading.Thread(target=load_async,
|
||||
args=(requestURL, callback))
|
||||
thread.start()
|
||||
|
||||
|
||||
def get_forecast(requestURL):
|
||||
forecastio_reponse = requests.get(requestURL)
|
||||
forecastio_reponse.raise_for_status()
|
||||
|
||||
json = forecastio_reponse.json()
|
||||
headers = forecastio_reponse.headers
|
||||
|
||||
return Forecast(json, forecastio_reponse, headers)
|
||||
|
||||
|
||||
def load_async(url, callback):
|
||||
callback(get_forecast(url))
|
||||
129
deps/forecastio/models.py
vendored
Normal file
129
deps/forecastio/models.py
vendored
Normal file
@@ -0,0 +1,129 @@
|
||||
from forecastio.utils import UnicodeMixin, PropertyUnavailable
|
||||
import datetime
|
||||
import requests
|
||||
|
||||
|
||||
class Forecast(UnicodeMixin):
|
||||
|
||||
def __init__(self, data, response, headers):
|
||||
self.response = response
|
||||
self.http_headers = headers
|
||||
self.json = data
|
||||
|
||||
self._alerts = []
|
||||
for alertJSON in self.json.get('alerts', []):
|
||||
self._alerts.append(Alert(alertJSON))
|
||||
|
||||
def update(self):
|
||||
r = requests.get(self.response.url)
|
||||
self.json = r.json()
|
||||
self.response = r
|
||||
|
||||
def currently(self):
|
||||
return self._forcastio_data('currently')
|
||||
|
||||
def minutely(self):
|
||||
return self._forcastio_data('minutely')
|
||||
|
||||
def hourly(self):
|
||||
return self._forcastio_data('hourly')
|
||||
|
||||
def daily(self):
|
||||
return self._forcastio_data('daily')
|
||||
|
||||
def offset(self):
|
||||
return self.json['offset']
|
||||
|
||||
def alerts(self):
|
||||
return self._alerts
|
||||
|
||||
def _forcastio_data(self, key):
|
||||
keys = ['minutely', 'currently', 'hourly', 'daily']
|
||||
try:
|
||||
if key not in self.json:
|
||||
keys.remove(key)
|
||||
url = "%s&exclude=%s%s" % (self.response.url.split('&')[0],
|
||||
','.join(keys), ',alerts,flags')
|
||||
|
||||
response = requests.get(url).json()
|
||||
self.json[key] = response[key]
|
||||
|
||||
if key == 'currently':
|
||||
return ForecastioDataPoint(self.json[key])
|
||||
else:
|
||||
return ForecastioDataBlock(self.json[key])
|
||||
except:
|
||||
if key == 'currently':
|
||||
return ForecastioDataPoint()
|
||||
else:
|
||||
return ForecastioDataBlock()
|
||||
|
||||
|
||||
class ForecastioDataBlock(UnicodeMixin):
|
||||
|
||||
def __init__(self, d=None):
|
||||
d = d or {}
|
||||
self.summary = d.get('summary')
|
||||
self.icon = d.get('icon')
|
||||
|
||||
self.data = [ForecastioDataPoint(datapoint)
|
||||
for datapoint in d.get('data', [])]
|
||||
|
||||
def __unicode__(self):
|
||||
return '<ForecastioDataBlock instance: ' \
|
||||
'%s with %d ForecastioDataPoints>' % (self.summary,
|
||||
len(self.data),)
|
||||
|
||||
|
||||
class ForecastioDataPoint(UnicodeMixin):
|
||||
|
||||
def __init__(self, d={}):
|
||||
self.d = d
|
||||
|
||||
try:
|
||||
self.time = datetime.datetime.utcfromtimestamp(int(d['time']))
|
||||
self.utime = d['time']
|
||||
except:
|
||||
pass
|
||||
|
||||
try:
|
||||
sr_time = int(d['sunriseTime'])
|
||||
self.sunriseTime = datetime.datetime.utcfromtimestamp(sr_time)
|
||||
except:
|
||||
self.sunriseTime = None
|
||||
|
||||
try:
|
||||
ss_time = int(d['sunsetTime'])
|
||||
self.sunsetTime = datetime.datetime.utcfromtimestamp(ss_time)
|
||||
except:
|
||||
self.sunsetTime = None
|
||||
|
||||
def __getattr__(self, name):
|
||||
try:
|
||||
return self.d[name]
|
||||
except KeyError:
|
||||
raise PropertyUnavailable(
|
||||
"Property '{}' is not valid"
|
||||
" or is not available for this forecast".format(name)
|
||||
)
|
||||
|
||||
def __unicode__(self):
|
||||
return '<ForecastioDataPoint instance: ' \
|
||||
'%s at %s>' % (self.summary, self.time,)
|
||||
|
||||
|
||||
class Alert(UnicodeMixin):
|
||||
def __init__(self, json):
|
||||
self.json = json
|
||||
|
||||
def __getattr__(self, name):
|
||||
try:
|
||||
return self.json[name]
|
||||
except KeyError:
|
||||
raise PropertyUnavailable(
|
||||
"Property '{}' is not valid"
|
||||
" or is not available for this forecast".format(name)
|
||||
)
|
||||
|
||||
def __unicode__(self):
|
||||
return '<Alert instance: {0} at {1}>'.format(self.title, self.time)
|
||||
18
deps/forecastio/utils.py
vendored
Normal file
18
deps/forecastio/utils.py
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
import sys
|
||||
|
||||
|
||||
class UnicodeMixin(object):
|
||||
|
||||
"""Mixin class to handle defining the proper __str__/__unicode__
|
||||
methods in Python 2 or 3."""
|
||||
|
||||
if sys.version_info[0] >= 3: # Python 3
|
||||
def __str__(self):
|
||||
return self.__unicode__()
|
||||
else: # Python 2
|
||||
def __str__(self):
|
||||
return self.__unicode__().encode('utf8')
|
||||
|
||||
|
||||
class PropertyUnavailable(AttributeError):
|
||||
pass
|
||||
Reference in New Issue
Block a user