68 lines
2.1 KiB
Python
68 lines
2.1 KiB
Python
"""Support the binary sensors of a BloomSky weather station."""
|
|
from __future__ import annotations
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components.binary_sensor import (
|
|
PLATFORM_SCHEMA,
|
|
BinarySensorDeviceClass,
|
|
BinarySensorEntity,
|
|
)
|
|
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
|
from homeassistant.core import HomeAssistant
|
|
import homeassistant.helpers.config_validation as cv
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
|
|
from . import DOMAIN
|
|
|
|
SENSOR_TYPES = {"Rain": BinarySensorDeviceClass.MOISTURE, "Night": None}
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
{
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSOR_TYPES)): vol.All(
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
|
)
|
|
}
|
|
)
|
|
|
|
|
|
def setup_platform(
|
|
hass: HomeAssistant,
|
|
config: ConfigType,
|
|
add_entities: AddEntitiesCallback,
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
) -> 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]
|
|
|
|
for device in bloomsky.devices.values():
|
|
for variable in sensors:
|
|
add_entities([BloomSkySensor(bloomsky, device, variable)], True)
|
|
|
|
|
|
class BloomSkySensor(BinarySensorEntity):
|
|
"""Representation of a single binary sensor in a BloomSky device."""
|
|
|
|
def __init__(self, bs, device, sensor_name):
|
|
"""Initialize a BloomSky binary sensor."""
|
|
self._bloomsky = bs
|
|
self._device_id = device["DeviceID"]
|
|
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)
|
|
|
|
def update(self) -> None:
|
|
"""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
|
|
]
|