from homeassistant.components.switch import SwitchEntity from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.update_coordinator import CoordinatorEntity from .coordinator import OneControlCoordinator from .const import DOMAIN, SWITCH_DEVICES async def async_setup_entry( hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback ) -> None: coordinator: OneControlCoordinator = hass.data[DOMAIN][entry.entry_id] async_add_entities( OneControlSwitch(coordinator, device_id, name) for device_id, name in SWITCH_DEVICES.items() ) class OneControlSwitch(CoordinatorEntity, SwitchEntity): def __init__(self, coordinator: OneControlCoordinator, device_id: int, name: str) -> None: super().__init__(coordinator) self._device_id = device_id self._attr_name = name self._attr_unique_id = f"{coordinator.address}_{device_id}" @property def is_on(self) -> bool | None: return self.coordinator.get_switch_state(self._device_id) @property def available(self) -> bool: return self.coordinator.is_connected async def async_turn_on(self, **kwargs) -> None: await self.coordinator.async_set_switch(self._device_id, True) async def async_turn_off(self, **kwargs) -> None: await self.coordinator.async_set_switch(self._device_id, False)