Move guardian base entity to separate module (#126486)
This commit is contained in:
parent
ef8b6e2805
commit
a9d12608bd
8 changed files with 99 additions and 86 deletions
|
@ -24,10 +24,7 @@ from homeassistant.const import (
|
|||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.entity import EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import (
|
||||
API_SENSOR_PAIR_DUMP,
|
||||
|
@ -357,70 +354,3 @@ class PairedSensorManager:
|
|||
config_entry_id=self._entry.entry_id, identifiers={(DOMAIN, uid)}
|
||||
)
|
||||
dev_reg.async_remove_device(device.id)
|
||||
|
||||
|
||||
class GuardianEntity(CoordinatorEntity[GuardianDataUpdateCoordinator]):
|
||||
"""Define a base Guardian entity."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self, coordinator: GuardianDataUpdateCoordinator, description: EntityDescription
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
self.entity_description = description
|
||||
|
||||
|
||||
class PairedSensorEntity(GuardianEntity):
|
||||
"""Define a Guardian paired sensor entity."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
entry: ConfigEntry,
|
||||
coordinator: GuardianDataUpdateCoordinator,
|
||||
description: EntityDescription,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator, description)
|
||||
|
||||
paired_sensor_uid = coordinator.data["uid"]
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, paired_sensor_uid)},
|
||||
manufacturer="Elexa",
|
||||
model=coordinator.data["codename"],
|
||||
name=f"Guardian paired sensor {paired_sensor_uid}",
|
||||
via_device=(DOMAIN, entry.data[CONF_UID]),
|
||||
)
|
||||
self._attr_unique_id = f"{paired_sensor_uid}_{description.key}"
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class ValveControllerEntityDescription(EntityDescription):
|
||||
"""Describe a Guardian valve controller entity."""
|
||||
|
||||
api_category: str
|
||||
|
||||
|
||||
class ValveControllerEntity(GuardianEntity):
|
||||
"""Define a Guardian valve controller entity."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
entry: ConfigEntry,
|
||||
coordinators: dict[str, GuardianDataUpdateCoordinator],
|
||||
description: ValveControllerEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinators[description.api_category], description)
|
||||
|
||||
self._diagnostics_coordinator = coordinators[API_SYSTEM_DIAGNOSTICS]
|
||||
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, entry.data[CONF_UID])},
|
||||
manufacturer="Elexa",
|
||||
model=self._diagnostics_coordinator.data["firmware"],
|
||||
name=f"Guardian valve controller {entry.data[CONF_UID]}",
|
||||
)
|
||||
self._attr_unique_id = f"{entry.data[CONF_UID]}_{description.key}"
|
||||
|
|
|
@ -18,12 +18,7 @@ from homeassistant.core import HomeAssistant, callback
|
|||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import (
|
||||
GuardianData,
|
||||
PairedSensorEntity,
|
||||
ValveControllerEntity,
|
||||
ValveControllerEntityDescription,
|
||||
)
|
||||
from . import GuardianData
|
||||
from .const import (
|
||||
API_SYSTEM_ONBOARD_SENSOR_STATUS,
|
||||
CONF_UID,
|
||||
|
@ -31,6 +26,11 @@ from .const import (
|
|||
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED,
|
||||
)
|
||||
from .coordinator import GuardianDataUpdateCoordinator
|
||||
from .entity import (
|
||||
PairedSensorEntity,
|
||||
ValveControllerEntity,
|
||||
ValveControllerEntityDescription,
|
||||
)
|
||||
from .util import (
|
||||
EntityDomainReplacementStrategy,
|
||||
async_finish_entity_domain_replacements,
|
||||
|
|
|
@ -18,8 +18,9 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import GuardianData, ValveControllerEntity, ValveControllerEntityDescription
|
||||
from . import GuardianData
|
||||
from .const import API_SYSTEM_DIAGNOSTICS, DOMAIN
|
||||
from .entity import ValveControllerEntity, ValveControllerEntityDescription
|
||||
from .util import convert_exceptions_to_homeassistant_error
|
||||
|
||||
|
||||
|
|
80
homeassistant/components/guardian/entity.py
Normal file
80
homeassistant/components/guardian/entity.py
Normal file
|
@ -0,0 +1,80 @@
|
|||
"""The Elexa Guardian integration."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from dataclasses import dataclass
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.entity import EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .const import API_SYSTEM_DIAGNOSTICS, CONF_UID, DOMAIN
|
||||
from .coordinator import GuardianDataUpdateCoordinator
|
||||
|
||||
|
||||
class GuardianEntity(CoordinatorEntity[GuardianDataUpdateCoordinator]):
|
||||
"""Define a base Guardian entity."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
|
||||
def __init__(
|
||||
self, coordinator: GuardianDataUpdateCoordinator, description: EntityDescription
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator)
|
||||
|
||||
self.entity_description = description
|
||||
|
||||
|
||||
class PairedSensorEntity(GuardianEntity):
|
||||
"""Define a Guardian paired sensor entity."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
entry: ConfigEntry,
|
||||
coordinator: GuardianDataUpdateCoordinator,
|
||||
description: EntityDescription,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinator, description)
|
||||
|
||||
paired_sensor_uid = coordinator.data["uid"]
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, paired_sensor_uid)},
|
||||
manufacturer="Elexa",
|
||||
model=coordinator.data["codename"],
|
||||
name=f"Guardian paired sensor {paired_sensor_uid}",
|
||||
via_device=(DOMAIN, entry.data[CONF_UID]),
|
||||
)
|
||||
self._attr_unique_id = f"{paired_sensor_uid}_{description.key}"
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
class ValveControllerEntityDescription(EntityDescription):
|
||||
"""Describe a Guardian valve controller entity."""
|
||||
|
||||
api_category: str
|
||||
|
||||
|
||||
class ValveControllerEntity(GuardianEntity):
|
||||
"""Define a Guardian valve controller entity."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
entry: ConfigEntry,
|
||||
coordinators: dict[str, GuardianDataUpdateCoordinator],
|
||||
description: ValveControllerEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize."""
|
||||
super().__init__(coordinators[description.api_category], description)
|
||||
|
||||
self._diagnostics_coordinator = coordinators[API_SYSTEM_DIAGNOSTICS]
|
||||
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, entry.data[CONF_UID])},
|
||||
manufacturer="Elexa",
|
||||
model=self._diagnostics_coordinator.data["firmware"],
|
||||
name=f"Guardian valve controller {entry.data[CONF_UID]}",
|
||||
)
|
||||
self._attr_unique_id = f"{entry.data[CONF_UID]}_{description.key}"
|
|
@ -25,12 +25,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from . import (
|
||||
GuardianData,
|
||||
PairedSensorEntity,
|
||||
ValveControllerEntity,
|
||||
ValveControllerEntityDescription,
|
||||
)
|
||||
from . import GuardianData
|
||||
from .const import (
|
||||
API_SYSTEM_DIAGNOSTICS,
|
||||
API_SYSTEM_ONBOARD_SENSOR_STATUS,
|
||||
|
@ -39,6 +34,11 @@ from .const import (
|
|||
DOMAIN,
|
||||
SIGNAL_PAIRED_SENSOR_COORDINATOR_ADDED,
|
||||
)
|
||||
from .entity import (
|
||||
PairedSensorEntity,
|
||||
ValveControllerEntity,
|
||||
ValveControllerEntityDescription,
|
||||
)
|
||||
|
||||
SENSOR_KIND_AVG_CURRENT = "average_current"
|
||||
SENSOR_KIND_BATTERY = "battery"
|
||||
|
|
|
@ -14,8 +14,9 @@ from homeassistant.const import EntityCategory
|
|||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import GuardianData, ValveControllerEntity, ValveControllerEntityDescription
|
||||
from . import GuardianData
|
||||
from .const import API_VALVE_STATUS, API_WIFI_STATUS, DOMAIN
|
||||
from .entity import ValveControllerEntity, ValveControllerEntityDescription
|
||||
from .util import convert_exceptions_to_homeassistant_error
|
||||
from .valve import GuardianValveState
|
||||
|
||||
|
|
|
@ -18,7 +18,7 @@ from homeassistant.helpers import entity_registry as er
|
|||
from .const import LOGGER
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from . import GuardianEntity
|
||||
from .entity import GuardianEntity
|
||||
|
||||
DEFAULT_UPDATE_INTERVAL = timedelta(seconds=30)
|
||||
|
||||
|
|
|
@ -19,8 +19,9 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import GuardianData, ValveControllerEntity, ValveControllerEntityDescription
|
||||
from . import GuardianData
|
||||
from .const import API_VALVE_STATUS, DOMAIN
|
||||
from .entity import ValveControllerEntity, ValveControllerEntityDescription
|
||||
from .util import convert_exceptions_to_homeassistant_error
|
||||
|
||||
VALVE_KIND_VALVE = "valve"
|
||||
|
|
Loading…
Add table
Reference in a new issue