78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
"""Writable zone enabled switches for GREE Controller."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from homeassistant.components.switch import SwitchEntity
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|
|
|
from . import GreeControllerRuntimeData
|
|
from .const import DOMAIN
|
|
from .coordinator import GreeControllerCoordinator
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
entry: ConfigEntry,
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
|
) -> None:
|
|
"""Create one enabled switch per controller zone."""
|
|
runtime: GreeControllerRuntimeData = entry.runtime_data
|
|
entities = [
|
|
GreeControllerZoneEnabledSwitch(runtime.coordinator, str(zone["zone_id"]))
|
|
for zone in runtime.coordinator.plan.get("zones", [])
|
|
if zone.get("zone_id")
|
|
]
|
|
async_add_entities(entities)
|
|
|
|
|
|
class GreeControllerZoneEnabledSwitch(CoordinatorEntity[GreeControllerCoordinator], SwitchEntity):
|
|
"""Enable or disable a controller thermostat zone."""
|
|
|
|
_attr_has_entity_name = True
|
|
_attr_name = "Enabled"
|
|
|
|
def __init__(self, coordinator: GreeControllerCoordinator, zone_id: str) -> None:
|
|
super().__init__(coordinator)
|
|
self._zone_id = zone_id
|
|
self._attr_unique_id = f"{zone_id}-enabled"
|
|
|
|
@property
|
|
def _zone(self) -> dict[str, Any]:
|
|
for zone in self.coordinator.plan.get("zones", []):
|
|
if str(zone.get("zone_id")) == self._zone_id:
|
|
return zone
|
|
return {}
|
|
|
|
@property
|
|
def available(self) -> bool:
|
|
return super().available and bool(self._zone)
|
|
|
|
@property
|
|
def is_on(self) -> bool:
|
|
return bool(self._zone.get("enabled", False))
|
|
|
|
@property
|
|
def device_info(self) -> DeviceInfo:
|
|
zone = self._zone
|
|
return DeviceInfo(
|
|
identifiers={(DOMAIN, f"zone:{self._zone_id}")},
|
|
name=str(zone.get("zone_name") or self._zone_id),
|
|
manufacturer="GREE Controller",
|
|
model="Zone thermostat",
|
|
via_device=(DOMAIN, "controller"),
|
|
)
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
await self.coordinator.client.zone_control(self._zone_id, {"enabled": True})
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
await self.coordinator.client.zone_control(self._zone_id, {"enabled": False})
|
|
await self.coordinator.async_request_refresh()
|