2019-02-14 04:35:12 +00:00
|
|
|
"""Support for OpenUV binary sensors."""
|
2021-08-25 12:18:53 +00:00
|
|
|
from homeassistant.components.binary_sensor import (
|
|
|
|
BinarySensorEntity,
|
|
|
|
BinarySensorEntityDescription,
|
|
|
|
)
|
2021-07-24 12:50:01 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-08-02 05:42:12 +00:00
|
|
|
from homeassistant.util.dt import as_local, parse_datetime, utcnow
|
|
|
|
|
2021-08-25 12:18:53 +00:00
|
|
|
from . import OpenUvEntity
|
2020-11-22 11:50:22 +00:00
|
|
|
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
|
|
|
|
2021-08-25 12:18:53 +00:00
|
|
|
BINARY_SENSOR_DESCRIPTION_PROTECTION_WINDOW = BinarySensorEntityDescription(
|
|
|
|
key=TYPE_PROTECTION_WINDOW,
|
|
|
|
name="Protection Window",
|
|
|
|
icon="mdi:sunglasses",
|
|
|
|
)
|
2020-01-23 01:48:20 +00:00
|
|
|
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2021-07-24 12:50:01 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2018-09-04 07:22:44 +00:00
|
|
|
"""Set up an OpenUV sensor based on a config entry."""
|
2021-10-17 05:59:19 +00:00
|
|
|
openuv = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT]
|
2021-08-25 12:18:53 +00:00
|
|
|
async_add_entities(
|
|
|
|
[OpenUvBinarySensor(openuv, BINARY_SENSOR_DESCRIPTION_PROTECTION_WINDOW)]
|
|
|
|
)
|
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."""
|
|
|
|
|
2020-03-18 03:03:16 +00:00
|
|
|
@callback
|
2021-07-24 12:50:01 +00:00
|
|
|
def update_from_latest_data(self) -> None:
|
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
|
|
|
|
|
2021-08-25 12:18:53 +00:00
|
|
|
if self.entity_description.key == TYPE_PROTECTION_WINDOW:
|
2021-07-24 12:50:01 +00:00
|
|
|
from_dt = parse_datetime(data["from_time"])
|
|
|
|
to_dt = parse_datetime(data["to_time"])
|
|
|
|
|
|
|
|
if not from_dt or not to_dt:
|
|
|
|
LOGGER.warning(
|
|
|
|
"Unable to parse protection window datetimes: %s, %s",
|
|
|
|
data["from_time"],
|
|
|
|
data["to_time"],
|
|
|
|
)
|
|
|
|
self._attr_is_on = False
|
|
|
|
return
|
|
|
|
|
|
|
|
self._attr_is_on = from_dt <= utcnow() <= to_dt
|
2021-07-02 07:47:49 +00:00
|
|
|
self._attr_extra_state_attributes.update(
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
2021-07-24 12:50:01 +00:00
|
|
|
ATTR_PROTECTION_WINDOW_ENDING_TIME: as_local(to_dt),
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_PROTECTION_WINDOW_ENDING_UV: data["to_uv"],
|
|
|
|
ATTR_PROTECTION_WINDOW_STARTING_UV: data["from_uv"],
|
2021-07-24 12:50:01 +00:00
|
|
|
ATTR_PROTECTION_WINDOW_STARTING_TIME: as_local(from_dt),
|
2019-07-31 19:25:30 +00:00
|
|
|
}
|
|
|
|
)
|