2019-02-13 20:21:14 +00:00
|
|
|
"""Support the binary sensors of a BloomSky weather station."""
|
2022-01-03 12:10:41 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2016-09-02 04:31:32 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-09-13 14:33:54 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
PLATFORM_SCHEMA,
|
2021-12-09 08:14:16 +00:00
|
|
|
BinarySensorDeviceClass,
|
2020-09-13 14:33:54 +00:00
|
|
|
BinarySensorEntity,
|
|
|
|
)
|
2016-09-02 04:31:32 +00:00
|
|
|
from homeassistant.const import CONF_MONITORED_CONDITIONS
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2016-09-02 04:31:32 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2022-01-03 12:10:41 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2016-09-02 04:31:32 +00:00
|
|
|
|
2020-04-05 21:16:30 +00:00
|
|
|
from . import DOMAIN
|
2019-04-12 06:37:45 +00:00
|
|
|
|
2021-12-09 08:14:16 +00:00
|
|
|
SENSOR_TYPES = {"Rain": BinarySensorDeviceClass.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
|
|
|
|
2022-01-03 12:10:41 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> 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
|
2020-04-23 23:00:57 +00:00
|
|
|
if discovery_info is not None:
|
|
|
|
return
|
|
|
|
|
2020-04-09 07:26:06 +00:00
|
|
|
sensors = config[CONF_MONITORED_CONDITIONS]
|
2020-04-05 21:16:30 +00:00
|
|
|
bloomsky = hass.data[DOMAIN]
|
2016-02-20 07:21:56 +00:00
|
|
|
|
2020-04-05 21:16:30 +00:00
|
|
|
for device in bloomsky.devices.values():
|
2016-02-20 07:21:56 +00:00
|
|
|
for variable in sensors:
|
2020-04-05 21:16:30 +00:00
|
|
|
add_entities([BloomSkySensor(bloomsky, device, variable)], True)
|
2016-02-20 07:21:56 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class BloomSkySensor(BinarySensorEntity):
|
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
|
|
|
|
2021-11-25 23:13:27 +00:00
|
|
|
def __init__(self, bs, device, sensor_name): # pylint: disable=invalid-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
|
2021-07-15 13:29:12 +00:00
|
|
|
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()
|
|
|
|
|
2021-07-15 13:29:12 +00:00
|
|
|
self._attr_is_on = self._bloomsky.devices[self._device_id]["Data"][
|
|
|
|
self._sensor_name
|
|
|
|
]
|