1 """Switcher integration Switch platform."""
3 from __future__
import annotations
5 from datetime
import timedelta
9 from aioswitcher.api
import Command, SwitcherApi, SwitcherBaseResponse
10 from aioswitcher.device
import DeviceCategory, DeviceState
11 import voluptuous
as vol
24 SERVICE_SET_AUTO_OFF_NAME,
25 SERVICE_TURN_ON_WITH_TIMER_NAME,
28 from .coordinator
import SwitcherDataUpdateCoordinator
29 from .entity
import SwitcherEntity
31 _LOGGER = logging.getLogger(__name__)
33 API_CONTROL_DEVICE =
"control_device"
34 API_SET_AUTO_SHUTDOWN =
"set_auto_shutdown"
36 SERVICE_SET_AUTO_OFF_SCHEMA: VolDictType = {
37 vol.Required(CONF_AUTO_OFF): cv.time_period_str,
40 SERVICE_TURN_ON_WITH_TIMER_SCHEMA: VolDictType = {
41 vol.Required(CONF_TIMER_MINUTES): vol.All(
42 cv.positive_int, vol.Range(min=1, max=150)
49 config_entry: ConfigEntry,
50 async_add_entities: AddEntitiesCallback,
52 """Set up Switcher switch from config entry."""
53 platform = entity_platform.async_get_current_platform()
55 platform.async_register_entity_service(
56 SERVICE_SET_AUTO_OFF_NAME,
57 SERVICE_SET_AUTO_OFF_SCHEMA,
58 "async_set_auto_off_service",
61 platform.async_register_entity_service(
62 SERVICE_TURN_ON_WITH_TIMER_NAME,
63 SERVICE_TURN_ON_WITH_TIMER_SCHEMA,
64 "async_turn_on_with_timer_service",
68 def async_add_switch(coordinator: SwitcherDataUpdateCoordinator) ->
None:
69 """Add switch from Switcher device."""
70 if coordinator.data.device_type.category == DeviceCategory.POWER_PLUG:
72 elif coordinator.data.device_type.category == DeviceCategory.WATER_HEATER:
75 config_entry.async_on_unload(
81 """Representation of a Switcher switch entity."""
85 def __init__(self, coordinator: SwitcherDataUpdateCoordinator) ->
None:
86 """Initialize the entity."""
91 self.
_attr_unique_id_attr_unique_id = f
"{coordinator.device_id}-{coordinator.mac_address}"
95 """When device updates, clear control result that overrides state."""
100 """Call Switcher API."""
102 "Calling api for %s, api: '%s', args: %s", self.coordinator.name, api, args
104 response: SwitcherBaseResponse |
None =
None
108 async
with SwitcherApi(
109 self.coordinator.data.device_type,
110 self.coordinator.data.ip_address,
111 self.coordinator.data.device_id,
112 self.coordinator.data.device_key,
114 response = await getattr(swapi, api)(*args)
115 except (TimeoutError, OSError, RuntimeError)
as err:
118 if error
or not response
or not response.successful:
120 "Call api for %s failed, api: '%s', args: %s, response/error: %s",
121 self.coordinator.name,
126 self.coordinator.last_update_success =
False
130 """Return True if entity is on."""
134 return bool(self.coordinator.data.device_state == DeviceState.ON)
137 """Turn the entity on."""
138 await self.
_async_call_api_async_call_api(API_CONTROL_DEVICE, Command.ON)
143 """Turn the entity off."""
144 await self.
_async_call_api_async_call_api(API_CONTROL_DEVICE, Command.OFF)
149 """Use for handling setting device auto-off service calls."""
151 "Service '%s' is not supported by %s",
152 SERVICE_SET_AUTO_OFF_NAME,
153 self.coordinator.name,
157 """Use for turning device on with a timer service calls."""
159 "Service '%s' is not supported by %s",
160 SERVICE_TURN_ON_WITH_TIMER_NAME,
161 self.coordinator.name,
166 """Representation of a Switcher power plug switch entity."""
168 _attr_device_class = SwitchDeviceClass.OUTLET
172 """Representation of a Switcher water heater switch entity."""
174 _attr_device_class = SwitchDeviceClass.SWITCH
177 """Use for handling setting device auto-off service calls."""
178 await self.
_async_call_api_async_call_api(API_SET_AUTO_SHUTDOWN, auto_off)
182 """Use for turning device on with a timer service calls."""
183 await self.
_async_call_api_async_call_api(API_CONTROL_DEVICE, Command.ON, timer_minutes)
None async_set_auto_off_service(self, timedelta auto_off)
None async_turn_on_with_timer_service(self, int timer_minutes)
None _handle_coordinator_update(self)
None async_turn_on(self, **Any kwargs)
None _async_call_api(self, str api, *Any args)
None __init__(self, SwitcherDataUpdateCoordinator coordinator)
None async_turn_off(self, **Any kwargs)
None async_set_auto_off_service(self, timedelta auto_off)
None async_turn_on_with_timer_service(self, int timer_minutes)
None async_write_ha_state(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)