core/homeassistant/components/bloomsky/__init__.py

80 lines
2.5 KiB
Python
Raw Normal View History

"""Support for BloomSky weather station."""
2016-02-04 20:01:45 +00:00
from datetime import timedelta
import logging
2016-02-19 05:27:50 +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
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):
"""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, 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:
discovery.load_platform(hass, component, DOMAIN, {}, config)
2016-02-04 20:01:45 +00:00
return True
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
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 = {}
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")
response = requests.get(
f"{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")
if response.status_code == 405:
_LOGGER.error("You have no bloomsky devices configured")
return
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()})