Replace custom OpenUV data object with coordinators (#80705)

* Replace custom OpenUV data object with coordinators

* Typing

* Code review
This commit is contained in:
Aaron Bach 2022-10-20 19:37:20 -06:00 committed by GitHub
parent 245c13e6ed
commit 60b3d6816b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 168 additions and 194 deletions

View file

@ -10,6 +10,7 @@ from homeassistant.util.dt import as_local, parse_datetime, utcnow
from . import OpenUvEntity
from .const import DATA_PROTECTION_WINDOW, DOMAIN, LOGGER, TYPE_PROTECTION_WINDOW
from .coordinator import OpenUvCoordinator
ATTR_PROTECTION_WINDOW_ENDING_TIME = "end_time"
ATTR_PROTECTION_WINDOW_ENDING_UV = "end_uv"
@ -26,32 +27,27 @@ BINARY_SENSOR_DESCRIPTION_PROTECTION_WINDOW = BinarySensorEntityDescription(
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
# Once we've successfully authenticated, we re-enable client request retries:
"""Set up an OpenUV sensor based on a config entry."""
openuv = hass.data[DOMAIN][entry.entry_id]
coordinators: dict[str, OpenUvCoordinator] = hass.data[DOMAIN][entry.entry_id]
async_add_entities(
[OpenUvBinarySensor(openuv, BINARY_SENSOR_DESCRIPTION_PROTECTION_WINDOW)]
[
OpenUvBinarySensor(
coordinators[DATA_PROTECTION_WINDOW],
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
def _update_from_latest_data(self) -> None:
"""Update the entity from the latest data."""
data = self.coordinator.data
for key in ("from_time", "to_time", "from_uv", "to_uv"):
if not data.get(key):