Move rainmachine base entity to separate module (#126513)

This commit is contained in:
epenet 2024-09-23 12:42:22 +02:00 committed by GitHub
parent a75a513531
commit c8e3e2ce1b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 97 additions and 93 deletions

View File

@ -31,8 +31,7 @@ from homeassistant.helpers import (
device_registry as dr, device_registry as dr,
entity_registry as er, entity_registry as er,
) )
from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.update_coordinator import UpdateFailed
from homeassistant.helpers.update_coordinator import CoordinatorEntity, UpdateFailed
from homeassistant.util.dt import as_timestamp, utcnow from homeassistant.util.dt import as_timestamp, utcnow
from homeassistant.util.network import is_ip_address from homeassistant.util.network import is_ip_address
@ -54,7 +53,6 @@ from .const import (
LOGGER, LOGGER,
) )
from .coordinator import RainMachineDataUpdateCoordinator from .coordinator import RainMachineDataUpdateCoordinator
from .model import RainMachineEntityDescription
DEFAULT_SSL = True DEFAULT_SSL = True
@ -528,64 +526,3 @@ async def async_reload_entry(
) -> None: ) -> None:
"""Handle an options update.""" """Handle an options update."""
await hass.config_entries.async_reload(entry.entry_id) await hass.config_entries.async_reload(entry.entry_id)
class RainMachineEntity(CoordinatorEntity[RainMachineDataUpdateCoordinator]):
"""Define a generic RainMachine entity."""
_attr_has_entity_name = True
def __init__(
self,
entry: RainMachineConfigEntry,
data: RainMachineData,
description: RainMachineEntityDescription,
) -> None:
"""Initialize."""
super().__init__(data.coordinators[description.api_category])
self._attr_extra_state_attributes = {}
self._attr_unique_id = f"{data.controller.mac}_{description.key}"
self._entry = entry
self._data = data
self._version_coordinator = data.coordinators[DATA_API_VERSIONS]
self.entity_description = description
@property
def device_info(self) -> DeviceInfo:
"""Return device information about this controller."""
return DeviceInfo(
identifiers={(DOMAIN, self._data.controller.mac)},
configuration_url=(
f"https://{self._entry.data[CONF_IP_ADDRESS]}:"
f"{self._entry.data[CONF_PORT]}"
),
connections={(dr.CONNECTION_NETWORK_MAC, self._data.controller.mac)},
name=self._data.controller.name.capitalize(),
manufacturer="RainMachine",
model=(
f"Version {self._version_coordinator.data['hwVer']} "
f"(API: {self._version_coordinator.data['apiVer']})"
),
sw_version=self._version_coordinator.data["swVer"],
)
@callback
def _handle_coordinator_update(self) -> None:
"""Respond to a DataUpdateCoordinator update."""
self.update_from_latest_data()
self.async_write_ha_state()
async def async_added_to_hass(self) -> None:
"""When entity is added to hass."""
await super().async_added_to_hass()
self.async_on_remove(
self._version_coordinator.async_add_listener(
self._handle_coordinator_update, self.coordinator_context
)
)
self.update_from_latest_data()
@callback
def update_from_latest_data(self) -> None:
"""Update the state."""

View File

@ -11,9 +11,9 @@ from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import RainMachineConfigEntry, RainMachineEntity from . import RainMachineConfigEntry
from .const import DATA_PROVISION_SETTINGS, DATA_RESTRICTIONS_CURRENT from .const import DATA_PROVISION_SETTINGS, DATA_RESTRICTIONS_CURRENT
from .model import RainMachineEntityDescription from .entity import RainMachineEntity, RainMachineEntityDescription
from .util import ( from .util import (
EntityDomainReplacementStrategy, EntityDomainReplacementStrategy,
async_finish_entity_domain_replacements, async_finish_entity_domain_replacements,

View File

@ -19,9 +19,9 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.dispatcher import async_dispatcher_send from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import RainMachineConfigEntry, RainMachineEntity from . import RainMachineConfigEntry
from .const import DATA_PROVISION_SETTINGS from .const import DATA_PROVISION_SETTINGS
from .model import RainMachineEntityDescription from .entity import RainMachineEntity, RainMachineEntityDescription
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)

View File

@ -0,0 +1,84 @@
"""Support for RainMachine devices."""
from __future__ import annotations
from dataclasses import dataclass
from homeassistant.const import CONF_IP_ADDRESS, CONF_PORT
from homeassistant.core import callback
from homeassistant.helpers import device_registry as dr
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import RainMachineConfigEntry, RainMachineData
from .const import DATA_API_VERSIONS, DOMAIN
from .coordinator import RainMachineDataUpdateCoordinator
@dataclass(frozen=True, kw_only=True)
class RainMachineEntityDescription(EntityDescription):
"""Describe a RainMachine entity."""
api_category: str
class RainMachineEntity(CoordinatorEntity[RainMachineDataUpdateCoordinator]):
"""Define a generic RainMachine entity."""
_attr_has_entity_name = True
def __init__(
self,
entry: RainMachineConfigEntry,
data: RainMachineData,
description: RainMachineEntityDescription,
) -> None:
"""Initialize."""
super().__init__(data.coordinators[description.api_category])
self._attr_extra_state_attributes = {}
self._attr_unique_id = f"{data.controller.mac}_{description.key}"
self._entry = entry
self._data = data
self._version_coordinator = data.coordinators[DATA_API_VERSIONS]
self.entity_description = description
@property
def device_info(self) -> DeviceInfo:
"""Return device information about this controller."""
return DeviceInfo(
identifiers={(DOMAIN, self._data.controller.mac)},
configuration_url=(
f"https://{self._entry.data[CONF_IP_ADDRESS]}:"
f"{self._entry.data[CONF_PORT]}"
),
connections={(dr.CONNECTION_NETWORK_MAC, self._data.controller.mac)},
name=self._data.controller.name.capitalize(),
manufacturer="RainMachine",
model=(
f"Version {self._version_coordinator.data['hwVer']} "
f"(API: {self._version_coordinator.data['apiVer']})"
),
sw_version=self._version_coordinator.data["swVer"],
)
@callback
def _handle_coordinator_update(self) -> None:
"""Respond to a DataUpdateCoordinator update."""
self.update_from_latest_data()
self.async_write_ha_state()
async def async_added_to_hass(self) -> None:
"""When entity is added to hass."""
await super().async_added_to_hass()
self.async_on_remove(
self._version_coordinator.async_add_listener(
self._handle_coordinator_update, self.coordinator_context
)
)
self.update_from_latest_data()
@callback
def update_from_latest_data(self) -> None:
"""Update the state."""

View File

@ -1,12 +0,0 @@
"""Define RainMachine data models."""
from dataclasses import dataclass
from homeassistant.helpers.entity import EntityDescription
@dataclass(frozen=True, kw_only=True)
class RainMachineEntityDescription(EntityDescription):
"""Describe a RainMachine entity."""
api_category: str

View File

@ -14,9 +14,9 @@ from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM, UnitSystem from homeassistant.util.unit_system import US_CUSTOMARY_SYSTEM, UnitSystem
from . import RainMachineConfigEntry, RainMachineData, RainMachineEntity from . import RainMachineConfigEntry, RainMachineData
from .const import DATA_RESTRICTIONS_UNIVERSAL from .const import DATA_RESTRICTIONS_UNIVERSAL
from .model import RainMachineEntityDescription from .entity import RainMachineEntity, RainMachineEntityDescription
from .util import key_exists from .util import key_exists

View File

@ -20,9 +20,9 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util.dt import utc_from_timestamp, utcnow from homeassistant.util.dt import utc_from_timestamp, utcnow
from . import RainMachineConfigEntry, RainMachineData, RainMachineEntity from . import RainMachineConfigEntry, RainMachineData
from .const import DATA_PROGRAMS, DATA_PROVISION_SETTINGS, DATA_ZONES from .const import DATA_PROGRAMS, DATA_PROVISION_SETTINGS, DATA_ZONES
from .model import RainMachineEntityDescription from .entity import RainMachineEntity, RainMachineEntityDescription
from .util import ( from .util import (
RUN_STATE_MAP, RUN_STATE_MAP,
EntityDomainReplacementStrategy, EntityDomainReplacementStrategy,

View File

@ -20,12 +20,7 @@ from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import VolDictType from homeassistant.helpers.typing import VolDictType
from . import ( from . import RainMachineConfigEntry, RainMachineData, async_update_programs_and_zones
RainMachineConfigEntry,
RainMachineData,
RainMachineEntity,
async_update_programs_and_zones,
)
from .const import ( from .const import (
CONF_ALLOW_INACTIVE_ZONES_TO_RUN, CONF_ALLOW_INACTIVE_ZONES_TO_RUN,
CONF_DEFAULT_ZONE_RUN_TIME, CONF_DEFAULT_ZONE_RUN_TIME,
@ -37,7 +32,7 @@ from .const import (
DATA_ZONES, DATA_ZONES,
DEFAULT_ZONE_RUN, DEFAULT_ZONE_RUN,
) )
from .model import RainMachineEntityDescription from .entity import RainMachineEntity, RainMachineEntityDescription
from .util import RUN_STATE_MAP, key_exists from .util import RUN_STATE_MAP, key_exists
ATTR_ACTIVITY_TYPE = "activity_type" ATTR_ACTIVITY_TYPE = "activity_type"

View File

@ -16,9 +16,9 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import RainMachineConfigEntry, RainMachineEntity from . import RainMachineConfigEntry
from .const import DATA_MACHINE_FIRMWARE_UPDATE_STATUS from .const import DATA_MACHINE_FIRMWARE_UPDATE_STATUS
from .model import RainMachineEntityDescription from .entity import RainMachineEntity, RainMachineEntityDescription
class UpdateStates(Enum): class UpdateStates(Enum):