1 """Valves for the Elexa Guardian integration."""
3 from __future__
import annotations
5 from collections.abc
import Callable, Coroutine
6 from dataclasses
import dataclass
7 from enum
import StrEnum
10 from aioguardian
import Client
15 ValveEntityDescription,
22 from .
import GuardianData
23 from .const
import API_VALVE_STATUS, DOMAIN
24 from .entity
import ValveControllerEntity, ValveControllerEntityDescription
25 from .util
import convert_exceptions_to_homeassistant_error
27 VALVE_KIND_VALVE =
"valve"
31 """States of a valve."""
35 FINISH_CLOSING =
"finish_closing"
36 FINISH_OPENING =
"finish_opening"
39 START_CLOSING =
"start_closing"
40 START_OPENING =
"start_opening"
43 @dataclass(frozen=True, kw_only=True)
45 ValveEntityDescription, ValveControllerEntityDescription
47 """Describe a Guardian valve controller valve."""
49 is_closed_fn: Callable[[dict[str, Any]], bool]
50 is_closing_fn: Callable[[dict[str, Any]], bool]
51 is_opening_fn: Callable[[dict[str, Any]], bool]
52 close_coro_fn: Callable[[Client], Coroutine[Any, Any,
None]]
53 halt_coro_fn: Callable[[Client], Coroutine[Any, Any,
None]]
54 open_coro_fn: Callable[[Client], Coroutine[Any, Any,
None]]
58 """Close the valve."""
60 await client.valve.close()
66 await client.valve.halt()
72 await client.valve.open()
77 """Return if the valve is closing."""
78 return data[
"state"]
in (
79 GuardianValveState.CLOSING,
80 GuardianValveState.FINISH_CLOSING,
81 GuardianValveState.START_CLOSING,
87 """Return if the valve is opening."""
88 return data[
"state"]
in (
89 GuardianValveState.OPENING,
90 GuardianValveState.FINISH_OPENING,
91 GuardianValveState.START_OPENING,
95 VALVE_CONTROLLER_DESCRIPTIONS = (
98 translation_key=
"valve_controller",
99 device_class=ValveDeviceClass.WATER,
100 api_category=API_VALVE_STATUS,
101 is_closed_fn=
lambda data: data[
"state"] == GuardianValveState.CLOSED,
102 is_closing_fn=is_closing,
103 is_opening_fn=is_opening,
104 close_coro_fn=async_close_valve,
105 halt_coro_fn=async_halt_valve,
106 open_coro_fn=async_open_valve,
112 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
114 """Set up Guardian switches based on a config entry."""
115 data: GuardianData = hass.data[DOMAIN][entry.entry_id]
119 for description
in VALVE_CONTROLLER_DESCRIPTIONS
124 """Define a switch related to a Guardian valve controller."""
126 _attr_supported_features = (
127 ValveEntityFeature.OPEN | ValveEntityFeature.CLOSE | ValveEntityFeature.STOP
129 entity_description: ValveControllerValveDescription
135 description: ValveControllerValveDescription,
138 super().
__init__(entry, data.valve_controller_coordinators, description)
144 """Return if the valve is closing or not."""
149 """Return if the valve is closed or not."""
154 """Return if the valve is opening or not."""
157 @convert_exceptions_to_homeassistant_error
159 """Close the valve."""
163 @convert_exceptions_to_homeassistant_error
165 """Open the valve."""
169 @convert_exceptions_to_homeassistant_error
171 """Stop the valve."""
None async_close_valve(self)
None async_stop_valve(self)
None async_open_valve(self)
None __init__(self, ConfigEntry entry, GuardianData data, ValveControllerValveDescription description)
None async_request_refresh(self)
None async_open_valve(Client client)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
None async_close_valve(Client client)
bool is_opening(dict[str, Any] data)
None async_halt_valve(Client client)
bool is_closing(dict[str, Any] data)