mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
Centralize MELCloud entity setup in base entities (#177395)
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.8
parent
7191fd8497
commit
876bfd45df
@@ -15,8 +15,8 @@ from homeassistant.const import EntityCategory
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .coordinator import MelCloudConfigEntry, MelCloudDeviceUpdateCoordinator
|
||||
from .entity import MelCloudEntity
|
||||
from .coordinator import MelCloudConfigEntry
|
||||
from .entity import MelCloudDescriptionEntity
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True, kw_only=True)
|
||||
@@ -149,24 +149,11 @@ async def async_setup_entry(
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class MelDeviceBinarySensor(MelCloudEntity, BinarySensorEntity):
|
||||
class MelDeviceBinarySensor(MelCloudDescriptionEntity, BinarySensorEntity):
|
||||
"""Representation of a Binary Sensor."""
|
||||
|
||||
entity_description: MelcloudBinarySensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: MelCloudDeviceUpdateCoordinator,
|
||||
description: MelcloudBinarySensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the binary sensor."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.device.serial}-{coordinator.device.mac}-{description.key}"
|
||||
)
|
||||
self._attr_device_info = coordinator.device_info
|
||||
|
||||
@property
|
||||
@override
|
||||
def is_on(self) -> bool | None:
|
||||
|
||||
@@ -2,6 +2,9 @@
|
||||
|
||||
from typing import override
|
||||
|
||||
from pymelcloud.atw_device import Zone
|
||||
|
||||
from homeassistant.helpers.entity import EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
|
||||
from .coordinator import MelCloudDeviceUpdateCoordinator
|
||||
@@ -17,3 +20,32 @@ class MelCloudEntity(CoordinatorEntity[MelCloudDeviceUpdateCoordinator]):
|
||||
def available(self) -> bool:
|
||||
"""Return True if entity is available."""
|
||||
return super().available and self.coordinator.device_available
|
||||
|
||||
|
||||
class MelCloudDescriptionEntity(MelCloudEntity):
|
||||
"""Base class for description-driven MELCloud device entities."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: MelCloudDeviceUpdateCoordinator,
|
||||
description: EntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the entity."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.device.serial}-{coordinator.device.mac}-{description.key}"
|
||||
)
|
||||
self._attr_device_info = coordinator.device_info
|
||||
|
||||
|
||||
class AtwZoneEntity(MelCloudEntity):
|
||||
"""Base class for Air-to-Water zone entities."""
|
||||
|
||||
def __init__(
|
||||
self, coordinator: MelCloudDeviceUpdateCoordinator, zone: Zone
|
||||
) -> None:
|
||||
"""Initialize the zone entity."""
|
||||
super().__init__(coordinator)
|
||||
self._zone = zone
|
||||
self._attr_device_info = coordinator.zone_device_info(zone)
|
||||
|
||||
@@ -24,7 +24,7 @@ from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .const import DOMAIN
|
||||
from .coordinator import MelCloudConfigEntry, MelCloudDeviceUpdateCoordinator
|
||||
from .entity import MelCloudEntity
|
||||
from .entity import AtwZoneEntity
|
||||
|
||||
FLOW_MODES = {ZONE_OPERATION_MODE_HEAT_FLOW, ZONE_OPERATION_MODE_COOL_FLOW}
|
||||
|
||||
@@ -74,7 +74,7 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
|
||||
class AtwZoneNumber(MelCloudEntity, NumberEntity):
|
||||
class AtwZoneNumber(AtwZoneEntity, NumberEntity):
|
||||
"""Number entity for an Air-to-Water zone."""
|
||||
|
||||
entity_description: MelcloudNumberEntityDescription
|
||||
@@ -86,13 +86,11 @@ class AtwZoneNumber(MelCloudEntity, NumberEntity):
|
||||
description: MelcloudNumberEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the number."""
|
||||
super().__init__(coordinator)
|
||||
self._zone = zone
|
||||
super().__init__(coordinator, zone)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.device.serial}-{zone.zone_index}-{description.key}"
|
||||
)
|
||||
self._attr_device_info = coordinator.zone_device_info(zone)
|
||||
self._attr_native_step = coordinator.device.temperature_increment
|
||||
|
||||
@property
|
||||
|
||||
@@ -17,7 +17,7 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .coordinator import MelCloudConfigEntry, MelCloudDeviceUpdateCoordinator
|
||||
from .entity import MelCloudEntity
|
||||
from .entity import AtwZoneEntity
|
||||
|
||||
OPTION_ROOM = "room"
|
||||
OPTION_FLOW = "flow"
|
||||
@@ -59,7 +59,7 @@ async def async_setup_entry(
|
||||
)
|
||||
|
||||
|
||||
class AtwZoneOperationModeSelect(MelCloudEntity, SelectEntity):
|
||||
class AtwZoneOperationModeSelect(AtwZoneEntity, SelectEntity):
|
||||
"""Select for the temperature control method of an Air-to-Water zone."""
|
||||
|
||||
_attr_translation_key = "operation_mode"
|
||||
@@ -70,12 +70,10 @@ class AtwZoneOperationModeSelect(MelCloudEntity, SelectEntity):
|
||||
zone: Zone,
|
||||
) -> None:
|
||||
"""Initialize the operation mode select."""
|
||||
super().__init__(coordinator)
|
||||
self._zone = zone
|
||||
super().__init__(coordinator, zone)
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.device.serial}-{zone.zone_index}-operation_mode"
|
||||
)
|
||||
self._attr_device_info = coordinator.zone_device_info(zone)
|
||||
available = {
|
||||
MODE_TO_OPTION[mode]
|
||||
for mode in zone.operation_modes
|
||||
|
||||
@@ -26,7 +26,7 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
||||
|
||||
from .coordinator import MelCloudConfigEntry, MelCloudDeviceUpdateCoordinator
|
||||
from .entity import MelCloudEntity
|
||||
from .entity import MelCloudDescriptionEntity
|
||||
|
||||
|
||||
@dataclasses.dataclass(frozen=True, kw_only=True)
|
||||
@@ -307,25 +307,11 @@ async def async_setup_entry(
|
||||
async_add_entities(entities)
|
||||
|
||||
|
||||
class MelDeviceSensor(MelCloudEntity, SensorEntity):
|
||||
class MelDeviceSensor(MelCloudDescriptionEntity, SensorEntity):
|
||||
"""Representation of a Sensor."""
|
||||
|
||||
entity_description: MelcloudSensorEntityDescription
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
coordinator: MelCloudDeviceUpdateCoordinator,
|
||||
description: MelcloudSensorEntityDescription,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
|
||||
self._attr_unique_id = (
|
||||
f"{coordinator.device.serial}-{coordinator.device.mac}-{description.key}"
|
||||
)
|
||||
self._attr_device_info = coordinator.device_info
|
||||
|
||||
@property
|
||||
@override
|
||||
def native_value(self) -> float | None:
|
||||
|
||||
Reference in New Issue
Block a user