2016-02-04 20:01:45 +00:00
|
|
|
"""
|
|
|
|
homeassistant.components.bloomsky
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
Support for BloomSky weather station.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/bloomsky/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
from datetime import timedelta
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
import requests
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-02-20 07:22:23 +00:00
|
|
|
from homeassistant.components import discovery
|
2016-02-04 20:01:45 +00:00
|
|
|
from homeassistant.const import CONF_API_KEY
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers import validate_config
|
|
|
|
from homeassistant.util import Throttle
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
DOMAIN = "bloomsky"
|
|
|
|
BLOOMSKY = None
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
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-02-20 07:22:23 +00:00
|
|
|
DISCOVER_SENSORS = 'bloomsky.sensors'
|
|
|
|
DISCOVER_BINARY_SENSORS = 'bloomsky.binary_sensor'
|
|
|
|
DISCOVER_CAMERAS = 'bloomsky.camera'
|
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
|
|
|
|
# pylint: disable=unused-argument,too-few-public-methods
|
|
|
|
def setup(hass, config):
|
2016-02-06 07:23:30 +00:00
|
|
|
""" Setup BloomSky component. """
|
2016-02-04 20:01:45 +00:00
|
|
|
if not validate_config(
|
|
|
|
config,
|
|
|
|
{DOMAIN: [CONF_API_KEY]},
|
|
|
|
_LOGGER):
|
|
|
|
return False
|
|
|
|
|
|
|
|
api_key = config[DOMAIN][CONF_API_KEY]
|
|
|
|
|
|
|
|
global BLOOMSKY
|
|
|
|
try:
|
|
|
|
BLOOMSKY = BloomSky(api_key)
|
|
|
|
except RuntimeError:
|
|
|
|
return False
|
|
|
|
|
2016-02-20 07:22:23 +00:00
|
|
|
for component, discovery_service in (
|
|
|
|
('camera', DISCOVER_CAMERAS), ('sensor', DISCOVER_SENSORS),
|
|
|
|
('binary_sensor', DISCOVER_BINARY_SENSORS)):
|
|
|
|
discovery.discover(hass, discovery_service, component=component,
|
|
|
|
hass_config=config)
|
|
|
|
|
2016-02-04 20:01:45 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class BloomSky(object):
|
2016-02-06 07:23:30 +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/
|
|
|
|
|
|
|
|
API_URL = "https://api.bloomsky.com/api/skydata"
|
|
|
|
|
|
|
|
def __init__(self, api_key):
|
|
|
|
self._api_key = api_key
|
|
|
|
self.devices = {}
|
|
|
|
_LOGGER.debug("Initial bloomsky device load...")
|
|
|
|
self.refresh_devices()
|
|
|
|
|
|
|
|
@Throttle(MIN_TIME_BETWEEN_UPDATES)
|
|
|
|
def refresh_devices(self):
|
|
|
|
"""
|
|
|
|
Uses the API to retreive a list of devices associated with an
|
|
|
|
account along with all the sensors on the device.
|
|
|
|
"""
|
|
|
|
_LOGGER.debug("Fetching bloomsky update")
|
|
|
|
response = requests.get(self.API_URL,
|
|
|
|
headers={"Authorization": self._api_key},
|
|
|
|
timeout=10)
|
|
|
|
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
|
|
|
|
# create dictionary keyed off of the device unique id
|
|
|
|
self.devices.update({
|
|
|
|
device["DeviceID"]: device for device in response.json()
|
|
|
|
})
|