hass-core/homeassistant/components/openuv/binary_sensor.py
Aaron Bach ca5a9c9456
Allow multiple instances of OpenUV via the homeassistant.update_entity service (#76878)
* Allow for multiple instances of the OpenUV integration

* Docstring

* Remove Repairs

* Fix tests

* Slightly faster OpenUV object lookup

* Entity update service

* Remove service descriptions

* hassfest

* Simplify strings

* Don't add UI instructions to Repairs item

* Add a throttle to entity update

* Update homeassistant/components/openuv/__init__.py

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>

* Switch from Throttle to Debouncer(s)

* Keep dispatcher for services

* Reduce change surface area

* Duplicate method

* Add issue registry through helper

* Update deprecation version

* Use config entry selector

* Remove device/service info

* Remove commented out method

* Correct entity IDs and better verbiage

* Fix tests

* Handle missing config entry ID in service calls

* Remove unhelpful comment

* Remove unused constants

Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
Co-authored-by: J. Nick Koston <nick@koston.org>
2022-09-17 17:56:45 -06:00

82 lines
2.9 KiB
Python

"""Support for OpenUV binary sensors."""
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.dt import as_local, parse_datetime, utcnow
from . import OpenUvEntity
from .const import DATA_PROTECTION_WINDOW, DOMAIN, LOGGER, TYPE_PROTECTION_WINDOW
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"
BINARY_SENSOR_DESCRIPTION_PROTECTION_WINDOW = BinarySensorEntityDescription(
key=TYPE_PROTECTION_WINDOW,
name="Protection window",
icon="mdi:sunglasses",
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up an OpenUV sensor based on a config entry."""
openuv = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
[OpenUvBinarySensor(openuv, BINARY_SENSOR_DESCRIPTION_PROTECTION_WINDOW)]
)
class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity):
"""Define a binary sensor for OpenUV."""
async def async_update(self) -> None:
"""Update the entity.
Only used by the generic entity update service.
"""
await self.openuv.async_update_protection_data()
self.async_update_state()
@callback
def update_from_latest_data(self) -> None:
"""Update the state."""
if not (data := self.openuv.data[DATA_PROTECTION_WINDOW]):
self._attr_available = False
return
self._attr_available = True
for key in ("from_time", "to_time", "from_uv", "to_uv"):
if not data.get(key):
LOGGER.info("Skipping update due to missing data: %s", key)
return
if self.entity_description.key == TYPE_PROTECTION_WINDOW:
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
self._attr_extra_state_attributes.update(
{
ATTR_PROTECTION_WINDOW_ENDING_TIME: as_local(to_dt),
ATTR_PROTECTION_WINDOW_ENDING_UV: data["to_uv"],
ATTR_PROTECTION_WINDOW_STARTING_UV: data["from_uv"],
ATTR_PROTECTION_WINDOW_STARTING_TIME: as_local(from_dt),
}
)