1 """Switcher integration Light platform."""
3 from __future__
import annotations
6 from typing
import Any, cast
8 from aioswitcher.api
import SwitcherApi, SwitcherBaseResponse
9 from aioswitcher.device
import DeviceCategory, DeviceState, SwitcherLight
18 from .const
import SIGNAL_DEVICE_ADD
19 from .coordinator
import SwitcherDataUpdateCoordinator
20 from .entity
import SwitcherEntity
22 _LOGGER = logging.getLogger(__name__)
24 API_SET_LIGHT =
"set_light"
29 config_entry: ConfigEntry,
30 async_add_entities: AddEntitiesCallback,
32 """Set up Switcher light from a config entry."""
35 def async_add_light(coordinator: SwitcherDataUpdateCoordinator) ->
None:
36 """Add light from Switcher device."""
37 entities: list[LightEntity] = []
39 if coordinator.data.device_type.category
in (
40 DeviceCategory.SINGLE_SHUTTER_DUAL_LIGHT,
41 DeviceCategory.DUAL_SHUTTER_SINGLE_LIGHT,
44 number_of_lights = len(cast(SwitcherLight, coordinator.data).light)
45 if number_of_lights == 1:
50 for i
in range(number_of_lights)
54 config_entry.async_on_unload(
60 """Representation of a Switcher light entity."""
62 _attr_color_mode = ColorMode.ONOFF
63 _attr_supported_color_modes = {ColorMode.ONOFF}
64 control_result: bool |
None =
None
69 """When device updates, clear control result that overrides state."""
75 """Return True if entity is on."""
79 data = cast(SwitcherLight, self.coordinator.data)
80 return bool(data.light[self._light_id] == DeviceState.ON)
83 """Call Switcher API."""
84 _LOGGER.debug(
"Calling api for %s, api: '%s', args: %s", self.
namename, api, args)
85 response: SwitcherBaseResponse |
None =
None
89 async
with SwitcherApi(
90 self.coordinator.data.device_type,
91 self.coordinator.data.ip_address,
92 self.coordinator.data.device_id,
93 self.coordinator.data.device_key,
94 self.coordinator.token,
96 response = await getattr(swapi, api)(*args)
97 except (TimeoutError, OSError, RuntimeError)
as err:
100 if error
or not response
or not response.successful:
101 self.coordinator.last_update_success =
False
104 f
"Call api for {self.name} failed, api: '{api}', "
105 f
"args: {args}, response/error: {response or error}"
109 """Turn the light on."""
110 await self.
_async_call_api_async_call_api(API_SET_LIGHT, DeviceState.ON, self._light_id)
115 """Turn the light off."""
116 await self.
_async_call_api_async_call_api(API_SET_LIGHT, DeviceState.OFF, self._light_id)
122 """Representation of a Switcher single light entity."""
128 coordinator: SwitcherDataUpdateCoordinator,
131 """Initialize the entity."""
137 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.device_id}-{coordinator.mac_address}"
141 """Representation of a Switcher multiple light entity."""
143 _attr_translation_key =
"light"
147 coordinator: SwitcherDataUpdateCoordinator,
150 """Initialize the entity."""
158 f
"{coordinator.device_id}-{coordinator.mac_address}-{light_id}"
None async_turn_off(self, **Any kwargs)
None async_turn_on(self, **Any kwargs)
None _handle_coordinator_update(self)
None _async_call_api(self, str api, *Any args)
_attr_translation_placeholders
None __init__(self, SwitcherDataUpdateCoordinator coordinator, int light_id)
None __init__(self, SwitcherDataUpdateCoordinator coordinator, int light_id)
None async_write_ha_state(self)
str|UndefinedType|None name(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)