1 """Implement a iotty Light Switch Device."""
3 from __future__
import annotations
8 from iottycloud.device
import Device
9 from iottycloud.lightswitch
import LightSwitch
10 from iottycloud.verbs
import LS_DEVICE_TYPE_UID
16 from .
import IottyConfigEntry
17 from .api
import IottyProxy
18 from .coordinator
import IottyDataUpdateCoordinator
19 from .entity
import IottyEntity
21 _LOGGER = logging.getLogger(__name__)
26 config_entry: IottyConfigEntry,
27 async_add_entities: AddEntitiesCallback,
29 """Activate the iotty LightSwitch component."""
30 _LOGGER.debug(
"Setup SWITCH entry id is %s", config_entry.entry_id)
32 coordinator = config_entry.runtime_data.coordinator
35 coordinator=coordinator, iotty_cloud=coordinator.iotty, iotty_device=d
37 for d
in coordinator.data.devices
38 if d.device_type == LS_DEVICE_TYPE_UID
39 if (isinstance(d, LightSwitch))
41 _LOGGER.debug(
"Found %d LightSwitches", len(entities))
45 known_devices: set = config_entry.runtime_data.known_devices
46 for known_device
in coordinator.data.devices:
47 if known_device.device_type == LS_DEVICE_TYPE_UID:
48 known_devices.add(known_device)
51 def async_update_data() -> None:
52 """Handle updated data from the API endpoint."""
53 if not coordinator.last_update_success:
56 devices = coordinator.data.devices
58 known_devices: set = config_entry.runtime_data.known_devices
61 for device
in devices:
63 any(d.device_id == device.device_id
for d
in known_devices)
64 or device.device_type != LS_DEVICE_TYPE_UID
69 coordinator=coordinator,
70 iotty_cloud=coordinator.iotty,
71 iotty_device=LightSwitch(
79 entities.extend([iotty_entity])
80 known_devices.add(device)
85 coordinator.async_add_listener(async_update_data)
89 """Haas entity class for iotty LightSwitch."""
91 _attr_device_class = SwitchDeviceClass.SWITCH
92 _iotty_device: LightSwitch
96 coordinator: IottyDataUpdateCoordinator,
97 iotty_cloud: IottyProxy,
98 iotty_device: LightSwitch,
100 """Initialize the LightSwitch device."""
101 super().
__init__(coordinator, iotty_cloud, iotty_device)
105 """Return true if the LightSwitch is on."""
107 "Retrieve device status for %s ? %s",
114 """Turn the LightSwitch on."""
115 _LOGGER.debug(
"[%s] Turning on", self.
_iotty_device_iotty_device.device_id)
122 """Turn the LightSwitch off."""
123 _LOGGER.debug(
"[%s] Turning off", self.
_iotty_device_iotty_device.device_id)
131 """Handle updated data from the coordinator."""
133 device: Device = next(
135 for device
in self.coordinator.data.devices
136 if device.device_id == self.
_iotty_device_iotty_device.device_id
138 if isinstance(device, LightSwitch):
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None __init__(self, IottyDataUpdateCoordinator coordinator, IottyProxy iotty_cloud, LightSwitch iotty_device)
None _handle_coordinator_update(self)
None async_write_ha_state(self)
None async_request_refresh(self)
None async_setup_entry(HomeAssistant hass, IottyConfigEntry config_entry, AddEntitiesCallback async_add_entities)