2019-02-13 20:21:14 +00:00
|
|
|
"""Support the binary sensors of a BloomSky weather station."""
|
2016-02-20 07:21:56 +00:00
|
|
|
import logging
|
|
|
|
|
2016-09-02 04:31:32 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorDevice, PLATFORM_SCHEMA
|
2016-09-02 04:31:32 +00:00
|
|
|
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-04-12 06:37:45 +00:00
|
|
|
from . import BLOOMSKY
|
|
|
|
|
2016-09-02 04:31:32 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2016-02-20 07:21:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_TYPES = {"Rain": "moisture", "Night": None}
|
2016-02-20 07:21:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2016-09-02 04:31:32 +00:00
|
|
|
|
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."""
|
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
|
|
|
|
2019-04-12 06:37:45 +00:00
|
|
|
for device in BLOOMSKY.devices.values():
|
2016-02-20 07:21:56 +00:00
|
|
|
for variable in sensors:
|
2019-07-31 19:25:30 +00:00
|
|
|
add_entities([BloomSkySensor(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
|
2019-07-31 19:25:30 +00:00
|
|
|
self._device_id = device["DeviceID"]
|
2016-02-20 07:21:56 +00:00
|
|
|
self._sensor_name = sensor_name
|
2019-07-31 19:25:30 +00:00
|
|
|
self._name = "{} {}".format(device["DeviceName"], sensor_name)
|
2017-08-08 18:21:33 +00:00
|
|
|
self._state = None
|
2019-09-03 15:09:59 +00:00
|
|
|
self._unique_id = f"{self._device_id}-{self._sensor_name}"
|
2018-10-13 08:23:00 +00:00
|
|
|
|
|
|
|
@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()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
self._state = self._bloomsky.devices[self._device_id]["Data"][self._sensor_name]
|