Use current config entry standards for OpenUV (#57137)
parent
eba7cad33f
commit
25253f2b7a
|
@ -50,7 +50,7 @@ TOPIC_UPDATE = f"{DOMAIN}_data_update"
|
|||
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."""
|
||||
hass.data.setdefault(DOMAIN, {DATA_CLIENT: {}, DATA_LISTENER: {}})
|
||||
|
||||
|
@ -59,22 +59,22 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||
try:
|
||||
websession = aiohttp_client.async_get_clientsession(hass)
|
||||
openuv = OpenUV(
|
||||
config_entry,
|
||||
entry,
|
||||
Client(
|
||||
config_entry.data[CONF_API_KEY],
|
||||
config_entry.data.get(CONF_LATITUDE, hass.config.latitude),
|
||||
config_entry.data.get(CONF_LONGITUDE, hass.config.longitude),
|
||||
altitude=config_entry.data.get(CONF_ELEVATION, hass.config.elevation),
|
||||
entry.data[CONF_API_KEY],
|
||||
entry.data.get(CONF_LATITUDE, hass.config.latitude),
|
||||
entry.data.get(CONF_LONGITUDE, hass.config.longitude),
|
||||
altitude=entry.data.get(CONF_ELEVATION, hass.config.elevation),
|
||||
session=websession,
|
||||
),
|
||||
)
|
||||
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:
|
||||
LOGGER.error("Config entry failed: %s", 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
|
||||
async def update_data(_: ServiceCall) -> None:
|
||||
|
@ -107,21 +107,19 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
|
|||
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_ok = await hass.config_entries.async_unload_platforms(
|
||||
config_entry, PLATFORMS
|
||||
)
|
||||
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
||||
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
|
||||
|
||||
|
||||
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."""
|
||||
version = config_entry.version
|
||||
data = {**config_entry.data}
|
||||
version = entry.version
|
||||
data = {**entry.data}
|
||||
|
||||
LOGGER.debug("Migrating from version %s", version)
|
||||
|
||||
|
@ -129,8 +127,8 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
|
|||
if version == 1:
|
||||
data.pop(CONF_BINARY_SENSORS, None)
|
||||
data.pop(CONF_SENSORS, None)
|
||||
version = config_entry.version = 2
|
||||
hass.config_entries.async_update_entry(config_entry, data=data)
|
||||
version = entry.version = 2
|
||||
hass.config_entries.async_update_entry(entry, data=data)
|
||||
LOGGER.debug("Migration to version %s successful", version)
|
||||
|
||||
return True
|
||||
|
@ -139,16 +137,16 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) ->
|
|||
class OpenUV:
|
||||
"""Define a generic OpenUV object."""
|
||||
|
||||
def __init__(self, config_entry: ConfigEntry, client: Client) -> None:
|
||||
def __init__(self, entry: ConfigEntry, client: Client) -> None:
|
||||
"""Initialize."""
|
||||
self._config_entry = config_entry
|
||||
self._entry = entry
|
||||
self.client = client
|
||||
self.data: dict[str, Any] = {}
|
||||
|
||||
async def async_update_protection_data(self) -> None:
|
||||
"""Update binary sensor (protection window) data."""
|
||||
low = self._config_entry.options.get(CONF_FROM_WINDOW, DEFAULT_FROM_WINDOW)
|
||||
high = self._config_entry.options.get(CONF_TO_WINDOW, DEFAULT_TO_WINDOW)
|
||||
low = self._entry.options.get(CONF_FROM_WINDOW, DEFAULT_FROM_WINDOW)
|
||||
high = self._entry.options.get(CONF_TO_WINDOW, DEFAULT_TO_WINDOW)
|
||||
|
||||
try:
|
||||
resp = await self.client.uv_protection_window(low=low, high=high)
|
||||
|
|
|
@ -90,9 +90,9 @@ class OpenUvFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
class OpenUvOptionsFlowHandler(config_entries.OptionsFlow):
|
||||
"""Handle a OpenUV options flow."""
|
||||
|
||||
def __init__(self, config_entry: ConfigEntry) -> None:
|
||||
def __init__(self, entry: ConfigEntry) -> None:
|
||||
"""Initialize."""
|
||||
self.config_entry = config_entry
|
||||
self.entry = entry
|
||||
|
||||
async def async_step_init(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
|
@ -108,7 +108,7 @@ class OpenUvOptionsFlowHandler(config_entries.OptionsFlow):
|
|||
vol.Optional(
|
||||
CONF_FROM_WINDOW,
|
||||
description={
|
||||
"suggested_value": self.config_entry.options.get(
|
||||
"suggested_value": self.entry.options.get(
|
||||
CONF_FROM_WINDOW, DEFAULT_FROM_WINDOW
|
||||
)
|
||||
},
|
||||
|
@ -116,7 +116,7 @@ class OpenUvOptionsFlowHandler(config_entries.OptionsFlow):
|
|||
vol.Optional(
|
||||
CONF_TO_WINDOW,
|
||||
description={
|
||||
"suggested_value": self.config_entry.options.get(
|
||||
"suggested_value": self.entry.options.get(
|
||||
CONF_FROM_WINDOW, DEFAULT_TO_WINDOW
|
||||
)
|
||||
},
|
||||
|
|
Loading…
Reference in New Issue