From 9557386b6e0052ebd76f4bd434f8454580a0612e Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Tue, 17 Sep 2024 15:41:03 +0200 Subject: [PATCH] Move huawei_lte base entity to separate module (#126098) --- .../components/huawei_lte/__init__.py | 64 +--------------- .../components/huawei_lte/binary_sensor.py | 2 +- homeassistant/components/huawei_lte/button.py | 2 +- .../components/huawei_lte/device_tracker.py | 3 +- homeassistant/components/huawei_lte/entity.py | 76 +++++++++++++++++++ homeassistant/components/huawei_lte/select.py | 3 +- homeassistant/components/huawei_lte/sensor.py | 3 +- homeassistant/components/huawei_lte/switch.py | 2 +- 8 files changed, 86 insertions(+), 69 deletions(-) create mode 100644 homeassistant/components/huawei_lte/entity.py diff --git a/homeassistant/components/huawei_lte/__init__.py b/homeassistant/components/huawei_lte/__init__.py index ad72e839534..a5a60d8406d 100644 --- a/homeassistant/components/huawei_lte/__init__.py +++ b/homeassistant/components/huawei_lte/__init__.py @@ -48,8 +48,7 @@ from homeassistant.helpers import ( entity_registry as er, ) from homeassistant.helpers.device_registry import DeviceInfo -from homeassistant.helpers.dispatcher import async_dispatcher_connect, dispatcher_send -from homeassistant.helpers.entity import Entity +from homeassistant.helpers.dispatcher import dispatcher_send from homeassistant.helpers.event import async_track_time_interval from homeassistant.helpers.service import async_register_admin_service from homeassistant.helpers.typing import ConfigType @@ -569,64 +568,3 @@ async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> # from pre-2022.4ish; they can be removed while at it when/if we eventually bump and # migrate to version > 3 for some other reason. return True - - -class HuaweiLteBaseEntity(Entity): - """Huawei LTE entity base class.""" - - _available = True - _attr_has_entity_name = True - _attr_should_poll = False - - def __init__(self, router: Router) -> None: - """Initialize.""" - self.router = router - self._unsub_handlers: list[Callable] = [] - - @property - def _device_unique_id(self) -> str: - """Return unique ID for entity within a router.""" - raise NotImplementedError - - @property - def unique_id(self) -> str: - """Return unique ID for entity.""" - return f"{self.router.config_entry.unique_id}-{self._device_unique_id}" - - @property - def available(self) -> bool: - """Return whether the entity is available.""" - return self._available - - async def async_update(self) -> None: - """Update state.""" - raise NotImplementedError - - async def async_added_to_hass(self) -> None: - """Connect to update signals.""" - self._unsub_handlers.append( - async_dispatcher_connect(self.hass, UPDATE_SIGNAL, self._async_maybe_update) - ) - - async def _async_maybe_update(self, config_entry_unique_id: str) -> None: - """Update state if the update signal comes from our router.""" - if config_entry_unique_id == self.router.config_entry.unique_id: - self.async_schedule_update_ha_state(True) - - async def async_will_remove_from_hass(self) -> None: - """Invoke unsubscription handlers.""" - for unsub in self._unsub_handlers: - unsub() - self._unsub_handlers.clear() - - -class HuaweiLteBaseEntityWithDevice(HuaweiLteBaseEntity): - """Base entity with device info.""" - - @property - def device_info(self) -> DeviceInfo: - """Get info for matching with parent router.""" - return DeviceInfo( - connections=self.router.device_connections, - identifiers=self.router.device_identifiers, - ) diff --git a/homeassistant/components/huawei_lte/binary_sensor.py b/homeassistant/components/huawei_lte/binary_sensor.py index c90a7854a91..06b859cea84 100644 --- a/homeassistant/components/huawei_lte/binary_sensor.py +++ b/homeassistant/components/huawei_lte/binary_sensor.py @@ -16,13 +16,13 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import HuaweiLteBaseEntityWithDevice from .const import ( DOMAIN, KEY_MONITORING_CHECK_NOTIFICATIONS, KEY_MONITORING_STATUS, KEY_WLAN_WIFI_FEATURE_SWITCH, ) +from .entity import HuaweiLteBaseEntityWithDevice _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/huawei_lte/button.py b/homeassistant/components/huawei_lte/button.py index f494836e80d..55b009d25bf 100644 --- a/homeassistant/components/huawei_lte/button.py +++ b/homeassistant/components/huawei_lte/button.py @@ -16,8 +16,8 @@ from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_platform -from . import HuaweiLteBaseEntityWithDevice from .const import DOMAIN +from .entity import HuaweiLteBaseEntityWithDevice _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/huawei_lte/device_tracker.py b/homeassistant/components/huawei_lte/device_tracker.py index 0e35208dcce..6a05b237160 100644 --- a/homeassistant/components/huawei_lte/device_tracker.py +++ b/homeassistant/components/huawei_lte/device_tracker.py @@ -20,7 +20,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import HuaweiLteBaseEntity, Router +from . import Router from .const import ( CONF_TRACK_WIRED_CLIENTS, DEFAULT_TRACK_WIRED_CLIENTS, @@ -29,6 +29,7 @@ from .const import ( KEY_WLAN_HOST_LIST, UPDATE_SIGNAL, ) +from .entity import HuaweiLteBaseEntity _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/huawei_lte/entity.py b/homeassistant/components/huawei_lte/entity.py new file mode 100644 index 00000000000..99d7ca112c4 --- /dev/null +++ b/homeassistant/components/huawei_lte/entity.py @@ -0,0 +1,76 @@ +"""Support for Huawei LTE routers.""" + +from __future__ import annotations + +from collections.abc import Callable +from datetime import timedelta + +from homeassistant.helpers.device_registry import DeviceInfo +from homeassistant.helpers.dispatcher import async_dispatcher_connect +from homeassistant.helpers.entity import Entity + +from . import Router +from .const import UPDATE_SIGNAL + +SCAN_INTERVAL = timedelta(seconds=10) + + +class HuaweiLteBaseEntity(Entity): + """Huawei LTE entity base class.""" + + _available = True + _attr_has_entity_name = True + _attr_should_poll = False + + def __init__(self, router: Router) -> None: + """Initialize.""" + self.router = router + self._unsub_handlers: list[Callable] = [] + + @property + def _device_unique_id(self) -> str: + """Return unique ID for entity within a router.""" + raise NotImplementedError + + @property + def unique_id(self) -> str: + """Return unique ID for entity.""" + return f"{self.router.config_entry.unique_id}-{self._device_unique_id}" + + @property + def available(self) -> bool: + """Return whether the entity is available.""" + return self._available + + async def async_update(self) -> None: + """Update state.""" + raise NotImplementedError + + async def async_added_to_hass(self) -> None: + """Connect to update signals.""" + self._unsub_handlers.append( + async_dispatcher_connect(self.hass, UPDATE_SIGNAL, self._async_maybe_update) + ) + + async def _async_maybe_update(self, config_entry_unique_id: str) -> None: + """Update state if the update signal comes from our router.""" + if config_entry_unique_id == self.router.config_entry.unique_id: + self.async_schedule_update_ha_state(True) + + async def async_will_remove_from_hass(self) -> None: + """Invoke unsubscription handlers.""" + for unsub in self._unsub_handlers: + unsub() + self._unsub_handlers.clear() + + +class HuaweiLteBaseEntityWithDevice(HuaweiLteBaseEntity): + """Base entity with device info.""" + + @property + def device_info(self) -> DeviceInfo: + """Get info for matching with parent router.""" + return DeviceInfo( + connections=self.router.device_connections, + identifiers=self.router.device_identifiers, + ) diff --git a/homeassistant/components/huawei_lte/select.py b/homeassistant/components/huawei_lte/select.py index bf8f65a8ba5..d8a16ae2f79 100644 --- a/homeassistant/components/huawei_lte/select.py +++ b/homeassistant/components/huawei_lte/select.py @@ -21,8 +21,9 @@ from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import UNDEFINED -from . import HuaweiLteBaseEntityWithDevice, Router +from . import Router from .const import DOMAIN, KEY_NET_NET_MODE +from .entity import HuaweiLteBaseEntityWithDevice _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/huawei_lte/sensor.py b/homeassistant/components/huawei_lte/sensor.py index 2a7fe5c29b2..86965e89dd0 100644 --- a/homeassistant/components/huawei_lte/sensor.py +++ b/homeassistant/components/huawei_lte/sensor.py @@ -30,7 +30,7 @@ from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import StateType -from . import HuaweiLteBaseEntityWithDevice, Router +from . import Router from .const import ( DOMAIN, KEY_DEVICE_INFORMATION, @@ -44,6 +44,7 @@ from .const import ( KEY_SMS_SMS_COUNT, SENSOR_KEYS, ) +from .entity import HuaweiLteBaseEntityWithDevice _LOGGER = logging.getLogger(__name__) diff --git a/homeassistant/components/huawei_lte/switch.py b/homeassistant/components/huawei_lte/switch.py index 3a499851f9a..07fd89d0b6c 100644 --- a/homeassistant/components/huawei_lte/switch.py +++ b/homeassistant/components/huawei_lte/switch.py @@ -15,12 +15,12 @@ from homeassistant.core import HomeAssistant from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity_platform import AddEntitiesCallback -from . import HuaweiLteBaseEntityWithDevice from .const import ( DOMAIN, KEY_DIALUP_MOBILE_DATASWITCH, KEY_WLAN_WIFI_GUEST_NETWORK_SWITCH, ) +from .entity import HuaweiLteBaseEntityWithDevice _LOGGER = logging.getLogger(__name__)