2019-02-14 04:35:12 +00:00
|
|
|
"""Support for OpenUV binary sensors."""
|
2020-04-23 19:57:07 +00:00
|
|
|
from homeassistant.components.binary_sensor import BinarySensorEntity
|
2019-02-14 04:35:12 +00:00
|
|
|
from homeassistant.core import callback
|
2018-08-02 05:42:12 +00:00
|
|
|
from homeassistant.util.dt import as_local, parse_datetime, utcnow
|
|
|
|
|
2020-11-22 11:50:22 +00:00
|
|
|
from . import OpenUvEntity
|
|
|
|
from .const import (
|
|
|
|
DATA_CLIENT,
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_PROTECTION_WINDOW,
|
|
|
|
DOMAIN,
|
2020-11-22 11:50:22 +00:00
|
|
|
LOGGER,
|
2019-07-31 19:25:30 +00:00
|
|
|
TYPE_PROTECTION_WINDOW,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_PROTECTION_WINDOW_ENDING_TIME = "end_time"
|
|
|
|
ATTR_PROTECTION_WINDOW_ENDING_UV = "end_uv"
|
|
|
|
ATTR_PROTECTION_WINDOW_STARTING_TIME = "start_time"
|
|
|
|
ATTR_PROTECTION_WINDOW_STARTING_UV = "start_uv"
|
2019-02-14 04:35:12 +00:00
|
|
|
|
2020-01-23 01:48:20 +00:00
|
|
|
BINARY_SENSORS = {TYPE_PROTECTION_WINDOW: ("Protection Window", "mdi:sunglasses")}
|
|
|
|
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2018-09-04 07:22:44 +00:00
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
|
|
"""Set up an OpenUV sensor based on a config entry."""
|
2020-11-22 11:50:22 +00:00
|
|
|
openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
2018-08-02 05:42:12 +00:00
|
|
|
|
|
|
|
binary_sensors = []
|
2020-01-23 01:48:20 +00:00
|
|
|
for kind, attrs in BINARY_SENSORS.items():
|
|
|
|
name, icon = attrs
|
2021-07-02 07:47:49 +00:00
|
|
|
binary_sensors.append(OpenUvBinarySensor(openuv, kind, name, icon))
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
async_add_entities(binary_sensors, True)
|
2018-08-02 05:42:12 +00:00
|
|
|
|
|
|
|
|
2020-04-23 19:57:07 +00:00
|
|
|
class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity):
|
2018-08-02 05:42:12 +00:00
|
|
|
"""Define a binary sensor for OpenUV."""
|
|
|
|
|
2021-07-02 07:47:49 +00:00
|
|
|
def __init__(self, openuv, sensor_type, name, icon):
|
2018-08-02 05:42:12 +00:00
|
|
|
"""Initialize the sensor."""
|
2021-07-02 07:47:49 +00:00
|
|
|
super().__init__(openuv, sensor_type)
|
|
|
|
|
|
|
|
self._attr_icon = icon
|
|
|
|
self._attr_name = name
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2020-03-18 03:03:16 +00:00
|
|
|
@callback
|
|
|
|
def update_from_latest_data(self):
|
2018-08-02 05:42:12 +00:00
|
|
|
"""Update the state."""
|
2018-10-02 08:34:12 +00:00
|
|
|
data = self.openuv.data[DATA_PROTECTION_WINDOW]
|
|
|
|
|
|
|
|
if not data:
|
2021-07-02 07:47:49 +00:00
|
|
|
self._attr_available = False
|
2018-10-02 08:34:12 +00:00
|
|
|
return
|
|
|
|
|
2021-07-02 07:47:49 +00:00
|
|
|
self._attr_available = True
|
2020-01-20 20:33:58 +00:00
|
|
|
|
2019-09-28 00:03:15 +00:00
|
|
|
for key in ("from_time", "to_time", "from_uv", "to_uv"):
|
|
|
|
if not data.get(key):
|
2020-11-22 11:50:22 +00:00
|
|
|
LOGGER.info("Skipping update due to missing data: %s", key)
|
2019-09-28 00:03:15 +00:00
|
|
|
return
|
|
|
|
|
2018-08-02 05:42:12 +00:00
|
|
|
if self._sensor_type == TYPE_PROTECTION_WINDOW:
|
2021-07-02 07:47:49 +00:00
|
|
|
self._attr_is_on = (
|
2019-07-31 19:25:30 +00:00
|
|
|
parse_datetime(data["from_time"])
|
|
|
|
<= utcnow()
|
|
|
|
<= parse_datetime(data["to_time"])
|
|
|
|
)
|
2021-07-02 07:47:49 +00:00
|
|
|
self._attr_extra_state_attributes.update(
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
|
|
|
ATTR_PROTECTION_WINDOW_ENDING_TIME: as_local(
|
|
|
|
parse_datetime(data["to_time"])
|
|
|
|
),
|
|
|
|
ATTR_PROTECTION_WINDOW_ENDING_UV: data["to_uv"],
|
|
|
|
ATTR_PROTECTION_WINDOW_STARTING_UV: data["from_uv"],
|
|
|
|
ATTR_PROTECTION_WINDOW_STARTING_TIME: as_local(
|
|
|
|
parse_datetime(data["from_time"])
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|