mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Move rainmachine base entity to separate module (#126513)
This commit is contained in:
parent
a75a513531
commit
c8e3e2ce1b
@ -31,8 +31,7 @@ from homeassistant.helpers import (
|
||||
device_registry as dr,
|
||||
entity_registry as er,
|
||||
)
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity, UpdateFailed
|
||||
from homeassistant.helpers.update_coordinator import UpdateFailed
|
||||
from homeassistant.util.dt import as_timestamp, utcnow
|
||||
from homeassistant.util.network import is_ip_address
|
||||
|
||||
@ -54,7 +53,6 @@ from .const import (
|
||||
LOGGER,
|
||||
)
|
||||
from .coordinator import RainMachineDataUpdateCoordinator
|
||||
from .model import RainMachineEntityDescription
|
||||
|
||||
DEFAULT_SSL = True
|
||||
|
||||
@ -528,64 +526,3 @@ async def async_reload_entry(
|
||||
) -> None:
|
||||
"""Handle an options update."""
|
||||
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."""
|
||||
|
@ -11,9 +11,9 @@ from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import RainMachineConfigEntry, RainMachineEntity
|
||||
from . import RainMachineConfigEntry
|
||||
from .const import DATA_PROVISION_SETTINGS, DATA_RESTRICTIONS_CURRENT
|
||||
from .model import RainMachineEntityDescription
|
||||
from .entity import RainMachineEntity, RainMachineEntityDescription
|
||||
from .util import (
|
||||
EntityDomainReplacementStrategy,
|
||||
async_finish_entity_domain_replacements,
|
||||
|
@ -19,9 +19,9 @@ from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_send
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import RainMachineConfigEntry, RainMachineEntity
|
||||
from . import RainMachineConfigEntry
|
||||
from .const import DATA_PROVISION_SETTINGS
|
||||
from .model import RainMachineEntityDescription
|
||||
from .entity import RainMachineEntity, RainMachineEntityDescription
|
||||
|
||||
|
||||
@dataclass(frozen=True, kw_only=True)
|
||||
|
84
homeassistant/components/rainmachine/entity.py
Normal file
84
homeassistant/components/rainmachine/entity.py
Normal 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."""
|
@ -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
|
@ -14,9 +14,9 @@ from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
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 .model import RainMachineEntityDescription
|
||||
from .entity import RainMachineEntity, RainMachineEntityDescription
|
||||
from .util import key_exists
|
||||
|
||||
|
||||
|
@ -20,9 +20,9 @@ from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
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 .model import RainMachineEntityDescription
|
||||
from .entity import RainMachineEntity, RainMachineEntityDescription
|
||||
from .util import (
|
||||
RUN_STATE_MAP,
|
||||
EntityDomainReplacementStrategy,
|
||||
|
@ -20,12 +20,7 @@ from homeassistant.helpers import config_validation as cv, entity_platform
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import VolDictType
|
||||
|
||||
from . import (
|
||||
RainMachineConfigEntry,
|
||||
RainMachineData,
|
||||
RainMachineEntity,
|
||||
async_update_programs_and_zones,
|
||||
)
|
||||
from . import RainMachineConfigEntry, RainMachineData, async_update_programs_and_zones
|
||||
from .const import (
|
||||
CONF_ALLOW_INACTIVE_ZONES_TO_RUN,
|
||||
CONF_DEFAULT_ZONE_RUN_TIME,
|
||||
@ -37,7 +32,7 @@ from .const import (
|
||||
DATA_ZONES,
|
||||
DEFAULT_ZONE_RUN,
|
||||
)
|
||||
from .model import RainMachineEntityDescription
|
||||
from .entity import RainMachineEntity, RainMachineEntityDescription
|
||||
from .util import RUN_STATE_MAP, key_exists
|
||||
|
||||
ATTR_ACTIVITY_TYPE = "activity_type"
|
||||
|
@ -16,9 +16,9 @@ from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import RainMachineConfigEntry, RainMachineEntity
|
||||
from . import RainMachineConfigEntry
|
||||
from .const import DATA_MACHINE_FIRMWARE_UPDATE_STATUS
|
||||
from .model import RainMachineEntityDescription
|
||||
from .entity import RainMachineEntity, RainMachineEntityDescription
|
||||
|
||||
|
||||
class UpdateStates(Enum):
|
||||
|
Loading…
x
Reference in New Issue
Block a user