2019-02-14 04:35:12 +00:00
|
|
|
"""Support for OpenUV sensors."""
|
2021-07-24 12:50:01 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-23 14:54:06 +00:00
|
|
|
from homeassistant.components.sensor import (
|
2021-12-16 15:33:50 +00:00
|
|
|
SensorDeviceClass,
|
2021-09-23 14:54:06 +00:00
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
2021-12-16 15:33:50 +00:00
|
|
|
SensorStateClass,
|
2021-09-23 14:54:06 +00:00
|
|
|
)
|
2021-07-24 12:50:01 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-16 15:33:50 +00:00
|
|
|
from homeassistant.const import TIME_MINUTES, UV_INDEX
|
2021-07-24 12:50:01 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
from homeassistant.util.dt import as_local, parse_datetime
|
|
|
|
|
2021-08-25 12:18:53 +00:00
|
|
|
from . import OpenUvEntity
|
2020-11-22 11:50:22 +00:00
|
|
|
from .const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_UV,
|
|
|
|
DOMAIN,
|
|
|
|
TYPE_CURRENT_OZONE_LEVEL,
|
|
|
|
TYPE_CURRENT_UV_INDEX,
|
|
|
|
TYPE_CURRENT_UV_LEVEL,
|
|
|
|
TYPE_MAX_UV_INDEX,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_1,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_2,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_3,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_4,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_5,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_6,
|
|
|
|
)
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_MAX_UV_TIME = "time"
|
2018-08-02 05:42:12 +00:00
|
|
|
|
|
|
|
EXPOSURE_TYPE_MAP = {
|
2019-07-31 19:25:30 +00:00
|
|
|
TYPE_SAFE_EXPOSURE_TIME_1: "st1",
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_2: "st2",
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_3: "st3",
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_4: "st4",
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_5: "st5",
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_6: "st6",
|
2018-08-02 05:42:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
UV_LEVEL_EXTREME = "Extreme"
|
|
|
|
UV_LEVEL_VHIGH = "Very High"
|
|
|
|
UV_LEVEL_HIGH = "High"
|
|
|
|
UV_LEVEL_MODERATE = "Moderate"
|
|
|
|
UV_LEVEL_LOW = "Low"
|
2018-08-20 12:22:41 +00:00
|
|
|
|
2021-08-25 12:18:53 +00:00
|
|
|
SENSOR_DESCRIPTIONS = (
|
2021-08-22 18:30:50 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_CURRENT_OZONE_LEVEL,
|
|
|
|
name="Current Ozone Level",
|
2021-12-16 15:33:50 +00:00
|
|
|
device_class=SensorDeviceClass.OZONE,
|
2021-08-22 18:30:50 +00:00
|
|
|
native_unit_of_measurement="du",
|
2021-12-16 15:33:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2020-01-23 01:48:20 +00:00
|
|
|
),
|
2021-08-22 18:30:50 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_CURRENT_UV_INDEX,
|
|
|
|
name="Current UV Index",
|
|
|
|
icon="mdi:weather-sunny",
|
|
|
|
native_unit_of_measurement=UV_INDEX,
|
2021-12-16 15:33:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2020-01-23 01:48:20 +00:00
|
|
|
),
|
2021-08-22 18:30:50 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_CURRENT_UV_LEVEL,
|
|
|
|
name="Current UV Level",
|
|
|
|
icon="mdi:weather-sunny",
|
2020-01-23 01:48:20 +00:00
|
|
|
),
|
2021-08-22 18:30:50 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_MAX_UV_INDEX,
|
|
|
|
name="Max UV Index",
|
|
|
|
icon="mdi:weather-sunny",
|
|
|
|
native_unit_of_measurement=UV_INDEX,
|
2021-12-16 15:33:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2020-01-23 01:48:20 +00:00
|
|
|
),
|
2021-08-22 18:30:50 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_SAFE_EXPOSURE_TIME_1,
|
|
|
|
name="Skin Type 1 Safe Exposure Time",
|
|
|
|
icon="mdi:timer-outline",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
2021-12-16 15:33:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2020-01-23 01:48:20 +00:00
|
|
|
),
|
2021-08-22 18:30:50 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_SAFE_EXPOSURE_TIME_2,
|
|
|
|
name="Skin Type 2 Safe Exposure Time",
|
|
|
|
icon="mdi:timer-outline",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
2021-12-16 15:33:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2020-01-23 01:48:20 +00:00
|
|
|
),
|
2021-08-22 18:30:50 +00:00
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_SAFE_EXPOSURE_TIME_3,
|
|
|
|
name="Skin Type 3 Safe Exposure Time",
|
|
|
|
icon="mdi:timer-outline",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
2021-12-16 15:33:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-22 18:30:50 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_SAFE_EXPOSURE_TIME_4,
|
|
|
|
name="Skin Type 4 Safe Exposure Time",
|
|
|
|
icon="mdi:timer-outline",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
2021-12-16 15:33:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-22 18:30:50 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_SAFE_EXPOSURE_TIME_5,
|
|
|
|
name="Skin Type 5 Safe Exposure Time",
|
|
|
|
icon="mdi:timer-outline",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
2021-12-16 15:33:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-22 18:30:50 +00:00
|
|
|
),
|
|
|
|
SensorEntityDescription(
|
|
|
|
key=TYPE_SAFE_EXPOSURE_TIME_6,
|
|
|
|
name="Skin Type 6 Safe Exposure Time",
|
|
|
|
icon="mdi:timer-outline",
|
|
|
|
native_unit_of_measurement=TIME_MINUTES,
|
2021-12-16 15:33:50 +00:00
|
|
|
state_class=SensorStateClass.MEASUREMENT,
|
2021-08-22 18:30:50 +00:00
|
|
|
),
|
|
|
|
)
|
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:
|
2020-11-22 11:50:22 +00:00
|
|
|
"""Set up a OpenUV sensor based on a config entry."""
|
2021-11-14 18:06:27 +00:00
|
|
|
openuv = hass.data[DOMAIN][entry.entry_id]
|
2021-08-25 12:18:53 +00:00
|
|
|
async_add_entities(
|
|
|
|
[OpenUvSensor(openuv, description) for description in SENSOR_DESCRIPTIONS]
|
|
|
|
)
|
2018-08-02 05:42:12 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class OpenUvSensor(OpenUvEntity, SensorEntity):
|
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."""
|
2021-10-18 16:36:35 +00:00
|
|
|
if not (data := self.openuv.data[DATA_UV].get("result")):
|
2021-07-02 07:47:49 +00:00
|
|
|
self._attr_available = False
|
2020-01-20 20:33:58 +00:00
|
|
|
return
|
|
|
|
|
2021-07-02 07:47:49 +00:00
|
|
|
self._attr_available = True
|
2020-01-20 20:33:58 +00:00
|
|
|
|
2021-08-25 12:18:53 +00:00
|
|
|
if self.entity_description.key == TYPE_CURRENT_OZONE_LEVEL:
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_value = data["ozone"]
|
2021-08-25 12:18:53 +00:00
|
|
|
elif self.entity_description.key == TYPE_CURRENT_UV_INDEX:
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_value = data["uv"]
|
2021-08-25 12:18:53 +00:00
|
|
|
elif self.entity_description.key == TYPE_CURRENT_UV_LEVEL:
|
2019-07-31 19:25:30 +00:00
|
|
|
if data["uv"] >= 11:
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_value = UV_LEVEL_EXTREME
|
2019-07-31 19:25:30 +00:00
|
|
|
elif data["uv"] >= 8:
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_value = UV_LEVEL_VHIGH
|
2019-07-31 19:25:30 +00:00
|
|
|
elif data["uv"] >= 6:
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_value = UV_LEVEL_HIGH
|
2019-07-31 19:25:30 +00:00
|
|
|
elif data["uv"] >= 3:
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_value = UV_LEVEL_MODERATE
|
2018-08-20 12:22:41 +00:00
|
|
|
else:
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_value = UV_LEVEL_LOW
|
2021-08-25 12:18:53 +00:00
|
|
|
elif self.entity_description.key == TYPE_MAX_UV_INDEX:
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_value = data["uv_max"]
|
2021-10-30 14:31:43 +00:00
|
|
|
if uv_max_time := parse_datetime(data["uv_max_time"]):
|
2021-07-24 12:50:01 +00:00
|
|
|
self._attr_extra_state_attributes.update(
|
|
|
|
{ATTR_MAX_UV_TIME: as_local(uv_max_time)}
|
|
|
|
)
|
2021-08-25 12:18:53 +00:00
|
|
|
elif self.entity_description.key in (
|
2019-07-31 19:25:30 +00:00
|
|
|
TYPE_SAFE_EXPOSURE_TIME_1,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_2,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_3,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_4,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_5,
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_6,
|
|
|
|
):
|
2021-08-12 11:26:17 +00:00
|
|
|
self._attr_native_value = data["safe_exposure_time"][
|
2021-08-25 12:18:53 +00:00
|
|
|
EXPOSURE_TYPE_MAP[self.entity_description.key]
|
2019-07-31 19:25:30 +00:00
|
|
|
]
|