Clean up usage of EntityDescription in OpenUV (#55127)
* Clean up usage of EntityDescription in OpenUV * Remove redundant typing * Code review * Update homeassistant/components/openuv/__init__.py Co-authored-by: Franck Nijhof <git@frenck.dev> * Black Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
parent
7dd169b48e
commit
2271f3b5f9
3 changed files with 29 additions and 43 deletions
|
@ -24,7 +24,7 @@ from homeassistant.helpers.dispatcher import (
|
||||||
async_dispatcher_connect,
|
async_dispatcher_connect,
|
||||||
async_dispatcher_send,
|
async_dispatcher_send,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity, EntityDescription
|
||||||
from homeassistant.helpers.service import verify_domain_control
|
from homeassistant.helpers.service import verify_domain_control
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
|
@ -175,14 +175,14 @@ class OpenUV:
|
||||||
class OpenUvEntity(Entity):
|
class OpenUvEntity(Entity):
|
||||||
"""Define a generic OpenUV entity."""
|
"""Define a generic OpenUV entity."""
|
||||||
|
|
||||||
def __init__(self, openuv: OpenUV, sensor_type: str) -> None:
|
def __init__(self, openuv: OpenUV, description: EntityDescription) -> None:
|
||||||
"""Initialize."""
|
"""Initialize."""
|
||||||
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
||||||
self._attr_should_poll = False
|
self._attr_should_poll = False
|
||||||
self._attr_unique_id = (
|
self._attr_unique_id = (
|
||||||
f"{openuv.client.latitude}_{openuv.client.longitude}_{sensor_type}"
|
f"{openuv.client.latitude}_{openuv.client.longitude}_{description.key}"
|
||||||
)
|
)
|
||||||
self._sensor_type = sensor_type
|
self.entity_description = description
|
||||||
self.openuv = openuv
|
self.openuv = openuv
|
||||||
|
|
||||||
async def async_added_to_hass(self) -> None:
|
async def async_added_to_hass(self) -> None:
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
"""Support for OpenUV binary sensors."""
|
"""Support for OpenUV binary sensors."""
|
||||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
from homeassistant.components.binary_sensor import (
|
||||||
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util.dt import as_local, parse_datetime, utcnow
|
from homeassistant.util.dt import as_local, parse_datetime, utcnow
|
||||||
|
|
||||||
from . import OpenUV, OpenUvEntity
|
from . import OpenUvEntity
|
||||||
from .const import (
|
from .const import (
|
||||||
DATA_CLIENT,
|
DATA_CLIENT,
|
||||||
DATA_PROTECTION_WINDOW,
|
DATA_PROTECTION_WINDOW,
|
||||||
|
@ -19,7 +22,11 @@ ATTR_PROTECTION_WINDOW_ENDING_UV = "end_uv"
|
||||||
ATTR_PROTECTION_WINDOW_STARTING_TIME = "start_time"
|
ATTR_PROTECTION_WINDOW_STARTING_TIME = "start_time"
|
||||||
ATTR_PROTECTION_WINDOW_STARTING_UV = "start_uv"
|
ATTR_PROTECTION_WINDOW_STARTING_UV = "start_uv"
|
||||||
|
|
||||||
BINARY_SENSORS = {TYPE_PROTECTION_WINDOW: ("Protection Window", "mdi:sunglasses")}
|
BINARY_SENSOR_DESCRIPTION_PROTECTION_WINDOW = BinarySensorEntityDescription(
|
||||||
|
key=TYPE_PROTECTION_WINDOW,
|
||||||
|
name="Protection Window",
|
||||||
|
icon="mdi:sunglasses",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
|
@ -27,25 +34,14 @@ async def async_setup_entry(
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up an OpenUV sensor based on a config entry."""
|
"""Set up an OpenUV sensor based on a config entry."""
|
||||||
openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
||||||
|
async_add_entities(
|
||||||
binary_sensors = []
|
[OpenUvBinarySensor(openuv, BINARY_SENSOR_DESCRIPTION_PROTECTION_WINDOW)]
|
||||||
for kind, attrs in BINARY_SENSORS.items():
|
)
|
||||||
name, icon = attrs
|
|
||||||
binary_sensors.append(OpenUvBinarySensor(openuv, kind, name, icon))
|
|
||||||
|
|
||||||
async_add_entities(binary_sensors, True)
|
|
||||||
|
|
||||||
|
|
||||||
class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity):
|
class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity):
|
||||||
"""Define a binary sensor for OpenUV."""
|
"""Define a binary sensor for OpenUV."""
|
||||||
|
|
||||||
def __init__(self, openuv: OpenUV, sensor_type: str, name: str, icon: str) -> None:
|
|
||||||
"""Initialize the sensor."""
|
|
||||||
super().__init__(openuv, sensor_type)
|
|
||||||
|
|
||||||
self._attr_icon = icon
|
|
||||||
self._attr_name = name
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_from_latest_data(self) -> None:
|
def update_from_latest_data(self) -> None:
|
||||||
"""Update the state."""
|
"""Update the state."""
|
||||||
|
@ -62,7 +58,7 @@ class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity):
|
||||||
LOGGER.info("Skipping update due to missing data: %s", key)
|
LOGGER.info("Skipping update due to missing data: %s", key)
|
||||||
return
|
return
|
||||||
|
|
||||||
if self._sensor_type == TYPE_PROTECTION_WINDOW:
|
if self.entity_description.key == TYPE_PROTECTION_WINDOW:
|
||||||
from_dt = parse_datetime(data["from_time"])
|
from_dt = parse_datetime(data["from_time"])
|
||||||
to_dt = parse_datetime(data["to_time"])
|
to_dt = parse_datetime(data["to_time"])
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util.dt import as_local, parse_datetime
|
from homeassistant.util.dt import as_local, parse_datetime
|
||||||
|
|
||||||
from . import OpenUV, OpenUvEntity
|
from . import OpenUvEntity
|
||||||
from .const import (
|
from .const import (
|
||||||
DATA_CLIENT,
|
DATA_CLIENT,
|
||||||
DATA_UV,
|
DATA_UV,
|
||||||
|
@ -42,7 +42,7 @@ UV_LEVEL_HIGH = "High"
|
||||||
UV_LEVEL_MODERATE = "Moderate"
|
UV_LEVEL_MODERATE = "Moderate"
|
||||||
UV_LEVEL_LOW = "Low"
|
UV_LEVEL_LOW = "Low"
|
||||||
|
|
||||||
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
SENSOR_DESCRIPTIONS = (
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key=TYPE_CURRENT_OZONE_LEVEL,
|
key=TYPE_CURRENT_OZONE_LEVEL,
|
||||||
name="Current Ozone Level",
|
name="Current Ozone Level",
|
||||||
|
@ -59,7 +59,6 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
key=TYPE_CURRENT_UV_LEVEL,
|
key=TYPE_CURRENT_UV_LEVEL,
|
||||||
name="Current UV Level",
|
name="Current UV Level",
|
||||||
icon="mdi:weather-sunny",
|
icon="mdi:weather-sunny",
|
||||||
native_unit_of_measurement=None,
|
|
||||||
),
|
),
|
||||||
SensorEntityDescription(
|
SensorEntityDescription(
|
||||||
key=TYPE_MAX_UV_INDEX,
|
key=TYPE_MAX_UV_INDEX,
|
||||||
|
@ -111,23 +110,14 @@ async def async_setup_entry(
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Set up a OpenUV sensor based on a config entry."""
|
"""Set up a OpenUV sensor based on a config entry."""
|
||||||
openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
|
||||||
|
async_add_entities(
|
||||||
entities = [OpenUvSensor(openuv, description) for description in SENSOR_TYPES]
|
[OpenUvSensor(openuv, description) for description in SENSOR_DESCRIPTIONS]
|
||||||
async_add_entities(entities, True)
|
)
|
||||||
|
|
||||||
|
|
||||||
class OpenUvSensor(OpenUvEntity, SensorEntity):
|
class OpenUvSensor(OpenUvEntity, SensorEntity):
|
||||||
"""Define a binary sensor for OpenUV."""
|
"""Define a binary sensor for OpenUV."""
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
openuv: OpenUV,
|
|
||||||
description: SensorEntityDescription,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the sensor."""
|
|
||||||
super().__init__(openuv, description.key)
|
|
||||||
self.entity_description = description
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def update_from_latest_data(self) -> None:
|
def update_from_latest_data(self) -> None:
|
||||||
"""Update the state."""
|
"""Update the state."""
|
||||||
|
@ -139,11 +129,11 @@ class OpenUvSensor(OpenUvEntity, SensorEntity):
|
||||||
|
|
||||||
self._attr_available = True
|
self._attr_available = True
|
||||||
|
|
||||||
if self._sensor_type == TYPE_CURRENT_OZONE_LEVEL:
|
if self.entity_description.key == TYPE_CURRENT_OZONE_LEVEL:
|
||||||
self._attr_native_value = data["ozone"]
|
self._attr_native_value = data["ozone"]
|
||||||
elif self._sensor_type == TYPE_CURRENT_UV_INDEX:
|
elif self.entity_description.key == TYPE_CURRENT_UV_INDEX:
|
||||||
self._attr_native_value = data["uv"]
|
self._attr_native_value = data["uv"]
|
||||||
elif self._sensor_type == TYPE_CURRENT_UV_LEVEL:
|
elif self.entity_description.key == TYPE_CURRENT_UV_LEVEL:
|
||||||
if data["uv"] >= 11:
|
if data["uv"] >= 11:
|
||||||
self._attr_native_value = UV_LEVEL_EXTREME
|
self._attr_native_value = UV_LEVEL_EXTREME
|
||||||
elif data["uv"] >= 8:
|
elif data["uv"] >= 8:
|
||||||
|
@ -154,14 +144,14 @@ class OpenUvSensor(OpenUvEntity, SensorEntity):
|
||||||
self._attr_native_value = UV_LEVEL_MODERATE
|
self._attr_native_value = UV_LEVEL_MODERATE
|
||||||
else:
|
else:
|
||||||
self._attr_native_value = UV_LEVEL_LOW
|
self._attr_native_value = UV_LEVEL_LOW
|
||||||
elif self._sensor_type == TYPE_MAX_UV_INDEX:
|
elif self.entity_description.key == TYPE_MAX_UV_INDEX:
|
||||||
self._attr_native_value = data["uv_max"]
|
self._attr_native_value = data["uv_max"]
|
||||||
uv_max_time = parse_datetime(data["uv_max_time"])
|
uv_max_time = parse_datetime(data["uv_max_time"])
|
||||||
if uv_max_time:
|
if uv_max_time:
|
||||||
self._attr_extra_state_attributes.update(
|
self._attr_extra_state_attributes.update(
|
||||||
{ATTR_MAX_UV_TIME: as_local(uv_max_time)}
|
{ATTR_MAX_UV_TIME: as_local(uv_max_time)}
|
||||||
)
|
)
|
||||||
elif self._sensor_type in (
|
elif self.entity_description.key in (
|
||||||
TYPE_SAFE_EXPOSURE_TIME_1,
|
TYPE_SAFE_EXPOSURE_TIME_1,
|
||||||
TYPE_SAFE_EXPOSURE_TIME_2,
|
TYPE_SAFE_EXPOSURE_TIME_2,
|
||||||
TYPE_SAFE_EXPOSURE_TIME_3,
|
TYPE_SAFE_EXPOSURE_TIME_3,
|
||||||
|
@ -170,5 +160,5 @@ class OpenUvSensor(OpenUvEntity, SensorEntity):
|
||||||
TYPE_SAFE_EXPOSURE_TIME_6,
|
TYPE_SAFE_EXPOSURE_TIME_6,
|
||||||
):
|
):
|
||||||
self._attr_native_value = data["safe_exposure_time"][
|
self._attr_native_value = data["safe_exposure_time"][
|
||||||
EXPOSURE_TYPE_MAP[self._sensor_type]
|
EXPOSURE_TYPE_MAP[self.entity_description.key]
|
||||||
]
|
]
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue