2022-01-11 00:23:31 +00:00
|
|
|
"""The Homewizard integration."""
|
2023-01-15 18:14:19 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_REAUTH, ConfigEntry
|
2022-01-11 00:23:31 +00:00
|
|
|
from homeassistant.const import CONF_IP_ADDRESS
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
|
|
|
|
2022-01-21 09:44:56 +00:00
|
|
|
from .const import DOMAIN, PLATFORMS
|
2022-01-11 00:23:31 +00:00
|
|
|
from .coordinator import HWEnergyDeviceUpdateCoordinator as Coordinator
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Set up Homewizard from a config entry."""
|
2023-01-15 16:30:27 +00:00
|
|
|
coordinator = Coordinator(hass, entry, entry.data[CONF_IP_ADDRESS])
|
2022-01-11 00:23:31 +00:00
|
|
|
try:
|
2022-05-25 07:05:11 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
2022-12-16 15:53:54 +00:00
|
|
|
|
2022-05-25 07:05:11 +00:00
|
|
|
except ConfigEntryNotReady:
|
2023-01-20 13:48:48 +00:00
|
|
|
await coordinator.api.close()
|
2022-12-16 15:53:54 +00:00
|
|
|
|
|
|
|
if coordinator.api_disabled:
|
|
|
|
entry.async_start_reauth(hass)
|
|
|
|
|
2022-05-25 07:05:11 +00:00
|
|
|
raise
|
2022-01-11 00:23:31 +00:00
|
|
|
|
2023-01-16 08:23:03 +00:00
|
|
|
hass.data.setdefault(DOMAIN, {})[entry.entry_id] = coordinator
|
|
|
|
|
2022-12-16 15:53:54 +00:00
|
|
|
# Abort reauth config flow if active
|
|
|
|
for progress_flow in hass.config_entries.flow.async_progress_by_handler(DOMAIN):
|
2023-01-16 08:23:03 +00:00
|
|
|
if (
|
|
|
|
"context" in progress_flow
|
|
|
|
and progress_flow["context"].get("source") == SOURCE_REAUTH
|
|
|
|
):
|
2022-12-16 15:53:54 +00:00
|
|
|
hass.config_entries.flow.async_abort(progress_flow["flow_id"])
|
|
|
|
|
2022-01-11 00:23:31 +00:00
|
|
|
# Finalize
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
2022-01-11 00:23:31 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|
|
|
"""Unload a config entry."""
|
2023-01-16 08:23:03 +00:00
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
2022-12-16 15:53:54 +00:00
|
|
|
coordinator = hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
await coordinator.api.close()
|
2022-01-11 00:23:31 +00:00
|
|
|
return unload_ok
|