|
| 1 | +"""``humidifier`` domain implementation.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from haclient.core.plugins import DomainSpec, register_domain |
| 8 | +from haclient.entity.base import Entity |
| 9 | + |
| 10 | + |
| 11 | +class Humidifier(Entity): |
| 12 | + """A Home Assistant humidifier (or dehumidifier) entity. |
| 13 | +
|
| 14 | + The public API uses ``on()`` / ``off()`` / ``toggle()`` as |
| 15 | + intent-specific names rather than the raw HA ``turn_on`` / |
| 16 | + ``turn_off`` services, and exposes humidity-specific state and |
| 17 | + actions. |
| 18 | +
|
| 19 | + Mode operations degrade safely: `set_mode` checks the entity's |
| 20 | + reported `available_modes` and raises ``ValueError`` for unsupported |
| 21 | + modes, while devices that do not report modes at all expose an |
| 22 | + empty `available_modes` list and a `mode` of ``None``. |
| 23 | + """ |
| 24 | + |
| 25 | + domain = "humidifier" |
| 26 | + |
| 27 | + # -- Listener decorators ------------------------------------------ |
| 28 | + |
| 29 | + def on_turn_on(self, func: Any) -> Any: |
| 30 | + """Register a listener for when the humidifier turns on. |
| 31 | +
|
| 32 | + Parameters |
| 33 | + ---------- |
| 34 | + func : callable |
| 35 | + Sync or async zero-argument callable invoked on every |
| 36 | + transition into the ``on`` state. |
| 37 | +
|
| 38 | + Returns |
| 39 | + ------- |
| 40 | + callable |
| 41 | + The same *func*, returned for decorator use. |
| 42 | + """ |
| 43 | + return self._register_state_transition_listener("on", func) |
| 44 | + |
| 45 | + def on_turn_off(self, func: Any) -> Any: |
| 46 | + """Register a listener for when the humidifier turns off. |
| 47 | +
|
| 48 | + Parameters |
| 49 | + ---------- |
| 50 | + func : callable |
| 51 | + Sync or async zero-argument callable invoked on every |
| 52 | + transition into the ``off`` state. |
| 53 | +
|
| 54 | + Returns |
| 55 | + ------- |
| 56 | + callable |
| 57 | + The same *func*, returned for decorator use. |
| 58 | + """ |
| 59 | + return self._register_state_transition_listener("off", func) |
| 60 | + |
| 61 | + def on_humidity_change(self, func: Any) -> Any: |
| 62 | + """Register a listener for target humidity changes. |
| 63 | +
|
| 64 | + Parameters |
| 65 | + ---------- |
| 66 | + func : callable |
| 67 | + Callable receiving the new target ``humidity`` value. |
| 68 | +
|
| 69 | + Returns |
| 70 | + ------- |
| 71 | + callable |
| 72 | + The same *func*, returned for decorator use. |
| 73 | + """ |
| 74 | + return self._register_attr_listener("humidity", func) |
| 75 | + |
| 76 | + def on_mode_change(self, func: Any) -> Any: |
| 77 | + """Register a listener for operating mode changes. |
| 78 | +
|
| 79 | + Parameters |
| 80 | + ---------- |
| 81 | + func : callable |
| 82 | + Callable receiving the new mode string. |
| 83 | +
|
| 84 | + Returns |
| 85 | + ------- |
| 86 | + callable |
| 87 | + The same *func*, returned for decorator use. |
| 88 | + """ |
| 89 | + return self._register_attr_listener("mode", func) |
| 90 | + |
| 91 | + # -- State properties --------------------------------------------- |
| 92 | + |
| 93 | + @property |
| 94 | + def is_on(self) -> bool: |
| 95 | + """Whether the humidifier is currently on.""" |
| 96 | + return self.state == "on" |
| 97 | + |
| 98 | + @property |
| 99 | + def target_humidity(self) -> int | None: |
| 100 | + """Configured target humidity, in percent, if reported.""" |
| 101 | + value = self.attributes.get("humidity") |
| 102 | + return int(value) if isinstance(value, (int, float)) else None |
| 103 | + |
| 104 | + @property |
| 105 | + def current_humidity(self) -> int | None: |
| 106 | + """Currently measured humidity, in percent. |
| 107 | +
|
| 108 | + Returns ``None`` when the device does not report a reading. |
| 109 | + """ |
| 110 | + value = self.attributes.get("current_humidity") |
| 111 | + return int(value) if isinstance(value, (int, float)) else None |
| 112 | + |
| 113 | + @property |
| 114 | + def mode(self) -> str | None: |
| 115 | + """Active operating mode, or ``None`` when the device has none.""" |
| 116 | + value = self.attributes.get("mode") |
| 117 | + return str(value) if isinstance(value, str) else None |
| 118 | + |
| 119 | + @property |
| 120 | + def available_modes(self) -> list[str]: |
| 121 | + """Operating modes supported by the device. |
| 122 | +
|
| 123 | + Returns an empty list when the device does not advertise modes. |
| 124 | + """ |
| 125 | + modes = self.attributes.get("available_modes") |
| 126 | + return [str(m) for m in modes] if isinstance(modes, list) else [] |
| 127 | + |
| 128 | + @property |
| 129 | + def device_class(self) -> str | None: |
| 130 | + """Device class (``"humidifier"`` or ``"dehumidifier"``).""" |
| 131 | + value = self.attributes.get("device_class") |
| 132 | + return str(value) if isinstance(value, str) else None |
| 133 | + |
| 134 | + # -- Actions ------------------------------------------------------ |
| 135 | + |
| 136 | + async def on(self) -> None: |
| 137 | + """Activate the humidifier.""" |
| 138 | + await self._call_service("turn_on") |
| 139 | + |
| 140 | + async def off(self) -> None: |
| 141 | + """Deactivate the humidifier.""" |
| 142 | + await self._call_service("turn_off") |
| 143 | + |
| 144 | + async def toggle(self) -> None: |
| 145 | + """Toggle the humidifier state.""" |
| 146 | + await self._call_service("toggle") |
| 147 | + |
| 148 | + async def set_humidity(self, humidity: int) -> None: |
| 149 | + """Set the target humidity, in percent. |
| 150 | +
|
| 151 | + Parameters |
| 152 | + ---------- |
| 153 | + humidity : int |
| 154 | + Target humidity between 0 and 100 (inclusive). |
| 155 | +
|
| 156 | + Raises |
| 157 | + ------ |
| 158 | + ValueError |
| 159 | + If *humidity* is outside the 0-100 range. |
| 160 | + """ |
| 161 | + value = int(humidity) |
| 162 | + if not 0 <= value <= 100: |
| 163 | + raise ValueError("humidity must be between 0 and 100") |
| 164 | + await self._call_service("set_humidity", {"humidity": value}) |
| 165 | + |
| 166 | + async def set_mode(self, mode: str) -> None: |
| 167 | + """Set the operating mode, when supported. |
| 168 | +
|
| 169 | + Parameters |
| 170 | + ---------- |
| 171 | + mode : str |
| 172 | + Mode to activate. Must be one of `available_modes` when the |
| 173 | + device reports any; the call is silently skipped for devices |
| 174 | + that do not support modes at all (empty `available_modes`). |
| 175 | +
|
| 176 | + Raises |
| 177 | + ------ |
| 178 | + ValueError |
| 179 | + If the device reports `available_modes` and *mode* is not |
| 180 | + in that list. |
| 181 | + """ |
| 182 | + modes = self.available_modes |
| 183 | + if not modes: |
| 184 | + # Graceful degradation: device exposes no modes. |
| 185 | + return |
| 186 | + if mode not in modes: |
| 187 | + raise ValueError( |
| 188 | + f"mode {mode!r} not in available_modes {modes!r}", |
| 189 | + ) |
| 190 | + await self._call_service("set_mode", {"mode": mode}) |
| 191 | + |
| 192 | + |
| 193 | +SPEC: DomainSpec[Humidifier] = register_domain(DomainSpec(name="humidifier", entity_cls=Humidifier)) |
| 194 | +"""The `DomainSpec` registered with the shared `DomainRegistry`.""" |
0 commit comments