File size: 11,012 Bytes
cffba29 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | # v20260913
from __future__ import annotations
from src.object_model import BoolProperty, DictProperty, FloatProperty, IntProperty, StringProperty
class Humidifier:
"""
Humidifier
Description: Humidifier. Users can query and set power, sleep mode, target humidity, and humidification intensity. When the device is on and the humidity of its space is below the target, it gradually increases humidity according to the humidification intensity; it produces no further humidification effect when turned off or when the space has reached the target humidity. Current space humidity is provided by the containing space.
Version: 1.0.0
Base: -
"""
def __init__(self, power: bool = None, sleep_mode: bool = None, target_humidity: int = None, humidification_intensity: str = None, *, _defer_power_init: bool = False) -> None:
_power_init_value = power if power is not None else False
# define properties
self.power = BoolProperty(
name='power',
description='Whether the humidifier is powered.',
default=False
)
self.sleep_mode = BoolProperty(
name='sleep_mode',
description='Whether sleep mode is enabled.',
default=False
)
self.target_humidity = IntProperty(
name='target_humidity',
description='Desired humidity for the containing space.',
default=50,
unit='%RH',
minimum=0,
maximum=100,
step=1,
step_origin=0
)
self.humidification_intensity = StringProperty(
name='humidification_intensity',
description='Humidification output intensity.',
default='medium',
choices=['low', 'medium', 'high']
)
code = self.power.internal_set(_power_init_value)
# initialize properties
if sleep_mode is not None: code = self.sleep_mode.internal_set(sleep_mode)
if target_humidity is not None: code = self.target_humidity.internal_set(target_humidity)
if humidification_intensity is not None: code = self.humidification_intensity.internal_set(humidification_intensity)
self._power_managed_properties = (self.sleep_mode, self.target_humidity, self.humidification_intensity,)
self._power_initialized = False
self._disabled = {
'properties': [],
'functions': [],
'events': []
}
if not _defer_power_init:
self.set_power(_power_init_value)
def get_power(self) -> tuple[str, dict]:
"""
Get the current power state.
"""
code, value = self.power.get()
if code != "[PASS]":
return code, {}
return "[PASS]", {"power": value}
def set_power(self, value: bool) -> tuple[str, dict]:
"""
Set power and update access for active dependent properties.
"""
if self._power_initialized:
current_code, current_value = self.power.get()
if current_code != "[PASS]":
return current_code, {}
if type(value) is bool and current_value == value:
return "[PASS]", {}
code = self.power.set(value)
if code != "[PASS]":
return code, {}
for prop in self._power_managed_properties:
switch_code = prop.handle_power_switch(value)
if switch_code != "[PASS]" and not switch_code.startswith("[NO_ACTION]"):
return switch_code, {}
self._power_initialized = True
return "[PASS]", {}
def get_sleep_mode(self) -> tuple[str, dict]:
"""
Return the sleep mode setting while the humidifier is powered.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
- sleep_mode: Whether sleep mode is enabled. (type=boolean)
Error Codes:
(no specified error codes)
"""
code, value = self.sleep_mode.get()
if code != "[PASS]":
return code, {}
return "[PASS]", {"sleep_mode": value}
def set_sleep_mode(self, value: bool) -> tuple[str, dict]:
"""
Enable or disable sleep mode while the humidifier is powered.
Args:
- value: Requested sleep mode setting.
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
(no explicit response)
Error Codes:
(no specified error codes)
"""
code, power = self.power.get()
if code != "[PASS]":
return code, {}
if power is not True:
return "[ERROR] POWER_OFF", {}
if type(value) is not bool:
return "[ERROR] INVALID_TYPE", {}
code = self.sleep_mode.set(value)
if code != "[PASS]":
return code, {}
return "[PASS]", {}
def get_target_humidity(self) -> tuple[str, dict]:
"""
Return the target humidity while the humidifier is powered.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
- target_humidity: Configured target humidity in percent relative humidity. (type=integer)
Error Codes:
(no specified error codes)
"""
code, value = self.target_humidity.get()
if code != "[PASS]":
return code, {}
return "[PASS]", {"target_humidity": value}
def set_target_humidity(self, value: int) -> tuple[str, dict]:
"""
Set the target humidity to an integer from 0 through 100 while the humidifier is powered.
Args:
- value: Target humidity in percent relative humidity. (min=0, max=100)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
(no explicit response)
Error Codes:
(no specified error codes)
"""
code, power = self.power.get()
if code != "[PASS]":
return code, {}
if power is not True:
return "[ERROR] POWER_OFF", {}
if type(value) is not int:
return "[ERROR] INVALID_TYPE", {}
if value < 0 or value > 100:
return "[ERROR] OUT_OF_RANGE", {}
code = self.target_humidity.set(value)
if code != "[PASS]":
return code, {}
return "[PASS]", {}
def get_humidification_intensity(self) -> tuple[str, dict]:
"""
Return the humidification intensity while the humidifier is powered.
Args:
(no arguments required)
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
- humidification_intensity: Current humidification intensity. (type=string)
Error Codes:
(no specified error codes)
"""
code, value = self.humidification_intensity.get()
if code != "[PASS]":
return code, {}
return "[PASS]", {"humidification_intensity": value}
def set_humidification_intensity(self, value: str) -> tuple[str, dict]:
"""
Set humidification intensity to low, medium, or high while the humidifier is powered.
Args:
- value: Requested humidification intensity. (choices=['low', 'medium', 'high'])
Returns:
- Return Code: The return code of this function, either "[PASS]" or one of Error Codes built-in or listed below (type=str)
- Response Data: A dictionary containing response data (type=dict)
(no explicit response)
Error Codes:
(no specified error codes)
"""
code, power = self.power.get()
if code != "[PASS]":
return code, {}
if power is not True:
return "[ERROR] POWER_OFF", {}
if type(value) is not str:
return "[ERROR] INVALID_TYPE", {}
if value not in ("low", "medium", "high"):
return "[ERROR] INVALID_VALUE", {}
code = self.humidification_intensity.set(value)
if code != "[PASS]":
return code, {}
return "[PASS]", {}
def _on_tick(self, context: dict) -> tuple[str, list[dict]]:
"""
On each tick, while powered, increase the containing space's humidity by a deterministic contribution when its current humidity is below target_humidity. The contribution is 1 percentage point at low intensity, 2 at medium intensity, and 3 at high intensity; sleep mode halves the contribution using integer reduction, with a minimum contribution of 1 while humidification is needed. Emit no humidity contribution when unpowered or when current humidity is at least target_humidity.
Effect properties:
- power: Whether the humidifier is powered.
- sleep_mode: Whether sleep mode is enabled.
- target_humidity: Desired humidity for the containing space.
- humidification_intensity: Configured humidification output intensity.
"""
code, powered = self.power.get()
if code != "[PASS]":
return code, []
if powered is not True:
return "[PASS]", []
space_state = context["space_state"]
humidity = space_state["humidity"]
code, target_humidity = self.target_humidity.get()
if code != "[PASS]":
return code, []
if humidity >= target_humidity:
return "[PASS]", []
code, intensity = self.humidification_intensity.get()
if code != "[PASS]":
return code, []
if intensity == "low":
contribution = 1
elif intensity == "medium":
contribution = 2
else:
contribution = 3
code, sleeping = self.sleep_mode.get()
if code != "[PASS]":
return code, []
if sleeping is True:
contribution = max(contribution // 2, 1)
return "[PASS]", [{
"trigger": "tick",
"source": context["object_id"],
"target": context["space_id"],
"function": "delta",
"parameters": {
"property": "humidity",
"value": contribution,
"unit": "%RH"
}
}] |