diff --git a/homeassistant/components/hive/__init__.py b/homeassistant/components/hive/__init__.py index ac008b857af..c45ecd24ea3 100644 --- a/homeassistant/components/hive/__init__.py +++ b/homeassistant/components/hive/__init__.py @@ -24,11 +24,11 @@ from .entity import HiveEntity _LOGGER = logging.getLogger(__name__) +type HiveConfigEntry = ConfigEntry[Hive] -async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: + +async def async_setup_entry(hass: HomeAssistant, entry: HiveConfigEntry) -> bool: """Set up Hive from a config entry.""" - hass.data.setdefault(DOMAIN, {}) - web_session = aiohttp_client.async_get_clientsession(hass) hive_config = dict(entry.data) hive = Hive(web_session) @@ -37,7 +37,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: hive_config["options"].update( {CONF_SCAN_INTERVAL: dict(entry.options).get(CONF_SCAN_INTERVAL, 120)} ) - hass.data[DOMAIN][entry.entry_id] = hive + entry.runtime_data = hive try: devices = await hive.session.startSession(hive_config) @@ -59,16 +59,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: return True -async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: +async def async_unload_entry(hass: HomeAssistant, entry: HiveConfigEntry) -> bool: """Unload a config entry.""" - unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS) - if unload_ok: - hass.data[DOMAIN].pop(entry.entry_id) - - return unload_ok + return await hass.config_entries.async_unload_platforms(entry, PLATFORMS) -async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: +async def async_remove_entry(hass: HomeAssistant, entry: HiveConfigEntry) -> None: """Remove a config entry.""" hive = Auth(entry.data["username"], entry.data["password"]) await hive.forget_device( @@ -78,7 +74,7 @@ async def async_remove_entry(hass: HomeAssistant, entry: ConfigEntry) -> None: async def async_remove_config_entry_device( - hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry + hass: HomeAssistant, config_entry: HiveConfigEntry, device_entry: DeviceEntry ) -> bool: """Remove a config entry from a device.""" return True diff --git a/homeassistant/components/hive/alarm_control_panel.py b/homeassistant/components/hive/alarm_control_panel.py index c2fe47642a0..338cc6bcf0a 100644 --- a/homeassistant/components/hive/alarm_control_panel.py +++ b/homeassistant/components/hive/alarm_control_panel.py @@ -9,11 +9,10 @@ from homeassistant.components.alarm_control_panel import ( AlarmControlPanelEntityFeature, AlarmControlPanelState, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN +from . import HiveConfigEntry from .entity import HiveEntity PARALLEL_UPDATES = 0 @@ -28,12 +27,12 @@ HIVETOHA = { async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: HiveConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Hive thermostat based on a config entry.""" - hive = hass.data[DOMAIN][entry.entry_id] + hive = entry.runtime_data if devices := hive.session.deviceList.get("alarm_control_panel"): async_add_entities( [HiveAlarmControlPanelEntity(hive, dev) for dev in devices], True diff --git a/homeassistant/components/hive/binary_sensor.py b/homeassistant/components/hive/binary_sensor.py index 2076d592a7c..cdf6c253916 100644 --- a/homeassistant/components/hive/binary_sensor.py +++ b/homeassistant/components/hive/binary_sensor.py @@ -10,11 +10,10 @@ from homeassistant.components.binary_sensor import ( BinarySensorEntity, BinarySensorEntityDescription, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import DOMAIN +from . import HiveConfigEntry from .entity import HiveEntity PARALLEL_UPDATES = 0 @@ -69,12 +68,12 @@ SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = ( async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: HiveConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Hive thermostat based on a config entry.""" - hive = hass.data[DOMAIN][entry.entry_id] + hive = entry.runtime_data sensors: list[BinarySensorEntity] = [] diff --git a/homeassistant/components/hive/climate.py b/homeassistant/components/hive/climate.py index bd7553faa1a..28062adb0e3 100644 --- a/homeassistant/components/hive/climate.py +++ b/homeassistant/components/hive/climate.py @@ -15,19 +15,13 @@ from homeassistant.components.climate import ( HVACAction, HVACMode, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from . import refresh_system -from .const import ( - ATTR_TIME_PERIOD, - DOMAIN, - SERVICE_BOOST_HEATING_OFF, - SERVICE_BOOST_HEATING_ON, -) +from . import HiveConfigEntry, refresh_system +from .const import ATTR_TIME_PERIOD, SERVICE_BOOST_HEATING_OFF, SERVICE_BOOST_HEATING_ON from .entity import HiveEntity HIVE_TO_HASS_STATE = { @@ -59,12 +53,12 @@ _LOGGER = logging.getLogger() async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: HiveConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Hive thermostat based on a config entry.""" - hive = hass.data[DOMAIN][entry.entry_id] + hive = entry.runtime_data devices = hive.session.deviceList.get("climate") if devices: async_add_entities((HiveClimateEntity(hive, dev) for dev in devices), True) diff --git a/homeassistant/components/hive/config_flow.py b/homeassistant/components/hive/config_flow.py index e3180dc9734..41dba27c3a5 100644 --- a/homeassistant/components/hive/config_flow.py +++ b/homeassistant/components/hive/config_flow.py @@ -16,7 +16,6 @@ import voluptuous as vol from homeassistant.config_entries import ( SOURCE_REAUTH, - ConfigEntry, ConfigFlow, ConfigFlowResult, OptionsFlow, @@ -24,6 +23,7 @@ from homeassistant.config_entries import ( from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME from homeassistant.core import callback +from . import HiveConfigEntry from .const import CONF_CODE, CONF_DEVICE_NAME, CONFIG_ENTRY_VERSION, DOMAIN @@ -37,7 +37,6 @@ class HiveFlowHandler(ConfigFlow, domain=DOMAIN): """Initialize the config flow.""" self.data: dict[str, Any] = {} self.tokens: dict[str, str] = {} - self.entry: ConfigEntry | None = None self.device_registration: bool = False self.device_name = "Home Assistant" @@ -54,7 +53,7 @@ class HiveFlowHandler(ConfigFlow, domain=DOMAIN): ) # Get user from existing entry and abort if already setup - self.entry = await self.async_set_unique_id(self.data[CONF_USERNAME]) + await self.async_set_unique_id(self.data[CONF_USERNAME]) if self.context["source"] != SOURCE_REAUTH: self._abort_if_unique_id_configured() @@ -145,12 +144,12 @@ class HiveFlowHandler(ConfigFlow, domain=DOMAIN): # Setup the config entry self.data["tokens"] = self.tokens if self.source == SOURCE_REAUTH: - assert self.entry - self.hass.config_entries.async_update_entry( - self.entry, title=self.data["username"], data=self.data + return self.async_update_reload_and_abort( + self._get_reauth_entry(), + title=self.data["username"], + data=self.data, + reason="reauth_successful", ) - await self.hass.config_entries.async_reload(self.entry.entry_id) - return self.async_abort(reason="reauth_successful") return self.async_create_entry(title=self.data["username"], data=self.data) async def async_step_reauth( @@ -166,7 +165,7 @@ class HiveFlowHandler(ConfigFlow, domain=DOMAIN): @staticmethod @callback def async_get_options_flow( - config_entry: ConfigEntry, + config_entry: HiveConfigEntry, ) -> HiveOptionsFlowHandler: """Hive options callback.""" return HiveOptionsFlowHandler(config_entry) @@ -175,7 +174,9 @@ class HiveFlowHandler(ConfigFlow, domain=DOMAIN): class HiveOptionsFlowHandler(OptionsFlow): """Config flow options for Hive.""" - def __init__(self, config_entry: ConfigEntry) -> None: + config_entry: HiveConfigEntry + + def __init__(self, config_entry: HiveConfigEntry) -> None: """Initialize Hive options flow.""" self.hive = None self.interval = config_entry.options.get(CONF_SCAN_INTERVAL, 120) @@ -190,7 +191,7 @@ class HiveOptionsFlowHandler(OptionsFlow): self, user_input: dict[str, Any] | None = None ) -> ConfigFlowResult: """Handle a flow initialized by the user.""" - self.hive = self.hass.data["hive"][self.config_entry.entry_id] + self.hive = self.config_entry.runtime_data errors: dict[str, str] = {} if user_input is not None: new_interval = user_input.get(CONF_SCAN_INTERVAL) diff --git a/homeassistant/components/hive/light.py b/homeassistant/components/hive/light.py index 80a81583429..f89d23b8513 100644 --- a/homeassistant/components/hive/light.py +++ b/homeassistant/components/hive/light.py @@ -3,7 +3,9 @@ from __future__ import annotations from datetime import timedelta -from typing import TYPE_CHECKING, Any +from typing import Any + +from apyhiveapi import Hive from homeassistant.components.light import ( ATTR_BRIGHTNESS, @@ -12,30 +14,26 @@ from homeassistant.components.light import ( ColorMode, LightEntity, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.util import color as color_util -from . import refresh_system -from .const import ATTR_MODE, DOMAIN +from . import HiveConfigEntry, refresh_system +from .const import ATTR_MODE from .entity import HiveEntity -if TYPE_CHECKING: - from apyhiveapi import Hive - PARALLEL_UPDATES = 0 SCAN_INTERVAL = timedelta(seconds=15) async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: HiveConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Hive thermostat based on a config entry.""" - hive: Hive = hass.data[DOMAIN][entry.entry_id] + hive = entry.runtime_data devices = hive.session.deviceList.get("light") if not devices: return diff --git a/homeassistant/components/hive/sensor.py b/homeassistant/components/hive/sensor.py index 0609e43c4a9..70a21038d67 100644 --- a/homeassistant/components/hive/sensor.py +++ b/homeassistant/components/hive/sensor.py @@ -13,7 +13,6 @@ from homeassistant.components.sensor import ( SensorEntityDescription, SensorStateClass, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import ( PERCENTAGE, EntityCategory, @@ -24,7 +23,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.helpers.typing import StateType -from .const import DOMAIN +from . import HiveConfigEntry from .entity import HiveEntity PARALLEL_UPDATES = 0 @@ -90,11 +89,11 @@ SENSOR_TYPES: tuple[HiveSensorEntityDescription, ...] = ( async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: HiveConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Hive thermostat based on a config entry.""" - hive = hass.data[DOMAIN][entry.entry_id] + hive = entry.runtime_data devices = hive.session.deviceList.get("sensor") if not devices: return diff --git a/homeassistant/components/hive/switch.py b/homeassistant/components/hive/switch.py index d4fefea5a56..0640436d105 100644 --- a/homeassistant/components/hive/switch.py +++ b/homeassistant/components/hive/switch.py @@ -8,13 +8,12 @@ from typing import Any from apyhiveapi import Hive from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription -from homeassistant.config_entries import ConfigEntry from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from . import refresh_system -from .const import ATTR_MODE, DOMAIN +from . import HiveConfigEntry, refresh_system +from .const import ATTR_MODE from .entity import HiveEntity PARALLEL_UPDATES = 0 @@ -34,12 +33,12 @@ SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = ( async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: HiveConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Hive thermostat based on a config entry.""" - hive = hass.data[DOMAIN][entry.entry_id] + hive = entry.runtime_data devices = hive.session.deviceList.get("switch") if not devices: return diff --git a/homeassistant/components/hive/water_heater.py b/homeassistant/components/hive/water_heater.py index 5f0a3d0f3fa..104c4f62f9c 100644 --- a/homeassistant/components/hive/water_heater.py +++ b/homeassistant/components/hive/water_heater.py @@ -10,17 +10,15 @@ from homeassistant.components.water_heater import ( WaterHeaterEntity, WaterHeaterEntityFeature, ) -from homeassistant.config_entries import ConfigEntry from homeassistant.const import STATE_OFF, STATE_ON, UnitOfTemperature from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from . import refresh_system +from . import HiveConfigEntry, refresh_system from .const import ( ATTR_ONOFF, ATTR_TIME_PERIOD, - DOMAIN, SERVICE_BOOST_HOT_WATER, WATER_HEATER_MODES, ) @@ -46,12 +44,12 @@ SUPPORT_WATER_HEATER = [STATE_ECO, STATE_ON, STATE_OFF] async def async_setup_entry( hass: HomeAssistant, - entry: ConfigEntry, + entry: HiveConfigEntry, async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Hive thermostat based on a config entry.""" - hive = hass.data[DOMAIN][entry.entry_id] + hive = entry.runtime_data devices = hive.session.deviceList.get("water_heater") if devices: async_add_entities((HiveWaterHeater(hive, dev) for dev in devices), True)