Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Switch representing the shutoff valve for the Flo by Moen integration."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aioflo.location import SLEEP_MINUTE_OPTIONS, SYSTEM_MODE_HOME, SYSTEM_REVERT_MODES
8 import voluptuous as vol
9 
10 from homeassistant.components.switch import SwitchEntity
11 from homeassistant.config_entries import ConfigEntry
12 from homeassistant.core import HomeAssistant, callback
13 from homeassistant.helpers import entity_platform
14 from homeassistant.helpers.entity_platform import AddEntitiesCallback
15 
16 from .const import DOMAIN as FLO_DOMAIN
17 from .coordinator import FloDeviceDataUpdateCoordinator
18 from .entity import FloEntity
19 
20 ATTR_REVERT_TO_MODE = "revert_to_mode"
21 ATTR_SLEEP_MINUTES = "sleep_minutes"
22 SERVICE_SET_SLEEP_MODE = "set_sleep_mode"
23 SERVICE_SET_AWAY_MODE = "set_away_mode"
24 SERVICE_SET_HOME_MODE = "set_home_mode"
25 SERVICE_RUN_HEALTH_TEST = "run_health_test"
26 
27 
29  hass: HomeAssistant,
30  config_entry: ConfigEntry,
31  async_add_entities: AddEntitiesCallback,
32 ) -> None:
33  """Set up the Flo switches from config entry."""
34  devices: list[FloDeviceDataUpdateCoordinator] = hass.data[FLO_DOMAIN][
35  config_entry.entry_id
36  ]["devices"]
37 
39  [FloSwitch(device) for device in devices if device.device_type != "puck_oem"]
40  )
41 
42  platform = entity_platform.async_get_current_platform()
43 
44  platform.async_register_entity_service(
45  SERVICE_SET_AWAY_MODE, None, "async_set_mode_away"
46  )
47  platform.async_register_entity_service(
48  SERVICE_SET_HOME_MODE, None, "async_set_mode_home"
49  )
50  platform.async_register_entity_service(
51  SERVICE_RUN_HEALTH_TEST, None, "async_run_health_test"
52  )
53  platform.async_register_entity_service(
54  SERVICE_SET_SLEEP_MODE,
55  {
56  vol.Required(ATTR_SLEEP_MINUTES, default=120): vol.All(
57  vol.Coerce(int),
58  vol.In(SLEEP_MINUTE_OPTIONS),
59  ),
60  vol.Required(ATTR_REVERT_TO_MODE, default=SYSTEM_MODE_HOME): vol.In(
61  SYSTEM_REVERT_MODES
62  ),
63  },
64  "async_set_mode_sleep",
65  )
66 
67 
69  """Switch class for the Flo by Moen valve."""
70 
71  _attr_translation_key = "shutoff_valve"
72 
73  def __init__(self, device: FloDeviceDataUpdateCoordinator) -> None:
74  """Initialize the Flo switch."""
75  super().__init__("shutoff_valve", device)
76  self._attr_is_on_attr_is_on = device.last_known_valve_state == "open"
77 
78  async def async_turn_on(self, **kwargs: Any) -> None:
79  """Open the valve."""
80  await self._device.api_client.device.open_valve(self._device.id)
81  self._attr_is_on_attr_is_on = True
82  self.async_write_ha_stateasync_write_ha_state()
83 
84  async def async_turn_off(self, **kwargs: Any) -> None:
85  """Close the valve."""
86  await self._device.api_client.device.close_valve(self._device.id)
87  self._attr_is_on_attr_is_on = False
88  self.async_write_ha_stateasync_write_ha_state()
89 
90  @callback
91  def async_update_state(self) -> None:
92  """Retrieve the latest valve state and update the state machine."""
93  self._attr_is_on_attr_is_on = self._device.last_known_valve_state == "open"
94  self.async_write_ha_stateasync_write_ha_state()
95 
96  async def async_added_to_hass(self) -> None:
97  """When entity is added to hass."""
98  await super().async_added_to_hass()
99  self.async_on_removeasync_on_remove(self._device.async_add_listener(self.async_update_stateasync_update_state))
100 
101  async def async_set_mode_home(self):
102  """Set the Flo location to home mode."""
103  await self._device.async_set_mode_home()
104 
105  async def async_set_mode_away(self):
106  """Set the Flo location to away mode."""
107  await self._device.async_set_mode_away()
108 
109  async def async_set_mode_sleep(self, sleep_minutes, revert_to_mode):
110  """Set the Flo location to sleep mode."""
111  await self._device.async_set_mode_sleep(sleep_minutes, revert_to_mode)
112 
113  async def async_run_health_test(self):
114  """Run a Flo device health test."""
115  await self._device.async_run_health_test()
None async_turn_on(self, **Any kwargs)
Definition: switch.py:78
def async_set_mode_sleep(self, sleep_minutes, revert_to_mode)
Definition: switch.py:109
None async_turn_off(self, **Any kwargs)
Definition: switch.py:84
None __init__(self, FloDeviceDataUpdateCoordinator device)
Definition: switch.py:73
None async_on_remove(self, CALLBACK_TYPE func)
Definition: entity.py:1331
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:32
None async_add_listener(HomeAssistant hass, Callable[[], None] listener)
Definition: __init__.py:82