1 """Support for WiLight switches."""
3 from __future__
import annotations
7 from pywilight.const
import ITEM_SWITCH, SWITCH_PAUSE_VALVE, SWITCH_VALVE
8 from pywilight.wilight_device
import PyWiLightDevice
9 import voluptuous
as vol
17 from .const
import DOMAIN
18 from .entity
import WiLightDevice
19 from .parent_device
import WiLightParent
20 from .support
import wilight_to_hass_trigger, wilight_trigger
as wl_trigger
23 ATTR_WATERING_TIME =
"watering_time"
24 ATTR_PAUSE_TIME =
"pause_time"
25 ATTR_TRIGGER_1 =
"trigger_1"
26 ATTR_TRIGGER_2 =
"trigger_2"
27 ATTR_TRIGGER_3 =
"trigger_3"
28 ATTR_TRIGGER_4 =
"trigger_4"
29 ATTR_TRIGGER_1_DESC =
"trigger_1_description"
30 ATTR_TRIGGER_2_DESC =
"trigger_2_description"
31 ATTR_TRIGGER_3_DESC =
"trigger_3_description"
32 ATTR_TRIGGER_4_DESC =
"trigger_4_description"
35 ATTR_TRIGGER =
"trigger"
36 ATTR_TRIGGER_INDEX =
"trigger_index"
39 SERVICE_SET_WATERING_TIME =
"set_watering_time"
40 SERVICE_SET_PAUSE_TIME =
"set_pause_time"
41 SERVICE_SET_TRIGGER =
"set_trigger"
44 RANGE_WATERING_TIME = 1800
46 RANGE_TRIGGER_INDEX = 4
49 VALID_WATERING_TIME = vol.All(
50 vol.Coerce(int), vol.Range(min=1, max=RANGE_WATERING_TIME)
52 VALID_PAUSE_TIME = vol.All(vol.Coerce(int), vol.Range(min=1, max=RANGE_PAUSE_TIME))
53 VALID_TRIGGER_INDEX = vol.All(
54 vol.Coerce(int), vol.Range(min=1, max=RANGE_TRIGGER_INDEX)
58 DESC_WATERING =
"watering"
63 """Parse configuration and add WiLight switch entities."""
65 for item
in api_device.items:
66 if item[
"type"] == ITEM_SWITCH:
68 item_name = item[
"name"]
69 if item[
"sub_type"] == SWITCH_VALVE:
71 elif item[
"sub_type"] == SWITCH_PAUSE_VALVE:
78 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
80 """Set up WiLight switches from a config entry."""
81 parent: WiLightParent = hass.data[DOMAIN][entry.entry_id]
89 async
def set_watering_time(entity, service: Any) ->
None:
90 if not isinstance(entity, WiLightValveSwitch):
91 raise TypeError(
"Entity is not a WiLight valve switch")
92 watering_time = service.data[ATTR_WATERING_TIME]
93 await entity.async_set_watering_time(watering_time=watering_time)
95 async
def set_trigger(entity, service: Any) ->
None:
96 if not isinstance(entity, WiLightValveSwitch):
97 raise TypeError(
"Entity is not a WiLight valve switch")
98 trigger_index = service.data[ATTR_TRIGGER_INDEX]
99 trigger = service.data[ATTR_TRIGGER]
100 await entity.async_set_trigger(trigger_index=trigger_index, trigger=trigger)
102 async
def set_pause_time(entity, service: Any) ->
None:
103 if not isinstance(entity, WiLightValvePauseSwitch):
104 raise TypeError(
"Entity is not a WiLight valve pause switch")
105 pause_time = service.data[ATTR_PAUSE_TIME]
106 await entity.async_set_pause_time(pause_time=pause_time)
108 platform = entity_platform.async_get_current_platform()
110 platform.async_register_entity_service(
111 SERVICE_SET_WATERING_TIME,
113 vol.Required(ATTR_WATERING_TIME): VALID_WATERING_TIME,
118 platform.async_register_entity_service(
121 vol.Required(ATTR_TRIGGER_INDEX): VALID_TRIGGER_INDEX,
122 vol.Required(ATTR_TRIGGER): wl_trigger,
127 platform.async_register_entity_service(
128 SERVICE_SET_PAUSE_TIME,
130 vol.Required(ATTR_PAUSE_TIME): VALID_PAUSE_TIME,
137 """Convert wilight pause_time seconds to hass hour."""
138 return round(value / 3600)
142 """Convert hass pause_time hours to wilight seconds."""
143 return round(value * 3600)
147 """Representation of a WiLights Valve switch."""
149 _attr_translation_key =
"watering"
153 """Return true if device is on."""
158 """Return watering time of valve switch.
160 None is unknown, 1 is minimum, 1800 is maximum.
162 return self.
_status_status.
get(
"timer_target")
166 """Return trigger_1 of valve switch."""
171 """Return trigger_2 of valve switch."""
176 """Return trigger_3 of valve switch."""
181 """Return trigger_4 of valve switch."""
186 """Return trigger_1_description of valve switch."""
191 """Return trigger_2_description of valve switch."""
196 """Return trigger_3_description of valve switch."""
201 """Return trigger_4_description of valve switch."""
206 """Return the state attributes."""
207 attr: dict[str, Any] = {}
213 attr[ATTR_TRIGGER_1] = self.
trigger_1trigger_1
216 attr[ATTR_TRIGGER_2] = self.
trigger_2trigger_2
219 attr[ATTR_TRIGGER_3] = self.
trigger_3trigger_3
222 attr[ATTR_TRIGGER_4] = self.
trigger_4trigger_4
239 """Turn the device on."""
243 """Turn the device off."""
247 """Set the watering time."""
248 await self.
_client_client.set_switch_time(self.
_index_index, watering_time)
251 """Set the trigger according to index."""
252 if trigger_index == 1:
253 await self.
_client_client.set_switch_trigger_1(self.
_index_index, trigger)
254 if trigger_index == 2:
255 await self.
_client_client.set_switch_trigger_2(self.
_index_index, trigger)
256 if trigger_index == 3:
257 await self.
_client_client.set_switch_trigger_3(self.
_index_index, trigger)
258 if trigger_index == 4:
259 await self.
_client_client.set_switch_trigger_4(self.
_index_index, trigger)
263 """Representation of a WiLights Valve Pause switch."""
265 _attr_translation_key =
"pause"
269 """Return true if device is on."""
274 """Return pause time of valve switch.
276 None is unknown, 1 is minimum, 24 is maximum.
278 pause_time = self.
_status_status.
get(
"timer_target")
279 if pause_time
is not None:
285 """Return the state attributes."""
286 attr: dict[str, Any] = {}
289 attr[ATTR_PAUSE_TIME] = self.
pause_timepause_time
294 """Turn the device on."""
298 """Turn the device off."""
302 """Set the pause time."""
304 await self.
_client_client.set_switch_time(self.
_index_index, target_time)
None async_turn_off(self, **Any kwargs)
dict[str, Any] extra_state_attributes(self)
None async_set_pause_time(self, int pause_time)
int|None pause_time(self)
None async_turn_on(self, **Any kwargs)
str|None trigger_3_description(self)
None async_turn_off(self, **Any kwargs)
str|None trigger_4_description(self)
None async_set_watering_time(self, int watering_time)
None async_turn_on(self, **Any kwargs)
None async_set_trigger(self, int trigger_index, str trigger)
int|None watering_time(self)
str|None trigger_1_description(self)
dict[str, Any] extra_state_attributes(self)
str|None trigger_2_description(self)
None turn_off(self, **Any kwargs)
None turn_on(self, **Any kwargs)
web.Response get(self, web.Request request, str config_key)
str|None wilight_to_hass_trigger(str|None value)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
int wilight_to_hass_pause_time(int value)
int hass_to_wilight_pause_time(int value)
tuple[Any] entities_from_discovered_wilight(PyWiLightDevice api_device)