Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Melnor RainCloud sprinkler water timer."""
2 
3 from __future__ import annotations
4 
5 import logging
6 from typing import Any
7 
8 import voluptuous as vol
9 
11  PLATFORM_SCHEMA as SWITCH_PLATFORM_SCHEMA,
12  SwitchEntity,
13 )
14 from homeassistant.const import CONF_MONITORED_CONDITIONS
15 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
19 
20 from .const import DATA_RAINCLOUD
21 from .entity import RainCloudEntity
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 ALLOWED_WATERING_TIME = [5, 10, 15, 30, 45, 60]
26 CONF_WATERING_TIME = "watering_minutes"
27 DEFAULT_WATERING_TIME = 15
28 
29 SWITCHES = ["auto_watering", "manual_watering"]
30 
31 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
32  {
33  vol.Optional(CONF_MONITORED_CONDITIONS, default=list(SWITCHES)): vol.All(
34  cv.ensure_list, [vol.In(SWITCHES)]
35  ),
36  vol.Optional(CONF_WATERING_TIME, default=DEFAULT_WATERING_TIME): vol.All(
37  vol.In(ALLOWED_WATERING_TIME)
38  ),
39  }
40 )
41 
42 
44  hass: HomeAssistant,
45  config: ConfigType,
46  add_entities: AddEntitiesCallback,
47  discovery_info: DiscoveryInfoType | None = None,
48 ) -> None:
49  """Set up a sensor for a raincloud device."""
50  raincloud = hass.data[DATA_RAINCLOUD].data
51  default_watering_timer = config[CONF_WATERING_TIME]
52 
54  (
55  RainCloudSwitch(default_watering_timer, zone, sensor_type)
56  for zone in raincloud.controller.faucet.zones
57  for sensor_type in config[CONF_MONITORED_CONDITIONS]
58  ),
59  True,
60  )
61 
62 
64  """A switch implementation for raincloud device."""
65 
66  def __init__(self, default_watering_timer, *args):
67  """Initialize a switch for raincloud device."""
68  super().__init__(*args)
69  self._default_watering_timer_default_watering_timer = default_watering_timer
70 
71  @property
72  def is_on(self):
73  """Return true if device is on."""
74  return self._state_state_state
75 
76  def turn_on(self, **kwargs: Any) -> None:
77  """Turn the device on."""
78  if self._sensor_type_sensor_type_sensor_type == "manual_watering":
79  self.datadata.watering_time = self._default_watering_timer_default_watering_timer
80  elif self._sensor_type_sensor_type_sensor_type == "auto_watering":
81  self.datadata.auto_watering = True
82  self._state_state_state = True
83 
84  def turn_off(self, **kwargs: Any) -> None:
85  """Turn the device off."""
86  if self._sensor_type_sensor_type_sensor_type == "manual_watering":
87  self.datadata.watering_time = "off"
88  elif self._sensor_type_sensor_type_sensor_type == "auto_watering":
89  self.datadata.auto_watering = False
90  self._state_state_state = False
91 
92  def update(self) -> None:
93  """Update device state."""
94  _LOGGER.debug("Updating RainCloud switch: %s", self._name_name)
95  if self._sensor_type_sensor_type_sensor_type == "manual_watering":
96  self._state_state_state = bool(self.datadata.watering_time)
97  elif self._sensor_type_sensor_type_sensor_type == "auto_watering":
98  self._state_state_state = self.datadata.auto_watering
99 
100  @property
102  """Return the state attributes."""
103  return {
104  "default_manual_timer": self._default_watering_timer_default_watering_timer,
105  "identifier": self.datadata.serial,
106  }
def __init__(self, default_watering_timer, *args)
Definition: switch.py:66
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: switch.py:48