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
|
2019-07-31 19:25:30 +00:00
|
|
|
BLOOMSKY_TYPE = ["camera", "binary_sensor", "sensor"]
|
2016-02-04 20:01:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +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)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{DOMAIN: vol.Schema({vol.Required(CONF_API_KEY): cv.string})}, extra=vol.ALLOW_EXTRA
|
|
|
|
)
|
2016-09-02 04:31:32 +00:00
|
|
|
|
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:
|
2019-07-24 17:37:36 +00:00
|
|
|
BLOOMSKY = BloomSky(api_key, hass.config.units.is_metric)
|
2016-02-04 20:01:45 +00:00
|
|
|
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/
|
2019-07-31 19:25:30 +00:00
|
|
|
API_URL = "http://api.bloomsky.com/api/skydata"
|
2016-02-04 20:01:45 +00:00
|
|
|
|
2019-07-24 17:37:36 +00:00
|
|
|
def __init__(self, api_key, is_metric):
|
2016-03-08 16:55:57 +00:00
|
|
|
"""Initialize the BookSky."""
|
2016-02-04 20:01:45 +00:00
|
|
|
self._api_key = api_key
|
2019-07-31 19:25:30 +00:00
|
|
|
self._endpoint_argument = "unit=intl" if is_metric else ""
|
2016-02-04 20:01:45 +00:00
|
|
|
self.devices = {}
|
2019-07-24 17:37:36 +00:00
|
|
|
self.is_metric = is_metric
|
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(
|
2019-07-24 17:37:36 +00:00
|
|
|
"{}?{}".format(self.API_URL, self._endpoint_argument),
|
2019-07-31 19:25:30 +00:00
|
|
|
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")
|
2019-07-24 17:37:36 +00:00
|
|
|
if response.status_code == 405:
|
|
|
|
_LOGGER.error("You have no bloomsky devices configured")
|
|
|
|
return
|
2019-02-27 21:10:40 +00:00
|
|
|
if response.status_code != 200:
|
2016-02-04 20:01:45 +00:00
|
|
|
_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
|
2019-07-31 19:25:30 +00:00
|
|
|
self.devices.update({device["DeviceID"]: device for device in response.json()})
|