2019-02-13 20:21:14 +00:00
|
|
|
"""Support for BloomSky weather station."""
|
2016-02-04 20:01:45 +00:00
|
|
|
from datetime import timedelta
|
2017-11-04 19:04:05 +00:00
|
|
|
import logging
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2017-11-04 19:04:05 +00:00
|
|
|
from aiohttp.hdrs import AUTHORIZATION
|
2016-02-04 20:01:45 +00:00
|
|
|
import requests
|
2016-09-02 04:31:32 +00:00
|
|
|
import voluptuous as vol
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
2016-09-02 04:31:32 +00:00
|
|
|
from homeassistant.helpers import discovery
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-11-04 19:04:05 +00:00
|
|
|
from homeassistant.util import Throttle
|
2016-09-02 04:31:32 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
BLOOMSKY = None
|
2016-09-02 04:31:32 +00:00
|
|
|
BLOOMSKY_TYPE = ['camera', 'binary_sensor', 'sensor']
|
2016-02-04 20:01:45 +00:00
|
|
|
|
2016-09-02 04:31:32 +00:00
|
|
|
DOMAIN = 'bloomsky'
|
2016-02-04 20:01:45 +00:00
|
|
|
|
2016-02-06 07:23:30 +00:00
|
|
|
# The BloomSky only updates every 5-8 minutes as per the API spec so there's
|
2016-02-04 20:01:45 +00:00
|
|
|
# no point in polling the API more frequently
|
|
|
|
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=300)
|
|
|
|
|
2016-09-02 04:31:32 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
}),
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
def setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the BloomSky component."""
|
2016-02-04 20:01:45 +00:00
|
|
|
api_key = config[DOMAIN][CONF_API_KEY]
|
|
|
|
|
|
|
|
global BLOOMSKY
|
|
|
|
try:
|
|
|
|
BLOOMSKY = BloomSky(api_key)
|
|
|
|
except RuntimeError:
|
|
|
|
return False
|
|
|
|
|
2016-09-02 04:31:32 +00:00
|
|
|
for component in BLOOMSKY_TYPE:
|
2016-06-12 00:43:13 +00:00
|
|
|
discovery.load_platform(hass, component, DOMAIN, {}, config)
|
2016-02-20 07:22:23 +00:00
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class BloomSky:
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Handle all communication with the BloomSky API."""
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
# API documentation at http://weatherlution.com/bloomsky-api/
|
2018-10-30 20:23:44 +00:00
|
|
|
API_URL = 'http://api.bloomsky.com/api/skydata'
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
def __init__(self, api_key):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize the BookSky."""
|
2016-02-04 20:01:45 +00:00
|
|
|
self._api_key = api_key
|
|
|
|
self.devices = {}
|
2016-09-02 04:31:32 +00:00
|
|
|
_LOGGER.debug("Initial BloomSky device load...")
|
2016-02-04 20:01:45 +00:00
|
|
|
self.refresh_devices()
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def refresh_devices(self):
|
2016-09-12 14:19:46 +00:00
|
|
|
"""Use the API to retrieve a list of devices."""
|
2016-09-02 04:31:32 +00:00
|
|
|
_LOGGER.debug("Fetching BloomSky update")
|
2017-04-30 05:04:49 +00:00
|
|
|
response = requests.get(
|
2017-11-04 19:04:05 +00:00
|
|
|
self.API_URL, headers={AUTHORIZATION: self._api_key}, timeout=10)
|
2016-02-04 20:01:45 +00:00
|
|
|
if response.status_code == 401:
|
|
|
|
raise RuntimeError("Invalid API_KEY")
|
|
|
|
elif response.status_code != 200:
|
|
|
|
_LOGGER.error("Invalid HTTP response: %s", response.status_code)
|
|
|
|
return
|
2016-03-08 16:55:57 +00:00
|
|
|
# Create dictionary keyed off of the device unique id
|
2016-02-04 20:01:45 +00:00
|
|
|
self.devices.update({
|
2016-09-02 04:31:32 +00:00
|
|
|
device['DeviceID']: device for device in response.json()
|
2016-02-04 20:01:45 +00:00
|
|
|
})
|