1 """Support for Valve devices."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
6 from datetime
import timedelta
7 from enum
import IntFlag, StrEnum
9 from typing
import Any, final
11 import voluptuous
as vol
17 SERVICE_SET_VALVE_POSITION,
32 from .const
import DOMAIN, ValveState
34 _LOGGER = logging.getLogger(__name__)
36 DATA_COMPONENT: HassKey[EntityComponent[ValveEntity]] = HassKey(DOMAIN)
37 ENTITY_ID_FORMAT = DOMAIN +
".{}"
38 PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA
39 PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE
44 """Device class for valve."""
51 DEVICE_CLASSES_SCHEMA = vol.All(vol.Lower, vol.Coerce(ValveDeviceClass))
56 """Supported features of the valve entity."""
64 ATTR_CURRENT_POSITION =
"current_position"
65 ATTR_POSITION =
"position"
68 async
def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
69 """Track states and offer events for valves."""
70 component = hass.data[DATA_COMPONENT] = EntityComponent[ValveEntity](
71 _LOGGER, DOMAIN, hass, SCAN_INTERVAL
74 await component.async_setup(config)
76 component.async_register_entity_service(
77 SERVICE_OPEN_VALVE,
None,
"async_handle_open_valve", [ValveEntityFeature.OPEN]
80 component.async_register_entity_service(
83 "async_handle_close_valve",
84 [ValveEntityFeature.CLOSE],
87 component.async_register_entity_service(
88 SERVICE_SET_VALVE_POSITION,
90 vol.Required(ATTR_POSITION): vol.All(
91 vol.Coerce(int), vol.Range(min=0, max=100)
94 "async_set_valve_position",
95 [ValveEntityFeature.SET_POSITION],
98 component.async_register_entity_service(
99 SERVICE_STOP_VALVE,
None,
"async_stop_valve", [ValveEntityFeature.STOP]
102 component.async_register_entity_service(
106 [ValveEntityFeature.OPEN | ValveEntityFeature.CLOSE],
113 """Set up a config entry."""
118 """Unload a config entry."""
122 @dataclass(frozen=True, kw_only=True)
124 """A class that describes valve entities."""
126 device_class: ValveDeviceClass |
None =
None
127 reports_position: bool =
False
131 """Base class for valve entities."""
133 entity_description: ValveEntityDescription
134 _attr_current_valve_position: int |
None =
None
135 _attr_device_class: ValveDeviceClass |
None
136 _attr_is_closed: bool |
None =
None
137 _attr_is_closing: bool |
None =
None
138 _attr_is_opening: bool |
None =
None
139 _attr_reports_position: bool
142 __is_last_toggle_direction_open =
True
146 """Return True if entity reports position, False otherwise."""
147 if hasattr(self,
"_attr_reports_position"):
148 return self._attr_reports_position
149 if hasattr(self,
"entity_description"):
150 return self.entity_description.reports_position
151 raise ValueError(f
"'reports_position' not set for {self.entity_id}.")
155 """Return current position of valve.
157 None is unknown, 0 is closed, 100 is fully open.
159 return self._attr_current_valve_position
163 """Return the class of this entity."""
164 if hasattr(self,
"_attr_device_class"):
165 return self._attr_device_class
166 if hasattr(self,
"entity_description"):
167 return self.entity_description.device_class
173 """Return the state of the valve."""
177 return ValveState.OPENING
180 return ValveState.CLOSING
181 if reports_position
is True:
184 position_zero = current_valve_position == 0
185 return ValveState.CLOSED
if position_zero
else ValveState.OPEN
186 if (closed := self.
is_closedis_closed)
is None:
188 return ValveState.CLOSED
if closed
else ValveState.OPEN
193 """Return the state attributes."""
200 """Flag supported features."""
201 return self._attr_supported_features
205 """Return if the valve is opening or not."""
206 return self._attr_is_opening
210 """Return if the valve is closing or not."""
211 return self._attr_is_closing
215 """Return if the valve is closed or not."""
216 return self._attr_is_closed
219 """Open the valve."""
220 raise NotImplementedError
223 """Open the valve."""
224 await self.
hasshass.async_add_executor_job(self.
open_valveopen_valve)
228 """Open the valve."""
236 raise NotImplementedError
240 await self.
hasshass.async_add_executor_job(self.
close_valveclose_valve)
244 """Close the valve."""
251 """Toggle the entity."""
263 """Move the valve to a specific position."""
264 raise NotImplementedError
267 """Move the valve to a specific position."""
271 """Stop the valve."""
272 raise NotImplementedError
275 """Stop the valve."""
276 await self.
hasshass.async_add_executor_job(self.
stop_valvestop_valve)
ValveEntityFeature supported_features(self)
None async_stop_valve(self)
dict[str, Any]|None state_attributes(self)
bool|None is_closed(self)
bool reports_position(self)
ValveDeviceClass|None device_class(self)
__is_last_toggle_direction_open
bool|None is_closing(self)
int|None current_valve_position(self)
None async_open_valve(self)
None async_handle_close_valve(self)
None async_close_valve(self)
None set_valve_position(self, int position)
None async_set_valve_position(self, int position)
None async_handle_open_valve(self)
bool|None is_opening(self)
int|None supported_features(self)
bool async_setup(HomeAssistant hass, ConfigType config)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)