1 """Switcher integration Cover 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, ShutterDirection, SwitcherShutter
23 from .const
import SIGNAL_DEVICE_ADD
24 from .coordinator
import SwitcherDataUpdateCoordinator
25 from .entity
import SwitcherEntity
27 _LOGGER = logging.getLogger(__name__)
29 API_SET_POSITON =
"set_position"
30 API_STOP =
"stop_shutter"
35 config_entry: ConfigEntry,
36 async_add_entities: AddEntitiesCallback,
38 """Set up Switcher cover from config entry."""
41 def async_add_cover(coordinator: SwitcherDataUpdateCoordinator) ->
None:
42 """Add cover from Switcher device."""
43 entities: list[CoverEntity] = []
45 if coordinator.data.device_type.category
in (
46 DeviceCategory.SHUTTER,
47 DeviceCategory.SINGLE_SHUTTER_DUAL_LIGHT,
48 DeviceCategory.DUAL_SHUTTER_SINGLE_LIGHT,
50 number_of_covers = len(cast(SwitcherShutter, coordinator.data).position)
51 if number_of_covers == 1:
56 for i
in range(number_of_covers)
60 config_entry.async_on_unload(
66 """Representation of a Switcher cover entity."""
68 _attr_device_class = CoverDeviceClass.SHUTTER
69 _attr_supported_features = (
70 CoverEntityFeature.OPEN
71 | CoverEntityFeature.CLOSE
72 | CoverEntityFeature.SET_POSITION
73 | CoverEntityFeature.STOP
79 """Handle updated data from the coordinator."""
84 """Update data from device."""
85 data = cast(SwitcherShutter, self.coordinator.data)
89 data.direction[self._cover_id] == ShutterDirection.SHUTTER_DOWN
92 data.direction[self._cover_id] == ShutterDirection.SHUTTER_UP
96 """Call Switcher API."""
97 _LOGGER.debug(
"Calling api for %s, api: '%s', args: %s", self.
namename, api, args)
98 response: SwitcherBaseResponse |
None =
None
102 async
with SwitcherApi(
103 self.coordinator.data.device_type,
104 self.coordinator.data.ip_address,
105 self.coordinator.data.device_id,
106 self.coordinator.data.device_key,
107 self.coordinator.token,
109 response = await getattr(swapi, api)(*args)
110 except (TimeoutError, OSError, RuntimeError)
as err:
113 if error
or not response
or not response.successful:
114 self.coordinator.last_update_success =
False
117 f
"Call api for {self.name} failed, api: '{api}', "
118 f
"args: {args}, response/error: {response or error}"
123 await self.
_async_call_api_async_call_api(API_SET_POSITON, 0, self._cover_id)
127 await self.
_async_call_api_async_call_api(API_SET_POSITON, 100, self._cover_id)
130 """Move the cover to a specific position."""
132 API_SET_POSITON, kwargs[ATTR_POSITION], self._cover_id
136 """Stop the cover."""
141 """Representation of a Switcher single cover entity."""
147 coordinator: SwitcherDataUpdateCoordinator,
150 """Initialize the entity."""
154 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.device_id}-{coordinator.mac_address}"
160 """Representation of a Switcher multiple cover entity."""
162 _attr_translation_key =
"cover"
166 coordinator: SwitcherDataUpdateCoordinator,
169 """Initialize the entity."""
175 f
"{coordinator.device_id}-{coordinator.mac_address}-{cover_id}"
None async_set_cover_position(self, **Any kwargs)
None async_close_cover(self, **Any kwargs)
None async_open_cover(self, **Any kwargs)
None async_stop_cover(self, **Any kwargs)
None _async_call_api(self, str api, *Any args)
_attr_current_cover_position
None _handle_coordinator_update(self)
_attr_translation_placeholders
None __init__(self, SwitcherDataUpdateCoordinator coordinator, int cover_id)
None __init__(self, SwitcherDataUpdateCoordinator coordinator, int cover_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)