core/homeassistant/components/bloomsky/__init__.py

83 lines
2.7 KiB
Python
Raw Normal View History

"""Support for BloomSky weather station."""
2016-02-04 20:01:45 +00:00
from datetime import timedelta
from http import HTTPStatus
import logging
2016-02-19 05:27:50 +00:00
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
from homeassistant.const import CONF_API_KEY, Platform
from homeassistant.core import HomeAssistant
2016-09-02 04:31:32 +00:00
from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
from homeassistant.util import Throttle
from homeassistant.util.unit_system import METRIC_SYSTEM
2016-09-02 04:31:32 +00:00
_LOGGER = logging.getLogger(__name__)
2016-02-04 20:01:45 +00:00
PLATFORMS = [Platform.CAMERA, Platform.BINARY_SENSOR, Platform.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: HomeAssistant, config: ConfigType) -> bool:
"""Set up the BloomSky integration."""
2016-02-04 20:01:45 +00:00
api_key = config[DOMAIN][CONF_API_KEY]
try:
bloomsky = BloomSky(api_key, hass.config.units is METRIC_SYSTEM)
2016-02-04 20:01:45 +00:00
except RuntimeError:
return False
hass.data[DOMAIN] = bloomsky
for platform in PLATFORMS:
discovery.load_platform(hass, platform, 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
_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}",
headers={"Authorization": self._api_key},
2019-07-31 19:25:30 +00:00
timeout=10,
)
if response.status_code == HTTPStatus.UNAUTHORIZED:
2016-02-04 20:01:45 +00:00
raise RuntimeError("Invalid API_KEY")
if response.status_code == HTTPStatus.METHOD_NOT_ALLOWED:
_LOGGER.error("You have no bloomsky devices configured")
return
if response.status_code != HTTPStatus.OK:
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()})