From a9c479a78b3b597c3602b2aa98abfa70488d427d Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:44:52 +0200 Subject: [PATCH] Move hive base entity to separate module (#126095) --- homeassistant/components/hive/__init__.py | 35 ++--------------- .../components/hive/alarm_control_panel.py | 2 +- .../components/hive/binary_sensor.py | 2 +- homeassistant/components/hive/climate.py | 3 +- homeassistant/components/hive/entity.py | 39 +++++++++++++++++++ homeassistant/components/hive/light.py | 3 +- homeassistant/components/hive/sensor.py | 2 +- homeassistant/components/hive/switch.py | 3 +- homeassistant/components/hive/water_heater.py | 3 +- 9 files changed, 53 insertions(+), 39 deletions(-) create mode 100644 homeassistant/components/hive/entity.py diff --git a/homeassistant/components/hive/__init__.py b/homeassistant/components/hive/__init__.py index 4001215d90e..1c11ccad595 100644 --- a/homeassistant/components/hive/__init__.py +++ b/homeassistant/components/hive/__init__.py @@ -18,15 +18,12 @@ from homeassistant.const import CONF_PASSWORD, CONF_SCAN_INTERVAL, CONF_USERNAME from homeassistant.core import HomeAssistant from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import aiohttp_client, config_validation as cv -from homeassistant.helpers.device_registry import DeviceEntry, DeviceInfo -from homeassistant.helpers.dispatcher import ( - async_dispatcher_connect, - async_dispatcher_send, -) -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.device_registry import DeviceEntry +from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.typing import ConfigType from .const import DOMAIN, PLATFORM_LOOKUP, PLATFORMS +from .entity import HiveEntity _LOGGER = logging.getLogger(__name__) @@ -139,29 +136,3 @@ def refresh_system[_HiveEntityT: HiveEntity, **_P]( async_dispatcher_send(self.hass, DOMAIN) return wrapper - - -class HiveEntity(Entity): - """Initiate Hive Base Class.""" - - def __init__(self, hive: Hive, hive_device: dict[str, Any]) -> None: - """Initialize the instance.""" - self.hive = hive - self.device = hive_device - self._attr_name = self.device["haName"] - self._attr_unique_id = f'{self.device["hiveID"]}-{self.device["hiveType"]}' - self._attr_device_info = DeviceInfo( - identifiers={(DOMAIN, self.device["device_id"])}, - model=self.device["deviceData"]["model"], - manufacturer=self.device["deviceData"]["manufacturer"], - name=self.device["device_name"], - sw_version=self.device["deviceData"]["version"], - via_device=(DOMAIN, self.device["parentDevice"]), - ) - self.attributes: dict[str, Any] = {} - - async def async_added_to_hass(self) -> None: - """When entity is added to Home Assistant.""" - self.async_on_remove( - async_dispatcher_connect(self.hass, DOMAIN, self.async_write_ha_state) - ) diff --git a/homeassistant/components/hive/alarm_control_panel.py b/homeassistant/components/hive/alarm_control_panel.py index 06383784a3f..34d5d3d10c6 100644 --- a/homeassistant/components/hive/alarm_control_panel.py +++ b/homeassistant/components/hive/alarm_control_panel.py @@ -18,8 +18,8 @@ from homeassistant.const import ( from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import HiveEntity from .const import DOMAIN +from .entity import HiveEntity PARALLEL_UPDATES = 0 SCAN_INTERVAL = timedelta(seconds=15) diff --git a/homeassistant/components/hive/binary_sensor.py b/homeassistant/components/hive/binary_sensor.py index 512b06ece6d..d14d98bcf50 100644 --- a/homeassistant/components/hive/binary_sensor.py +++ b/homeassistant/components/hive/binary_sensor.py @@ -14,8 +14,8 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import HiveEntity from .const import DOMAIN +from .entity import HiveEntity PARALLEL_UPDATES = 0 SCAN_INTERVAL = timedelta(seconds=15) diff --git a/homeassistant/components/hive/climate.py b/homeassistant/components/hive/climate.py index 87d93eea95f..4e5ea95f2fa 100644 --- a/homeassistant/components/hive/climate.py +++ b/homeassistant/components/hive/climate.py @@ -21,13 +21,14 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import HiveEntity, refresh_system +from . import refresh_system from .const import ( ATTR_TIME_PERIOD, DOMAIN, SERVICE_BOOST_HEATING_OFF, SERVICE_BOOST_HEATING_ON, ) +from .entity import HiveEntity HIVE_TO_HASS_STATE = { "SCHEDULE": HVACMode.AUTO, diff --git a/homeassistant/components/hive/entity.py b/homeassistant/components/hive/entity.py new file mode 100644 index 00000000000..1209e8c8f05 --- /dev/null +++ b/homeassistant/components/hive/entity.py @@ -0,0 +1,39 @@ +"""Support for the Hive devices and services.""" + +from __future__ import annotations + +from typing import Any + +from apyhiveapi import Hive + +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity import Entity + +from .const import DOMAIN + + +class HiveEntity(Entity): + """Initiate Hive Base Class.""" + + def __init__(self, hive: Hive, hive_device: dict[str, Any]) -> None: + """Initialize the instance.""" + self.hive = hive + self.device = hive_device + self._attr_name = self.device["haName"] + self._attr_unique_id = f'{self.device["hiveID"]}-{self.device["hiveType"]}' + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, self.device["device_id"])}, + model=self.device["deviceData"]["model"], + manufacturer=self.device["deviceData"]["manufacturer"], + name=self.device["device_name"], + sw_version=self.device["deviceData"]["version"], + via_device=(DOMAIN, self.device["parentDevice"]), + ) + self.attributes: dict[str, Any] = {} + + async def async_added_to_hass(self) -> None: + """When entity is added to Home Assistant.""" + self.async_on_remove( + async_dispatcher_connect(self.hass, DOMAIN, self.async_write_ha_state) + ) diff --git a/homeassistant/components/hive/light.py b/homeassistant/components/hive/light.py index 1ce49599262..10de781bf1d 100644 --- a/homeassistant/components/hive/light.py +++ b/homeassistant/components/hive/light.py @@ -17,8 +17,9 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback import homeassistant.util.color as color_util -from . import HiveEntity, refresh_system +from . import refresh_system from .const import ATTR_MODE, DOMAIN +from .entity import HiveEntity if TYPE_CHECKING: from apyhiveapi import Hive diff --git a/homeassistant/components/hive/sensor.py b/homeassistant/components/hive/sensor.py index d51acecc9f6..97f7a07237d 100644 --- a/homeassistant/components/hive/sensor.py +++ b/homeassistant/components/hive/sensor.py @@ -24,8 +24,8 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType -from . import HiveEntity from .const import DOMAIN +from .entity import HiveEntity PARALLEL_UPDATES = 0 SCAN_INTERVAL = timedelta(seconds=15) diff --git a/homeassistant/components/hive/switch.py b/homeassistant/components/hive/switch.py index 136f03de195..1421616db57 100644 --- a/homeassistant/components/hive/switch.py +++ b/homeassistant/components/hive/switch.py @@ -13,8 +13,9 @@ from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import HiveEntity, refresh_system +from . import refresh_system from .const import ATTR_MODE, DOMAIN +from .entity import HiveEntity PARALLEL_UPDATES = 0 SCAN_INTERVAL = timedelta(seconds=15) diff --git a/homeassistant/components/hive/water_heater.py b/homeassistant/components/hive/water_heater.py index 2e582e19567..b038739d2ad 100644 --- a/homeassistant/components/hive/water_heater.py +++ b/homeassistant/components/hive/water_heater.py @@ -16,7 +16,7 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import HiveEntity, refresh_system +from . import refresh_system from .const import ( ATTR_ONOFF, ATTR_TIME_PERIOD, @@ -24,6 +24,7 @@ from .const import ( SERVICE_BOOST_HOT_WATER, WATER_HEATER_MODES, ) +from .entity import HiveEntity HOTWATER_NAME = "Hot Water" PARALLEL_UPDATES = 0