2019-02-14 04:35:12 +00:00
|
|
|
"""Support for UV data from openuv.io."""
|
2018-08-02 05:42:12 +00:00
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-09-04 07:22:44 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT
|
2018-08-02 05:42:12 +00:00
|
|
|
from homeassistant.const import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_ATTRIBUTION,
|
|
|
|
CONF_API_KEY,
|
|
|
|
CONF_BINARY_SENSORS,
|
|
|
|
CONF_ELEVATION,
|
|
|
|
CONF_LATITUDE,
|
|
|
|
CONF_LONGITUDE,
|
|
|
|
CONF_MONITORED_CONDITIONS,
|
|
|
|
CONF_SENSORS,
|
|
|
|
)
|
2018-10-15 19:21:21 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2018-09-04 07:22:44 +00:00
|
|
|
from homeassistant.helpers import aiohttp_client, config_validation as cv
|
2018-08-02 05:42:12 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
|
|
|
from homeassistant.helpers.entity import Entity
|
2019-04-13 22:50:19 +00:00
|
|
|
from homeassistant.helpers.service import verify_domain_control
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2018-09-04 07:22:44 +00:00
|
|
|
from .config_flow import configured_instances
|
2019-01-14 12:12:06 +00:00
|
|
|
from .const import DOMAIN
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2018-09-04 07:22:44 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_OPENUV_CLIENT = "data_client"
|
|
|
|
DATA_OPENUV_LISTENER = "data_listener"
|
|
|
|
DATA_PROTECTION_WINDOW = "protection_window"
|
|
|
|
DATA_UV = "uv"
|
|
|
|
|
|
|
|
DEFAULT_ATTRIBUTION = "Data provided by OpenUV"
|
|
|
|
|
|
|
|
NOTIFICATION_ID = "openuv_notification"
|
|
|
|
NOTIFICATION_TITLE = "OpenUV Component Setup"
|
|
|
|
|
|
|
|
TOPIC_UPDATE = "{0}_data_update".format(DOMAIN)
|
|
|
|
|
|
|
|
TYPE_CURRENT_OZONE_LEVEL = "current_ozone_level"
|
|
|
|
TYPE_CURRENT_UV_INDEX = "current_uv_index"
|
|
|
|
TYPE_CURRENT_UV_LEVEL = "current_uv_level"
|
|
|
|
TYPE_MAX_UV_INDEX = "max_uv_index"
|
|
|
|
TYPE_PROTECTION_WINDOW = "uv_protection_window"
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_1 = "safe_exposure_time_type_1"
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_2 = "safe_exposure_time_type_2"
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_3 = "safe_exposure_time_type_3"
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_4 = "safe_exposure_time_type_4"
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_5 = "safe_exposure_time_type_5"
|
|
|
|
TYPE_SAFE_EXPOSURE_TIME_6 = "safe_exposure_time_type_6"
|
|
|
|
|
|
|
|
BINARY_SENSORS = {TYPE_PROTECTION_WINDOW: ("Protection Window", "mdi:sunglasses")}
|
|
|
|
|
|
|
|
BINARY_SENSOR_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(BINARY_SENSORS)): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(BINARY_SENSORS)]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
2018-08-02 05:42:12 +00:00
|
|
|
|
|
|
|
SENSORS = {
|
2019-07-31 19:25:30 +00:00
|
|
|
TYPE_CURRENT_OZONE_LEVEL: ("Current Ozone Level", "mdi:vector-triangle", "du"),
|
|
|
|
TYPE_CURRENT_UV_INDEX: ("Current UV Index", "mdi:weather-sunny", "index"),
|
|
|
|
TYPE_CURRENT_UV_LEVEL: ("Current UV Level", "mdi:weather-sunny", None),
|
|
|
|
TYPE_MAX_UV_INDEX: ("Max UV Index", "mdi:weather-sunny", "index"),
|
2018-08-02 05:42:12 +00:00
|
|
|
TYPE_SAFE_EXPOSURE_TIME_1: (
|
2019-07-31 19:25:30 +00:00
|
|
|
"Skin Type 1 Safe Exposure Time",
|
|
|
|
"mdi:timer",
|
|
|
|
"minutes",
|
|
|
|
),
|
2018-08-02 05:42:12 +00:00
|
|
|
TYPE_SAFE_EXPOSURE_TIME_2: (
|
2019-07-31 19:25:30 +00:00
|
|
|
"Skin Type 2 Safe Exposure Time",
|
|
|
|
"mdi:timer",
|
|
|
|
"minutes",
|
|
|
|
),
|
2018-08-02 05:42:12 +00:00
|
|
|
TYPE_SAFE_EXPOSURE_TIME_3: (
|
2019-07-31 19:25:30 +00:00
|
|
|
"Skin Type 3 Safe Exposure Time",
|
|
|
|
"mdi:timer",
|
|
|
|
"minutes",
|
|
|
|
),
|
2018-08-02 05:42:12 +00:00
|
|
|
TYPE_SAFE_EXPOSURE_TIME_4: (
|
2019-07-31 19:25:30 +00:00
|
|
|
"Skin Type 4 Safe Exposure Time",
|
|
|
|
"mdi:timer",
|
|
|
|
"minutes",
|
|
|
|
),
|
2018-08-02 05:42:12 +00:00
|
|
|
TYPE_SAFE_EXPOSURE_TIME_5: (
|
2019-07-31 19:25:30 +00:00
|
|
|
"Skin Type 5 Safe Exposure Time",
|
|
|
|
"mdi:timer",
|
|
|
|
"minutes",
|
|
|
|
),
|
2018-08-02 05:42:12 +00:00
|
|
|
TYPE_SAFE_EXPOSURE_TIME_6: (
|
2019-07-31 19:25:30 +00:00
|
|
|
"Skin Type 6 Safe Exposure Time",
|
|
|
|
"mdi:timer",
|
|
|
|
"minutes",
|
|
|
|
),
|
2018-08-02 05:42:12 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SENSORS)): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSORS)]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Optional(CONF_ELEVATION): float,
|
|
|
|
vol.Optional(CONF_LATITUDE): cv.latitude,
|
|
|
|
vol.Optional(CONF_LONGITUDE): cv.longitude,
|
|
|
|
vol.Optional(CONF_BINARY_SENSORS, default={}): BINARY_SENSOR_SCHEMA,
|
|
|
|
vol.Optional(CONF_SENSORS, default={}): SENSOR_SCHEMA,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2018-08-02 05:42:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup(hass, config):
|
|
|
|
"""Set up the OpenUV component."""
|
2018-09-04 07:22:44 +00:00
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
hass.data[DOMAIN][DATA_OPENUV_CLIENT] = {}
|
|
|
|
hass.data[DOMAIN][DATA_OPENUV_LISTENER] = {}
|
|
|
|
|
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
2018-08-02 05:42:12 +00:00
|
|
|
|
|
|
|
conf = config[DOMAIN]
|
2018-09-04 07:22:44 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
identifier = "{0}, {1}".format(
|
2018-10-27 17:07:08 +00:00
|
|
|
conf.get(CONF_LATITUDE, hass.config.latitude),
|
2019-07-31 19:25:30 +00:00
|
|
|
conf.get(CONF_LONGITUDE, hass.config.longitude),
|
|
|
|
)
|
2018-10-15 19:21:21 +00:00
|
|
|
if identifier in configured_instances(hass):
|
|
|
|
return True
|
2018-09-04 07:22:44 +00:00
|
|
|
|
2018-10-27 17:07:08 +00:00
|
|
|
data = {
|
|
|
|
CONF_API_KEY: conf[CONF_API_KEY],
|
|
|
|
CONF_BINARY_SENSORS: conf[CONF_BINARY_SENSORS],
|
|
|
|
CONF_SENSORS: conf[CONF_SENSORS],
|
|
|
|
}
|
|
|
|
|
|
|
|
if CONF_LATITUDE in conf:
|
|
|
|
data[CONF_LATITUDE] = conf[CONF_LATITUDE]
|
|
|
|
if CONF_LONGITUDE in conf:
|
|
|
|
data[CONF_LONGITUDE] = conf[CONF_LONGITUDE]
|
|
|
|
if CONF_ELEVATION in conf:
|
|
|
|
data[CONF_ELEVATION] = conf[CONF_ELEVATION]
|
|
|
|
|
2018-10-15 19:21:21 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=data
|
|
|
|
)
|
|
|
|
)
|
2018-09-04 07:22:44 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry):
|
|
|
|
"""Set up OpenUV as config entry."""
|
|
|
|
from pyopenuv import Client
|
|
|
|
from pyopenuv.errors import OpenUvError
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2019-04-13 22:50:19 +00:00
|
|
|
_verify_domain_control = verify_domain_control(hass, DOMAIN)
|
|
|
|
|
2018-08-02 05:42:12 +00:00
|
|
|
try:
|
|
|
|
websession = aiohttp_client.async_get_clientsession(hass)
|
|
|
|
openuv = OpenUV(
|
|
|
|
Client(
|
2018-09-04 07:22:44 +00:00
|
|
|
config_entry.data[CONF_API_KEY],
|
2018-10-27 17:07:08 +00:00
|
|
|
config_entry.data.get(CONF_LATITUDE, hass.config.latitude),
|
|
|
|
config_entry.data.get(CONF_LONGITUDE, hass.config.longitude),
|
2018-09-04 07:22:44 +00:00
|
|
|
websession,
|
2019-07-31 19:25:30 +00:00
|
|
|
altitude=config_entry.data.get(CONF_ELEVATION, hass.config.elevation),
|
|
|
|
),
|
2018-09-04 07:22:44 +00:00
|
|
|
config_entry.data.get(CONF_BINARY_SENSORS, {}).get(
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_MONITORED_CONDITIONS, list(BINARY_SENSORS)
|
|
|
|
),
|
2018-09-04 07:22:44 +00:00
|
|
|
config_entry.data.get(CONF_SENSORS, {}).get(
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_MONITORED_CONDITIONS, list(SENSORS)
|
|
|
|
),
|
|
|
|
)
|
2018-08-02 05:42:12 +00:00
|
|
|
await openuv.async_update()
|
2018-09-04 07:22:44 +00:00
|
|
|
hass.data[DOMAIN][DATA_OPENUV_CLIENT][config_entry.entry_id] = openuv
|
2018-08-02 05:42:12 +00:00
|
|
|
except OpenUvError as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Config entry failed: %s", err)
|
2018-10-15 19:21:21 +00:00
|
|
|
raise ConfigEntryNotReady
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
for component in ("binary_sensor", "sensor"):
|
2018-10-27 17:07:08 +00:00
|
|
|
hass.async_create_task(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.config_entries.async_forward_entry_setup(config_entry, component)
|
|
|
|
)
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2019-04-13 22:50:19 +00:00
|
|
|
@_verify_domain_control
|
2019-01-14 12:12:06 +00:00
|
|
|
async def update_data(service):
|
2018-08-02 05:42:12 +00:00
|
|
|
"""Refresh OpenUV data."""
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("Refreshing OpenUV data")
|
2019-05-02 03:15:10 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
await openuv.async_update()
|
|
|
|
except OpenUvError as err:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("Error during data update: %s", err)
|
2019-05-02 03:15:10 +00:00
|
|
|
return
|
|
|
|
|
2018-08-02 05:42:12 +00:00
|
|
|
async_dispatcher_send(hass, TOPIC_UPDATE)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.services.async_register(DOMAIN, "update_data", update_data)
|
2018-09-04 07:22:44 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, config_entry):
|
|
|
|
"""Unload an OpenUV config entry."""
|
|
|
|
hass.data[DOMAIN][DATA_OPENUV_CLIENT].pop(config_entry.entry_id)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
for component in ("binary_sensor", "sensor"):
|
|
|
|
await hass.config_entries.async_forward_entry_unload(config_entry, component)
|
2018-10-15 19:21:21 +00:00
|
|
|
|
2018-08-02 05:42:12 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class OpenUV:
|
|
|
|
"""Define a generic OpenUV object."""
|
|
|
|
|
2018-09-04 07:22:44 +00:00
|
|
|
def __init__(self, client, binary_sensor_conditions, sensor_conditions):
|
2018-08-02 05:42:12 +00:00
|
|
|
"""Initialize."""
|
2018-09-04 07:22:44 +00:00
|
|
|
self.binary_sensor_conditions = binary_sensor_conditions
|
2018-08-02 05:42:12 +00:00
|
|
|
self.client = client
|
|
|
|
self.data = {}
|
2018-09-04 07:22:44 +00:00
|
|
|
self.sensor_conditions = sensor_conditions
|
2018-08-02 05:42:12 +00:00
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Update sensor/binary sensor data."""
|
2018-09-04 07:22:44 +00:00
|
|
|
if TYPE_PROTECTION_WINDOW in self.binary_sensor_conditions:
|
2018-10-02 08:34:12 +00:00
|
|
|
resp = await self.client.uv_protection_window()
|
2019-07-31 19:25:30 +00:00
|
|
|
data = resp["result"]
|
2018-10-02 08:34:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if data.get("from_time") and data.get("to_time"):
|
2018-10-02 08:34:12 +00:00
|
|
|
self.data[DATA_PROTECTION_WINDOW] = data
|
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.debug("No valid protection window data for this location")
|
2018-10-02 08:34:12 +00:00
|
|
|
self.data[DATA_PROTECTION_WINDOW] = {}
|
2018-08-02 05:42:12 +00:00
|
|
|
|
2018-09-04 07:22:44 +00:00
|
|
|
if any(c in self.sensor_conditions for c in SENSORS):
|
2018-08-02 05:42:12 +00:00
|
|
|
data = await self.client.uv_index()
|
|
|
|
self.data[DATA_UV] = data
|
|
|
|
|
|
|
|
|
|
|
|
class OpenUvEntity(Entity):
|
|
|
|
"""Define a generic OpenUV entity."""
|
|
|
|
|
|
|
|
def __init__(self, openuv):
|
|
|
|
"""Initialize."""
|
|
|
|
self._attrs = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
|
|
|
self._name = None
|
|
|
|
self.openuv = openuv
|
|
|
|
|
|
|
|
@property
|
|
|
|
def device_state_attributes(self):
|
|
|
|
"""Return the state attributes."""
|
|
|
|
return self._attrs
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the entity."""
|
|
|
|
return self._name
|