2024-08-30 13:12:49 +00:00
|
|
|
"""Support for switch entities."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from typing import Any
|
|
|
|
|
2024-09-04 13:52:41 +00:00
|
|
|
from thinqconnect import DeviceType
|
2024-08-30 13:12:49 +00:00
|
|
|
from thinqconnect.devices.const import Property as ThinQProperty
|
2024-09-10 08:59:07 +00:00
|
|
|
from thinqconnect.integration import ActiveMode
|
2024-08-30 13:12:49 +00:00
|
|
|
|
|
|
|
from homeassistant.components.switch import (
|
|
|
|
SwitchDeviceClass,
|
|
|
|
SwitchEntity,
|
|
|
|
SwitchEntityDescription,
|
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from . import ThinqConfigEntry
|
|
|
|
from .entity import ThinQEntity
|
|
|
|
|
2024-09-04 13:52:41 +00:00
|
|
|
DEVICE_TYPE_SWITCH_MAP: dict[DeviceType, tuple[SwitchEntityDescription, ...]] = {
|
2024-08-30 13:12:49 +00:00
|
|
|
DeviceType.AIR_PURIFIER_FAN: (
|
2024-09-04 13:52:41 +00:00
|
|
|
SwitchEntityDescription(
|
|
|
|
key=ThinQProperty.AIR_FAN_OPERATION_MODE, translation_key="operation_power"
|
|
|
|
),
|
2024-08-30 13:12:49 +00:00
|
|
|
),
|
|
|
|
DeviceType.AIR_PURIFIER: (
|
2024-09-04 13:52:41 +00:00
|
|
|
SwitchEntityDescription(
|
|
|
|
key=ThinQProperty.AIR_PURIFIER_OPERATION_MODE,
|
|
|
|
translation_key="operation_power",
|
|
|
|
),
|
2024-08-30 13:12:49 +00:00
|
|
|
),
|
|
|
|
DeviceType.DEHUMIDIFIER: (
|
2024-09-04 13:52:41 +00:00
|
|
|
SwitchEntityDescription(
|
|
|
|
key=ThinQProperty.DEHUMIDIFIER_OPERATION_MODE,
|
|
|
|
translation_key="operation_power",
|
|
|
|
),
|
2024-08-30 13:12:49 +00:00
|
|
|
),
|
|
|
|
DeviceType.HUMIDIFIER: (
|
2024-09-04 13:52:41 +00:00
|
|
|
SwitchEntityDescription(
|
|
|
|
key=ThinQProperty.HUMIDIFIER_OPERATION_MODE,
|
|
|
|
translation_key="operation_power",
|
|
|
|
),
|
2024-08-30 13:12:49 +00:00
|
|
|
),
|
|
|
|
DeviceType.SYSTEM_BOILER: (
|
2024-09-04 13:52:41 +00:00
|
|
|
SwitchEntityDescription(
|
|
|
|
key=ThinQProperty.BOILER_OPERATION_MODE, translation_key="operation_power"
|
|
|
|
),
|
2024-08-30 13:12:49 +00:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ThinqConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up an entry for switch platform."""
|
|
|
|
entities: list[ThinQSwitchEntity] = []
|
|
|
|
for coordinator in entry.runtime_data.values():
|
|
|
|
if (
|
2024-09-04 13:52:41 +00:00
|
|
|
descriptions := DEVICE_TYPE_SWITCH_MAP.get(
|
|
|
|
coordinator.api.device.device_type
|
2024-08-30 13:12:49 +00:00
|
|
|
)
|
|
|
|
) is not None:
|
|
|
|
for description in descriptions:
|
|
|
|
entities.extend(
|
2024-09-04 13:52:41 +00:00
|
|
|
ThinQSwitchEntity(coordinator, description, property_id)
|
2024-09-10 08:59:07 +00:00
|
|
|
for property_id in coordinator.api.get_active_idx(
|
|
|
|
description.key, ActiveMode.READ_WRITE
|
|
|
|
)
|
2024-08-30 13:12:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if entities:
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
|
|
|
|
class ThinQSwitchEntity(ThinQEntity, SwitchEntity):
|
|
|
|
"""Represent a thinq switch platform."""
|
|
|
|
|
|
|
|
_attr_device_class = SwitchDeviceClass.SWITCH
|
|
|
|
|
|
|
|
def _update_status(self) -> None:
|
|
|
|
"""Update status itself."""
|
|
|
|
super()._update_status()
|
|
|
|
|
2024-09-04 13:52:41 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"[%s:%s] update status: %s",
|
|
|
|
self.coordinator.device_name,
|
|
|
|
self.property_id,
|
|
|
|
self.data.is_on,
|
|
|
|
)
|
|
|
|
self._attr_is_on = self.data.is_on
|
2024-08-30 13:12:49 +00:00
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn on the switch."""
|
|
|
|
_LOGGER.debug("[%s] async_turn_on", self.name)
|
2024-09-04 13:52:41 +00:00
|
|
|
await self.async_call_api(self.coordinator.api.async_turn_on(self.property_id))
|
2024-08-30 13:12:49 +00:00
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn off the switch."""
|
|
|
|
_LOGGER.debug("[%s] async_turn_off", self.name)
|
2024-09-04 13:52:41 +00:00
|
|
|
await self.async_call_api(self.coordinator.api.async_turn_off(self.property_id))
|