diff --git a/homeassistant/components/openuv/__init__.py b/homeassistant/components/openuv/__init__.py index d931049bb31..d14760d6cb1 100644 --- a/homeassistant/components/openuv/__init__.py +++ b/homeassistant/components/openuv/__init__.py @@ -24,7 +24,7 @@ from homeassistant.helpers.dispatcher import ( async_dispatcher_connect, 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 .const import ( @@ -175,14 +175,14 @@ class OpenUV: class OpenUvEntity(Entity): """Define a generic OpenUV entity.""" - def __init__(self, openuv: OpenUV, sensor_type: str) -> None: + def __init__(self, openuv: OpenUV, description: EntityDescription) -> None: """Initialize.""" self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION} self._attr_should_poll = False 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 async def async_added_to_hass(self) -> None: diff --git a/homeassistant/components/openuv/binary_sensor.py b/homeassistant/components/openuv/binary_sensor.py index 12b1f0c82af..a632d212abd 100644 --- a/homeassistant/components/openuv/binary_sensor.py +++ b/homeassistant/components/openuv/binary_sensor.py @@ -1,11 +1,14 @@ """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.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util.dt import as_local, parse_datetime, utcnow -from . import OpenUV, OpenUvEntity +from . import OpenUvEntity from .const import ( DATA_CLIENT, 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_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( @@ -27,25 +34,14 @@ async def async_setup_entry( ) -> None: """Set up an OpenUV sensor based on a config entry.""" openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id] - - binary_sensors = [] - for kind, attrs in BINARY_SENSORS.items(): - name, icon = attrs - binary_sensors.append(OpenUvBinarySensor(openuv, kind, name, icon)) - - async_add_entities(binary_sensors, True) + async_add_entities( + [OpenUvBinarySensor(openuv, BINARY_SENSOR_DESCRIPTION_PROTECTION_WINDOW)] + ) class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity): """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 def update_from_latest_data(self) -> None: """Update the state.""" @@ -62,7 +58,7 @@ class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity): LOGGER.info("Skipping update due to missing data: %s", key) return - if self._sensor_type == TYPE_PROTECTION_WINDOW: + if self.entity_description.key == TYPE_PROTECTION_WINDOW: from_dt = parse_datetime(data["from_time"]) to_dt = parse_datetime(data["to_time"]) diff --git a/homeassistant/components/openuv/sensor.py b/homeassistant/components/openuv/sensor.py index 9769245c48f..bb04bda4cb4 100644 --- a/homeassistant/components/openuv/sensor.py +++ b/homeassistant/components/openuv/sensor.py @@ -8,7 +8,7 @@ from homeassistant.core import HomeAssistant, callback from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.util.dt import as_local, parse_datetime -from . import OpenUV, OpenUvEntity +from . import OpenUvEntity from .const import ( DATA_CLIENT, DATA_UV, @@ -42,7 +42,7 @@ UV_LEVEL_HIGH = "High" UV_LEVEL_MODERATE = "Moderate" UV_LEVEL_LOW = "Low" -SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( +SENSOR_DESCRIPTIONS = ( SensorEntityDescription( key=TYPE_CURRENT_OZONE_LEVEL, name="Current Ozone Level", @@ -59,7 +59,6 @@ SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( key=TYPE_CURRENT_UV_LEVEL, name="Current UV Level", icon="mdi:weather-sunny", - native_unit_of_measurement=None, ), SensorEntityDescription( key=TYPE_MAX_UV_INDEX, @@ -111,23 +110,14 @@ async def async_setup_entry( ) -> None: """Set up a OpenUV sensor based on a config entry.""" openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id] - - entities = [OpenUvSensor(openuv, description) for description in SENSOR_TYPES] - async_add_entities(entities, True) + async_add_entities( + [OpenUvSensor(openuv, description) for description in SENSOR_DESCRIPTIONS] + ) class OpenUvSensor(OpenUvEntity, SensorEntity): """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 def update_from_latest_data(self) -> None: """Update the state.""" @@ -139,11 +129,11 @@ class OpenUvSensor(OpenUvEntity, SensorEntity): 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"] - elif self._sensor_type == TYPE_CURRENT_UV_INDEX: + elif self.entity_description.key == TYPE_CURRENT_UV_INDEX: 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: self._attr_native_value = UV_LEVEL_EXTREME elif data["uv"] >= 8: @@ -154,14 +144,14 @@ class OpenUvSensor(OpenUvEntity, SensorEntity): self._attr_native_value = UV_LEVEL_MODERATE else: 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"] uv_max_time = parse_datetime(data["uv_max_time"]) if uv_max_time: self._attr_extra_state_attributes.update( {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_2, TYPE_SAFE_EXPOSURE_TIME_3, @@ -170,5 +160,5 @@ class OpenUvSensor(OpenUvEntity, SensorEntity): TYPE_SAFE_EXPOSURE_TIME_6, ): self._attr_native_value = data["safe_exposure_time"][ - EXPOSURE_TYPE_MAP[self._sensor_type] + EXPOSURE_TYPE_MAP[self.entity_description.key] ]