Re-organize OpenUV constants (#43453)

Co-authored-by: Franck Nijhof <git@frenck.dev>
This commit is contained in:
Aaron Bach 2020-11-22 04:50:22 -07:00 committed by GitHub
parent 7319c4692d
commit be39104a36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 50 additions and 50 deletions

View File

@ -1,6 +1,5 @@
"""Support for UV data from openuv.io."""
import asyncio
import logging
from pyopenuv import Client
from pyopenuv.errors import OpenUvError
@ -24,14 +23,14 @@ from homeassistant.helpers.dispatcher import (
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.service import verify_domain_control
from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
DATA_OPENUV_CLIENT = "data_client"
DATA_OPENUV_LISTENER = "data_listener"
DATA_PROTECTION_WINDOW = "protection_window"
DATA_UV = "uv"
from .const import (
DATA_CLIENT,
DATA_LISTENER,
DATA_PROTECTION_WINDOW,
DATA_UV,
DOMAIN,
LOGGER,
)
DEFAULT_ATTRIBUTION = "Data provided by OpenUV"
@ -40,24 +39,12 @@ NOTIFICATION_TITLE = "OpenUV Component Setup"
TOPIC_UPDATE = f"{DOMAIN}_data_update"
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"
PLATFORMS = ["binary_sensor", "sensor"]
async def async_setup(hass, config):
"""Set up the OpenUV component."""
hass.data[DOMAIN] = {DATA_OPENUV_CLIENT: {}, DATA_OPENUV_LISTENER: {}}
hass.data[DOMAIN] = {DATA_CLIENT: {}, DATA_LISTENER: {}}
return True
@ -77,9 +64,9 @@ async def async_setup_entry(hass, config_entry):
)
)
await openuv.async_update()
hass.data[DOMAIN][DATA_OPENUV_CLIENT][config_entry.entry_id] = openuv
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = openuv
except OpenUvError as err:
_LOGGER.error("Config entry failed: %s", err)
LOGGER.error("Config entry failed: %s", err)
raise ConfigEntryNotReady from err
for component in PLATFORMS:
@ -90,21 +77,21 @@ async def async_setup_entry(hass, config_entry):
@_verify_domain_control
async def update_data(service):
"""Refresh all OpenUV data."""
_LOGGER.debug("Refreshing all OpenUV data")
LOGGER.debug("Refreshing all OpenUV data")
await openuv.async_update()
async_dispatcher_send(hass, TOPIC_UPDATE)
@_verify_domain_control
async def update_uv_index_data(service):
"""Refresh OpenUV UV index data."""
_LOGGER.debug("Refreshing OpenUV UV index data")
LOGGER.debug("Refreshing OpenUV UV index data")
await openuv.async_update_uv_index_data()
async_dispatcher_send(hass, TOPIC_UPDATE)
@_verify_domain_control
async def update_protection_data(service):
"""Refresh OpenUV protection window data."""
_LOGGER.debug("Refreshing OpenUV protection window data")
LOGGER.debug("Refreshing OpenUV protection window data")
await openuv.async_update_protection_data()
async_dispatcher_send(hass, TOPIC_UPDATE)
@ -129,7 +116,7 @@ async def async_unload_entry(hass, config_entry):
)
)
if unload_ok:
hass.data[DOMAIN][DATA_OPENUV_CLIENT].pop(config_entry.entry_id)
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id)
return unload_ok
@ -139,7 +126,7 @@ async def async_migrate_entry(hass, config_entry):
version = config_entry.version
data = {**config_entry.data}
_LOGGER.debug("Migrating from version %s", version)
LOGGER.debug("Migrating from version %s", version)
# 1 -> 2: Remove unused condition data:
if version == 1:
@ -147,7 +134,7 @@ async def async_migrate_entry(hass, config_entry):
data.pop(CONF_SENSORS, None)
version = config_entry.version = 2
hass.config_entries.async_update_entry(config_entry, data=data)
_LOGGER.debug("Migration to version %s successful", version)
LOGGER.debug("Migration to version %s successful", version)
return True
@ -166,7 +153,7 @@ class OpenUV:
resp = await self.client.uv_protection_window()
self.data[DATA_PROTECTION_WINDOW] = resp["result"]
except OpenUvError as err:
_LOGGER.error("Error during protection data update: %s", err)
LOGGER.error("Error during protection data update: %s", err)
self.data[DATA_PROTECTION_WINDOW] = {}
async def async_update_uv_index_data(self):
@ -175,7 +162,7 @@ class OpenUV:
data = await self.client.uv_index()
self.data[DATA_UV] = data
except OpenUvError as err:
_LOGGER.error("Error during uv index data update: %s", err)
LOGGER.error("Error during uv index data update: %s", err)
self.data[DATA_UV] = {}
async def async_update(self):

View File

@ -1,20 +1,17 @@
"""Support for OpenUV binary sensors."""
import logging
from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.core import callback
from homeassistant.util.dt import as_local, parse_datetime, utcnow
from . import (
DATA_OPENUV_CLIENT,
from . import OpenUvEntity
from .const import (
DATA_CLIENT,
DATA_PROTECTION_WINDOW,
DOMAIN,
LOGGER,
TYPE_PROTECTION_WINDOW,
OpenUvEntity,
)
_LOGGER = logging.getLogger(__name__)
ATTR_PROTECTION_WINDOW_ENDING_TIME = "end_time"
ATTR_PROTECTION_WINDOW_ENDING_UV = "end_uv"
ATTR_PROTECTION_WINDOW_STARTING_TIME = "start_time"
@ -25,7 +22,7 @@ BINARY_SENSORS = {TYPE_PROTECTION_WINDOW: ("Protection Window", "mdi:sunglasses"
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up an OpenUV sensor based on a config entry."""
openuv = hass.data[DOMAIN][DATA_OPENUV_CLIENT][entry.entry_id]
openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
binary_sensors = []
for kind, attrs in BINARY_SENSORS.items():
@ -86,7 +83,7 @@ class OpenUvBinarySensor(OpenUvEntity, BinarySensorEntity):
for key in ("from_time", "to_time", "from_uv", "to_uv"):
if not data.get(key):
_LOGGER.info("Skipping update due to missing data: %s", key)
LOGGER.info("Skipping update due to missing data: %s", key)
return
if self._sensor_type == TYPE_PROTECTION_WINDOW:

View File

@ -1,2 +1,22 @@
"""Define constants for the OpenUV component."""
import logging
DOMAIN = "openuv"
LOGGER = logging.getLogger(__package__)
DATA_CLIENT = "data_client"
DATA_LISTENER = "data_listener"
DATA_PROTECTION_WINDOW = "protection_window"
DATA_UV = "uv"
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"

View File

@ -1,12 +1,11 @@
"""Support for OpenUV sensors."""
import logging
from homeassistant.const import TIME_MINUTES, UV_INDEX
from homeassistant.core import callback
from homeassistant.util.dt import as_local, parse_datetime
from . import (
DATA_OPENUV_CLIENT,
from . import OpenUvEntity
from .const import (
DATA_CLIENT,
DATA_UV,
DOMAIN,
TYPE_CURRENT_OZONE_LEVEL,
@ -19,11 +18,8 @@ from . import (
TYPE_SAFE_EXPOSURE_TIME_4,
TYPE_SAFE_EXPOSURE_TIME_5,
TYPE_SAFE_EXPOSURE_TIME_6,
OpenUvEntity,
)
_LOGGER = logging.getLogger(__name__)
ATTR_MAX_UV_TIME = "time"
EXPOSURE_TYPE_MAP = {
@ -80,8 +76,8 @@ SENSORS = {
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up a Nest sensor based on a config entry."""
openuv = hass.data[DOMAIN][DATA_OPENUV_CLIENT][entry.entry_id]
"""Set up a OpenUV sensor based on a config entry."""
openuv = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
sensors = []
for kind, attrs in SENSORS.items():