core/homeassistant/components/bloomsky/binary_sensor.py

58 lines
1.8 KiB
Python
Raw Normal View History

"""Support the binary sensors of a BloomSky weather station."""
2016-09-02 04:31:32 +00:00
import voluptuous as vol
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_MOISTURE,
PLATFORM_SCHEMA,
BinarySensorEntity,
)
2016-09-02 04:31:32 +00:00
from homeassistant.const import CONF_MONITORED_CONDITIONS
import homeassistant.helpers.config_validation as cv
from . import DOMAIN
SENSOR_TYPES = {"Rain": DEVICE_CLASS_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
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the available BloomSky weather binary sensors."""
# Default needed in case of discovery
if discovery_info is not None:
return
sensors = config[CONF_MONITORED_CONDITIONS]
bloomsky = hass.data[DOMAIN]
2016-02-20 07:21:56 +00:00
for device in bloomsky.devices.values():
2016-02-20 07:21:56 +00:00
for variable in sensors:
add_entities([BloomSkySensor(bloomsky, device, variable)], True)
2016-02-20 07:21:56 +00:00
class BloomSkySensor(BinarySensorEntity):
"""Representation of a single binary sensor in a BloomSky device."""
2016-02-20 07:21:56 +00:00
2021-11-25 23:13:27 +00:00
def __init__(self, bs, device, sensor_name): # pylint: disable=invalid-name
"""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
self._attr_name = f"{device['DeviceName']} {sensor_name}"
self._attr_unique_id = f"{self._device_id}-{sensor_name}"
self._attr_device_class = SENSOR_TYPES.get(sensor_name)
2016-02-20 07:21:56 +00:00
def update(self):
"""Request an update from the BloomSky API."""
self._bloomsky.refresh_devices()
self._attr_is_on = self._bloomsky.devices[self._device_id]["Data"][
self._sensor_name
]