Home Assistant Unofficial Reference 2024.12.1
sensor.py
Go to the documentation of this file.
1 """Platform for sensor integration."""
2 
3 from enum import Enum
4 
5 import smarttub
6 import voluptuous as vol
7 
8 from homeassistant.components.sensor import SensorEntity
9 from homeassistant.config_entries import ConfigEntry
10 from homeassistant.core import HomeAssistant
11 from homeassistant.helpers import config_validation as cv, entity_platform
12 from homeassistant.helpers.entity_platform import AddEntitiesCallback
13 from homeassistant.helpers.typing import VolDictType
14 
15 from .const import DOMAIN, SMARTTUB_CONTROLLER
16 from .entity import SmartTubSensorBase
17 
18 # the desired duration, in hours, of the cycle
19 ATTR_DURATION = "duration"
20 ATTR_CYCLE_LAST_UPDATED = "cycle_last_updated"
21 ATTR_MODE = "mode"
22 # the hour of the day at which to start the cycle (0-23)
23 ATTR_START_HOUR = "start_hour"
24 
25 SET_PRIMARY_FILTRATION_SCHEMA = vol.All(
26  cv.has_at_least_one_key(ATTR_DURATION, ATTR_START_HOUR),
27  cv.make_entity_service_schema(
28  {
29  vol.Optional(ATTR_DURATION): vol.All(int, vol.Range(min=1, max=24)),
30  vol.Optional(ATTR_START_HOUR): vol.All(int, vol.Range(min=0, max=23)),
31  },
32  ),
33 )
34 
35 SET_SECONDARY_FILTRATION_SCHEMA: VolDictType = {
36  vol.Required(ATTR_MODE): vol.In(
37  {
38  mode.name.lower()
39  for mode in smarttub.SpaSecondaryFiltrationCycle.SecondaryFiltrationMode
40  }
41  ),
42 }
43 
44 
46  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
47 ) -> None:
48  """Set up sensor entities for the sensors in the tub."""
49 
50  controller = hass.data[DOMAIN][entry.entry_id][SMARTTUB_CONTROLLER]
51 
52  entities = []
53  for spa in controller.spas:
54  entities.extend(
55  [
56  SmartTubSensor(controller.coordinator, spa, "State", "state"),
58  controller.coordinator, spa, "Flow Switch", "flow_switch"
59  ),
60  SmartTubSensor(controller.coordinator, spa, "Ozone", "ozone"),
61  SmartTubSensor(controller.coordinator, spa, "UV", "uv"),
63  controller.coordinator, spa, "Blowout Cycle", "blowout_cycle"
64  ),
66  controller.coordinator, spa, "Cleanup Cycle", "cleanup_cycle"
67  ),
68  SmartTubPrimaryFiltrationCycle(controller.coordinator, spa),
69  SmartTubSecondaryFiltrationCycle(controller.coordinator, spa),
70  ]
71  )
72 
73  async_add_entities(entities)
74 
75  platform = entity_platform.async_get_current_platform()
76 
77  platform.async_register_entity_service(
78  "set_primary_filtration",
79  SET_PRIMARY_FILTRATION_SCHEMA,
80  "async_set_primary_filtration",
81  )
82 
83  platform.async_register_entity_service(
84  "set_secondary_filtration",
85  SET_SECONDARY_FILTRATION_SCHEMA,
86  "async_set_secondary_filtration",
87  )
88 
89 
91  """Generic class for SmartTub status sensors."""
92 
93  @property
94  def native_value(self) -> str | None:
95  """Return the current state of the sensor."""
96  if self._state_state is None:
97  return None
98 
99  if isinstance(self._state_state, Enum):
100  return self._state_state.name.lower()
101 
102  return self._state_state.lower()
103 
104 
106  """The primary filtration cycle."""
107 
108  def __init__(self, coordinator, spa):
109  """Initialize the entity."""
110  super().__init__(
111  coordinator, spa, "Primary Filtration Cycle", "primary_filtration"
112  )
113 
114  @property
115  def cycle(self) -> smarttub.SpaPrimaryFiltrationCycle:
116  """Return the underlying smarttub.SpaPrimaryFiltrationCycle object."""
117  return self._state_state
118 
119  @property
120  def native_value(self) -> str:
121  """Return the current state of the sensor."""
122  return self.cyclecycle.status.name.lower()
123 
124  @property
126  """Return the state attributes."""
127  return {
128  ATTR_DURATION: self.cyclecycle.duration,
129  ATTR_CYCLE_LAST_UPDATED: self.cyclecycle.last_updated.isoformat(),
130  ATTR_MODE: self.cyclecycle.mode.name.lower(),
131  ATTR_START_HOUR: self.cyclecycle.start_hour,
132  }
133 
134  async def async_set_primary_filtration(self, **kwargs):
135  """Update primary filtration settings."""
136  await self.cyclecycle.set(
137  duration=kwargs.get(ATTR_DURATION),
138  start_hour=kwargs.get(ATTR_START_HOUR),
139  )
140  await self.coordinator.async_request_refresh()
141 
142 
144  """The secondary filtration cycle."""
145 
146  def __init__(self, coordinator, spa):
147  """Initialize the entity."""
148  super().__init__(
149  coordinator, spa, "Secondary Filtration Cycle", "secondary_filtration"
150  )
151 
152  @property
153  def cycle(self) -> smarttub.SpaSecondaryFiltrationCycle:
154  """Return the underlying smarttub.SpaSecondaryFiltrationCycle object."""
155  return self._state_state
156 
157  @property
158  def native_value(self) -> str:
159  """Return the current state of the sensor."""
160  return self.cyclecycle.status.name.lower()
161 
162  @property
164  """Return the state attributes."""
165  return {
166  ATTR_CYCLE_LAST_UPDATED: self.cyclecycle.last_updated.isoformat(),
167  ATTR_MODE: self.cyclecycle.mode.name.lower(),
168  }
169 
170  async def async_set_secondary_filtration(self, **kwargs):
171  """Update primary filtration settings."""
172  mode = smarttub.SpaSecondaryFiltrationCycle.SecondaryFiltrationMode[
173  kwargs[ATTR_MODE].upper()
174  ]
175  await self.cyclecycle.set_mode(mode)
176  await self.coordinator.async_request_refresh()
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: sensor.py:47