Move huawei_lte base entity to separate module (#126098)

This commit is contained in:
epenet 2024-09-17 15:41:03 +02:00 committed by GitHub
parent 1afcbd02a9
commit 9557386b6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 86 additions and 69 deletions

View File

@ -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,
)

View File

@ -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__)

View File

@ -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__)

View File

@ -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__)

View File

@ -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,
)

View File

@ -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__)

View File

@ -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__)

View File

@ -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__)