2016-02-20 07:21:56 +00:00
|
|
|
"""
|
|
|
|
Support the binary sensors of a BloomSky weather station.
|
|
|
|
|
|
|
|
For more details about this component, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/binary_sensor.bloomsky/
|
|
|
|
"""
|
|
|
|
import logging
|
|
|
|
|
2016-09-02 04:31:32 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorDevice, PLATFORM_SCHEMA)
|
|
|
|
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-02-20 07:21:56 +00:00
|
|
|
|
2016-09-02 04:31:32 +00:00
|
|
|
DEPENDENCIES = ['bloomsky']
|
2016-02-20 07:21:56 +00:00
|
|
|
|
|
|
|
SENSOR_TYPES = {
|
2016-09-02 04:31:32 +00:00
|
|
|
'Rain': 'moisture',
|
|
|
|
'Night': None,
|
2016-02-20 07:21:56 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 04:31:32 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
2018-02-17 09:29:14 +00:00
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)):
|
2016-09-02 04:31:32 +00:00
|
|
|
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
|
|
|
})
|
|
|
|
|
2016-02-20 07:21:56 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the available BloomSky weather binary sensors."""
|
2018-05-01 18:57:30 +00:00
|
|
|
bloomsky = hass.components.bloomsky
|
2016-09-10 16:12:24 +00:00
|
|
|
# Default needed in case of discovery
|
|
|
|
sensors = config.get(CONF_MONITORED_CONDITIONS, SENSOR_TYPES)
|
2016-02-20 07:21:56 +00:00
|
|
|
|
|
|
|
for device in bloomsky.BLOOMSKY.devices.values():
|
|
|
|
for variable in sensors:
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(
|
2017-08-08 18:21:33 +00:00
|
|
|
[BloomSkySensor(bloomsky.BLOOMSKY, device, variable)], True)
|
2016-02-20 07:21:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BloomSkySensor(BinarySensorDevice):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Representation of a single binary sensor in a BloomSky device."""
|
2016-02-20 07:21:56 +00:00
|
|
|
|
|
|
|
def __init__(self, bs, device, sensor_name):
|
2016-02-22 09:11:46 +00:00
|
|
|
"""Initialize a BloomSky binary sensor."""
|
2016-02-20 07:21:56 +00:00
|
|
|
self._bloomsky = bs
|
2016-09-02 04:31:32 +00:00
|
|
|
self._device_id = device['DeviceID']
|
2016-02-20 07:21:56 +00:00
|
|
|
self._sensor_name = sensor_name
|
2016-09-02 04:31:32 +00:00
|
|
|
self._name = '{} {}'.format(device['DeviceName'], sensor_name)
|
2017-08-08 18:21:33 +00:00
|
|
|
self._state = None
|
2018-10-13 08:23:00 +00:00
|
|
|
self._unique_id = '{}-{}'.format(self._device_id, self._sensor_name)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._unique_id
|
2016-02-20 07:21:56 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Return the name of the BloomSky device and this sensor."""
|
2016-02-20 07:21:56 +00:00
|
|
|
return self._name
|
|
|
|
|
2016-02-20 08:08:02 +00:00
|
|
|
@property
|
2017-02-11 04:46:15 +00:00
|
|
|
def device_class(self):
|
|
|
|
"""Return the class of this sensor, from DEVICE_CLASSES."""
|
2016-02-20 08:08:02 +00:00
|
|
|
return SENSOR_TYPES.get(self._sensor_name)
|
|
|
|
|
2016-02-20 07:21:56 +00:00
|
|
|
@property
|
|
|
|
def is_on(self):
|
2016-03-07 19:21:08 +00:00
|
|
|
"""Return true if binary sensor is on."""
|
2016-02-20 07:21:56 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Request an update from the BloomSky API."""
|
|
|
|
self._bloomsky.refresh_devices()
|
|
|
|
|
|
|
|
self._state = \
|
2016-09-02 04:31:32 +00:00
|
|
|
self._bloomsky.devices[self._device_id]['Data'][self._sensor_name]
|