Use current config entry standards for OpenUV (#57137)

pull/57149/head
Aaron Bach 2021-10-05 15:03:39 -06:00 committed by GitHub
parent eba7cad33f
commit 25253f2b7a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 26 deletions

View File

@ -50,7 +50,7 @@ TOPIC_UPDATE = f"{DOMAIN}_data_update"
PLATFORMS = ["binary_sensor", "sensor"] PLATFORMS = ["binary_sensor", "sensor"]
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up OpenUV as config entry.""" """Set up OpenUV as config entry."""
hass.data.setdefault(DOMAIN, {DATA_CLIENT: {}, DATA_LISTENER: {}}) hass.data.setdefault(DOMAIN, {DATA_CLIENT: {}, DATA_LISTENER: {}})
@ -59,22 +59,22 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
try: try:
websession = aiohttp_client.async_get_clientsession(hass) websession = aiohttp_client.async_get_clientsession(hass)
openuv = OpenUV( openuv = OpenUV(
config_entry, entry,
Client( Client(
config_entry.data[CONF_API_KEY], entry.data[CONF_API_KEY],
config_entry.data.get(CONF_LATITUDE, hass.config.latitude), entry.data.get(CONF_LATITUDE, hass.config.latitude),
config_entry.data.get(CONF_LONGITUDE, hass.config.longitude), entry.data.get(CONF_LONGITUDE, hass.config.longitude),
altitude=config_entry.data.get(CONF_ELEVATION, hass.config.elevation), altitude=entry.data.get(CONF_ELEVATION, hass.config.elevation),
session=websession, session=websession,
), ),
) )
await openuv.async_update() await openuv.async_update()
hass.data[DOMAIN][DATA_CLIENT][config_entry.entry_id] = openuv hass.data[DOMAIN][DATA_CLIENT][entry.entry_id] = openuv
except OpenUvError as err: except OpenUvError as err:
LOGGER.error("Config entry failed: %s", err) LOGGER.error("Config entry failed: %s", err)
raise ConfigEntryNotReady from err raise ConfigEntryNotReady from err
hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) hass.config_entries.async_setup_platforms(entry, PLATFORMS)
@_verify_domain_control @_verify_domain_control
async def update_data(_: ServiceCall) -> None: async def update_data(_: ServiceCall) -> None:
@ -107,21 +107,19 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
return True return True
async def async_unload_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Unload an OpenUV config entry.""" """Unload an OpenUV config entry."""
unload_ok = await hass.config_entries.async_unload_platforms( unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
config_entry, PLATFORMS
)
if unload_ok: if unload_ok:
hass.data[DOMAIN][DATA_CLIENT].pop(config_entry.entry_id) hass.data[DOMAIN][DATA_CLIENT].pop(entry.entry_id)
return unload_ok return unload_ok
async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool: async def async_migrate_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Migrate the config entry upon new versions.""" """Migrate the config entry upon new versions."""
version = config_entry.version version = entry.version
data = {**config_entry.data} data = {**entry.data}
LOGGER.debug("Migrating from version %s", version) LOGGER.debug("Migrating from version %s", version)
@ -129,8 +127,8 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
if version == 1: if version == 1:
data.pop(CONF_BINARY_SENSORS, None) data.pop(CONF_BINARY_SENSORS, None)
data.pop(CONF_SENSORS, None) data.pop(CONF_SENSORS, None)
version = config_entry.version = 2 version = entry.version = 2
hass.config_entries.async_update_entry(config_entry, data=data) hass.config_entries.async_update_entry(entry, data=data)
LOGGER.debug("Migration to version %s successful", version) LOGGER.debug("Migration to version %s successful", version)
return True return True
@ -139,16 +137,16 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
class OpenUV: class OpenUV:
"""Define a generic OpenUV object.""" """Define a generic OpenUV object."""
def __init__(self, config_entry: ConfigEntry, client: Client) -> None: def __init__(self, entry: ConfigEntry, client: Client) -> None:
"""Initialize.""" """Initialize."""
self._config_entry = config_entry self._entry = entry
self.client = client self.client = client
self.data: dict[str, Any] = {} self.data: dict[str, Any] = {}
async def async_update_protection_data(self) -> None: async def async_update_protection_data(self) -> None:
"""Update binary sensor (protection window) data.""" """Update binary sensor (protection window) data."""
low = self._config_entry.options.get(CONF_FROM_WINDOW, DEFAULT_FROM_WINDOW) low = self._entry.options.get(CONF_FROM_WINDOW, DEFAULT_FROM_WINDOW)
high = self._config_entry.options.get(CONF_TO_WINDOW, DEFAULT_TO_WINDOW) high = self._entry.options.get(CONF_TO_WINDOW, DEFAULT_TO_WINDOW)
try: try:
resp = await self.client.uv_protection_window(low=low, high=high) resp = await self.client.uv_protection_window(low=low, high=high)

View File

@ -90,9 +90,9 @@ class OpenUvFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
class OpenUvOptionsFlowHandler(config_entries.OptionsFlow): class OpenUvOptionsFlowHandler(config_entries.OptionsFlow):
"""Handle a OpenUV options flow.""" """Handle a OpenUV options flow."""
def __init__(self, config_entry: ConfigEntry) -> None: def __init__(self, entry: ConfigEntry) -> None:
"""Initialize.""" """Initialize."""
self.config_entry = config_entry self.entry = entry
async def async_step_init( async def async_step_init(
self, user_input: dict[str, Any] | None = None self, user_input: dict[str, Any] | None = None
@ -108,7 +108,7 @@ class OpenUvOptionsFlowHandler(config_entries.OptionsFlow):
vol.Optional( vol.Optional(
CONF_FROM_WINDOW, CONF_FROM_WINDOW,
description={ description={
"suggested_value": self.config_entry.options.get( "suggested_value": self.entry.options.get(
CONF_FROM_WINDOW, DEFAULT_FROM_WINDOW CONF_FROM_WINDOW, DEFAULT_FROM_WINDOW
) )
}, },
@ -116,7 +116,7 @@ class OpenUvOptionsFlowHandler(config_entries.OptionsFlow):
vol.Optional( vol.Optional(
CONF_TO_WINDOW, CONF_TO_WINDOW,
description={ description={
"suggested_value": self.config_entry.options.get( "suggested_value": self.entry.options.get(
CONF_FROM_WINDOW, DEFAULT_TO_WINDOW CONF_FROM_WINDOW, DEFAULT_TO_WINDOW
) )
}, },