2024-07-19 10:10:39 +00:00
|
|
|
"""Implement a iotty Light Switch Device."""
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
2024-12-20 14:33:05 +00:00
|
|
|
from typing import TYPE_CHECKING, Any
|
2024-07-19 10:10:39 +00:00
|
|
|
|
|
|
|
from iottycloud.lightswitch import LightSwitch
|
2024-12-20 14:33:05 +00:00
|
|
|
from iottycloud.outlet import Outlet
|
|
|
|
from iottycloud.verbs import (
|
|
|
|
COMMAND_TURNOFF,
|
|
|
|
COMMAND_TURNON,
|
|
|
|
LS_DEVICE_TYPE_UID,
|
|
|
|
OU_DEVICE_TYPE_UID,
|
|
|
|
)
|
|
|
|
|
|
|
|
from homeassistant.components.switch import (
|
|
|
|
SwitchDeviceClass,
|
|
|
|
SwitchEntity,
|
|
|
|
SwitchEntityDescription,
|
|
|
|
)
|
2024-07-19 10:10:39 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2025-02-10 20:08:03 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
|
2024-07-19 10:10:39 +00:00
|
|
|
|
|
|
|
from .api import IottyProxy
|
2025-02-09 20:01:13 +00:00
|
|
|
from .coordinator import IottyConfigEntry, IottyDataUpdateCoordinator
|
2024-09-13 12:55:53 +00:00
|
|
|
from .entity import IottyEntity
|
2024-07-19 10:10:39 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2024-12-20 14:33:05 +00:00
|
|
|
ENTITIES: dict[str, SwitchEntityDescription] = {
|
|
|
|
LS_DEVICE_TYPE_UID: SwitchEntityDescription(
|
|
|
|
key="light",
|
|
|
|
name=None,
|
|
|
|
device_class=SwitchDeviceClass.SWITCH,
|
|
|
|
),
|
|
|
|
OU_DEVICE_TYPE_UID: SwitchEntityDescription(
|
|
|
|
key="outlet",
|
|
|
|
name=None,
|
|
|
|
device_class=SwitchDeviceClass.OUTLET,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
2024-07-19 10:10:39 +00:00
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: IottyConfigEntry,
|
2025-02-10 20:08:03 +00:00
|
|
|
async_add_entities: AddConfigEntryEntitiesCallback,
|
2024-07-19 10:10:39 +00:00
|
|
|
) -> None:
|
2024-12-20 14:33:05 +00:00
|
|
|
"""Activate the iotty Switch component."""
|
2024-07-19 10:10:39 +00:00
|
|
|
_LOGGER.debug("Setup SWITCH entry id is %s", config_entry.entry_id)
|
|
|
|
|
|
|
|
coordinator = config_entry.runtime_data.coordinator
|
2024-12-20 14:33:05 +00:00
|
|
|
lightswitch_entities = [
|
|
|
|
IottySwitch(
|
|
|
|
coordinator=coordinator,
|
|
|
|
iotty_cloud=coordinator.iotty,
|
|
|
|
iotty_device=d,
|
|
|
|
entity_description=ENTITIES[LS_DEVICE_TYPE_UID],
|
2024-07-19 10:10:39 +00:00
|
|
|
)
|
|
|
|
for d in coordinator.data.devices
|
|
|
|
if d.device_type == LS_DEVICE_TYPE_UID
|
|
|
|
if (isinstance(d, LightSwitch))
|
|
|
|
]
|
2024-12-20 14:33:05 +00:00
|
|
|
_LOGGER.debug("Found %d LightSwitches", len(lightswitch_entities))
|
|
|
|
|
|
|
|
outlet_entities = [
|
|
|
|
IottySwitch(
|
|
|
|
coordinator=coordinator,
|
|
|
|
iotty_cloud=coordinator.iotty,
|
|
|
|
iotty_device=d,
|
|
|
|
entity_description=ENTITIES[OU_DEVICE_TYPE_UID],
|
|
|
|
)
|
|
|
|
for d in coordinator.data.devices
|
|
|
|
if d.device_type == OU_DEVICE_TYPE_UID
|
|
|
|
if (isinstance(d, Outlet))
|
|
|
|
]
|
|
|
|
_LOGGER.debug("Found %d Outlets", len(outlet_entities))
|
|
|
|
|
|
|
|
entities = lightswitch_entities + outlet_entities
|
2024-07-19 10:10:39 +00:00
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
known_devices: set = config_entry.runtime_data.known_devices
|
|
|
|
for known_device in coordinator.data.devices:
|
2024-12-20 14:33:05 +00:00
|
|
|
if known_device.device_type in {LS_DEVICE_TYPE_UID, OU_DEVICE_TYPE_UID}:
|
2024-07-19 10:10:39 +00:00
|
|
|
known_devices.add(known_device)
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def async_update_data() -> None:
|
|
|
|
"""Handle updated data from the API endpoint."""
|
|
|
|
if not coordinator.last_update_success:
|
2024-07-31 18:21:26 +00:00
|
|
|
return
|
2024-07-19 10:10:39 +00:00
|
|
|
|
|
|
|
devices = coordinator.data.devices
|
|
|
|
entities = []
|
|
|
|
known_devices: set = config_entry.runtime_data.known_devices
|
|
|
|
|
|
|
|
# Add entities for devices which we've not yet seen
|
|
|
|
for device in devices:
|
2024-12-20 14:33:05 +00:00
|
|
|
if any(d.device_id == device.device_id for d in known_devices) or (
|
|
|
|
device.device_type not in {LS_DEVICE_TYPE_UID, OU_DEVICE_TYPE_UID}
|
2024-07-19 10:10:39 +00:00
|
|
|
):
|
|
|
|
continue
|
|
|
|
|
2024-12-20 14:33:05 +00:00
|
|
|
iotty_entity: SwitchEntity
|
|
|
|
iotty_device: LightSwitch | Outlet
|
|
|
|
if device.device_type == LS_DEVICE_TYPE_UID:
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
assert isinstance(device, LightSwitch)
|
|
|
|
iotty_device = LightSwitch(
|
2024-07-19 10:10:39 +00:00
|
|
|
device.device_id,
|
|
|
|
device.serial_number,
|
|
|
|
device.device_type,
|
|
|
|
device.device_name,
|
2024-12-20 14:33:05 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
assert isinstance(device, Outlet)
|
|
|
|
iotty_device = Outlet(
|
|
|
|
device.device_id,
|
|
|
|
device.serial_number,
|
|
|
|
device.device_type,
|
|
|
|
device.device_name,
|
|
|
|
)
|
|
|
|
|
|
|
|
iotty_entity = IottySwitch(
|
|
|
|
coordinator=coordinator,
|
|
|
|
iotty_cloud=coordinator.iotty,
|
|
|
|
iotty_device=iotty_device,
|
|
|
|
entity_description=ENTITIES[device.device_type],
|
2024-07-19 10:10:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
entities.extend([iotty_entity])
|
|
|
|
known_devices.add(device)
|
|
|
|
|
|
|
|
async_add_entities(entities)
|
|
|
|
|
|
|
|
# Add a subscriber to the coordinator to discover new devices
|
|
|
|
coordinator.async_add_listener(async_update_data)
|
|
|
|
|
|
|
|
|
2024-12-20 14:33:05 +00:00
|
|
|
class IottySwitch(IottyEntity, SwitchEntity):
|
|
|
|
"""Haas entity class for iotty switch."""
|
2024-07-19 10:10:39 +00:00
|
|
|
|
2024-12-20 14:33:05 +00:00
|
|
|
_attr_device_class: SwitchDeviceClass | None
|
|
|
|
_iotty_device: LightSwitch | Outlet
|
2024-07-19 10:10:39 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
coordinator: IottyDataUpdateCoordinator,
|
|
|
|
iotty_cloud: IottyProxy,
|
2024-12-20 14:33:05 +00:00
|
|
|
iotty_device: LightSwitch | Outlet,
|
|
|
|
entity_description: SwitchEntityDescription,
|
2024-07-19 10:10:39 +00:00
|
|
|
) -> None:
|
2024-12-20 14:33:05 +00:00
|
|
|
"""Initialize the Switch device."""
|
2024-09-13 12:55:53 +00:00
|
|
|
super().__init__(coordinator, iotty_cloud, iotty_device)
|
2024-12-20 14:33:05 +00:00
|
|
|
self.entity_description = entity_description
|
|
|
|
self._attr_device_class = entity_description.device_class
|
2024-07-19 10:10:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
2024-12-20 14:33:05 +00:00
|
|
|
"""Return true if the Switch is on."""
|
2024-07-19 10:10:39 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"Retrieve device status for %s ? %s",
|
|
|
|
self._iotty_device.device_id,
|
|
|
|
self._iotty_device.is_on,
|
|
|
|
)
|
|
|
|
return self._iotty_device.is_on
|
|
|
|
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2024-12-20 14:33:05 +00:00
|
|
|
"""Turn the Switch on."""
|
2024-07-19 10:10:39 +00:00
|
|
|
_LOGGER.debug("[%s] Turning on", self._iotty_device.device_id)
|
2024-12-20 14:33:05 +00:00
|
|
|
await self._iotty_cloud.command(self._iotty_device.device_id, COMMAND_TURNON)
|
2024-07-19 10:10:39 +00:00
|
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2024-12-20 14:33:05 +00:00
|
|
|
"""Turn the Switch off."""
|
2024-07-19 10:10:39 +00:00
|
|
|
_LOGGER.debug("[%s] Turning off", self._iotty_device.device_id)
|
2024-12-20 14:33:05 +00:00
|
|
|
await self._iotty_cloud.command(self._iotty_device.device_id, COMMAND_TURNOFF)
|
2024-07-19 10:10:39 +00:00
|
|
|
await self.coordinator.async_request_refresh()
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def _handle_coordinator_update(self) -> None:
|
|
|
|
"""Handle updated data from the coordinator."""
|
|
|
|
|
2024-12-20 14:33:05 +00:00
|
|
|
device: LightSwitch | Outlet = next( # type: ignore[assignment]
|
2024-07-19 10:10:39 +00:00
|
|
|
device
|
|
|
|
for device in self.coordinator.data.devices
|
|
|
|
if device.device_id == self._iotty_device.device_id
|
|
|
|
)
|
2024-12-20 14:33:05 +00:00
|
|
|
self._iotty_device.is_on = device.is_on
|
2024-07-19 10:10:39 +00:00
|
|
|
self.async_write_ha_state()
|