File: climate.py

package info (click to toggle)
python-xknx 3.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: python: 39,710; javascript: 8,556; makefile: 27; sh: 12
file content (390 lines) | stat: -rw-r--r-- 14,576 bytes parent folder | download
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""
Module for managing the climate within a room.

* It reads/listens to a temperature address from KNX bus.
* Manages and sends the desired setpoint to KNX bus.
"""

from __future__ import annotations

from collections.abc import Iterator
import logging
from typing import TYPE_CHECKING, Any

from xknx.devices.fan import FanSpeedMode
from xknx.remote_value import (
    GroupAddressesType,
    RemoteValue,
    RemoteValueDptValue1Ucount,
    RemoteValueNumeric,
    RemoteValueScaling,
    RemoteValueSetpointShift,
    RemoteValueSwitch,
    RemoteValueTemp,
)
from xknx.remote_value.remote_value_setpoint_shift import SetpointShiftMode

from .climate_mode import ClimateMode
from .device import Device, DeviceCallbackType

if TYPE_CHECKING:
    from xknx.telegram import Telegram
    from xknx.telegram.address import DeviceGroupAddress
    from xknx.xknx import XKNX

logger = logging.getLogger("xknx.log")


DEFAULT_SETPOINT_SHIFT_MAX = 6
DEFAULT_SETPOINT_SHIFT_MIN = -6
DEFAULT_TEMPERATURE_STEP = 0.1


class Climate(Device):
    """Class for managing the climate."""

    def __init__(
        self,
        xknx: XKNX,
        name: str,
        group_address_temperature: GroupAddressesType = None,
        group_address_target_temperature: GroupAddressesType = None,
        group_address_target_temperature_state: GroupAddressesType = None,
        group_address_setpoint_shift: GroupAddressesType = None,
        group_address_setpoint_shift_state: GroupAddressesType = None,
        setpoint_shift_mode: SetpointShiftMode | None = None,
        setpoint_shift_max: float = DEFAULT_SETPOINT_SHIFT_MAX,
        setpoint_shift_min: float = DEFAULT_SETPOINT_SHIFT_MIN,
        temperature_step: float = DEFAULT_TEMPERATURE_STEP,
        group_address_on_off: GroupAddressesType = None,
        group_address_on_off_state: GroupAddressesType = None,
        on_off_invert: bool = False,
        group_address_active_state: GroupAddressesType = None,
        group_address_command_value_state: GroupAddressesType = None,
        sync_state: bool | int | float | str = True,
        min_temp: float | None = None,
        max_temp: float | None = None,
        mode: ClimateMode | None = None,
        device_updated_cb: DeviceCallbackType[Climate] | None = None,
        group_address_fan_speed: GroupAddressesType = None,
        group_address_fan_speed_state: GroupAddressesType = None,
        fan_speed_mode: FanSpeedMode = FanSpeedMode.PERCENT,
        group_address_humidity_state: GroupAddressesType = None,
        group_address_swing: GroupAddressesType = None,
        group_address_swing_state: GroupAddressesType = None,
        group_address_horizontal_swing: GroupAddressesType = None,
        group_address_horizontal_swing_state: GroupAddressesType = None,
    ) -> None:
        """Initialize Climate class."""
        super().__init__(xknx, name, device_updated_cb)

        self.min_temp = min_temp
        self.max_temp = max_temp
        self.setpoint_shift_min = setpoint_shift_min
        self.setpoint_shift_max = setpoint_shift_max
        self.temperature_step = temperature_step

        self.temperature = RemoteValueTemp(
            xknx,
            group_address_state=group_address_temperature,
            sync_state=sync_state,
            device_name=self.name,
            feature_name="Current temperature",
            after_update_cb=self.after_update,
        )

        self.target_temperature = RemoteValueTemp(
            xknx,
            group_address_target_temperature,
            group_address_target_temperature_state,
            sync_state=sync_state,
            device_name=self.name,
            feature_name="Target temperature",
            after_update_cb=self.after_update,
        )

        self._setpoint_shift = RemoteValueSetpointShift(
            xknx,
            group_address_setpoint_shift,
            group_address_setpoint_shift_state,
            sync_state=sync_state,
            device_name=self.name,
            after_update_cb=self.after_update,
            setpoint_shift_mode=setpoint_shift_mode,
            setpoint_shift_step=self.temperature_step,
        )

        self.supports_on_off = (
            group_address_on_off is not None or group_address_on_off_state is not None
        )

        self.on = RemoteValueSwitch(  # pylint: disable=invalid-name
            xknx,
            group_address_on_off,
            group_address_on_off_state,
            sync_state=sync_state,
            device_name=self.name,
            after_update_cb=self.after_update,
            invert=on_off_invert,
        )

        self.active = RemoteValueSwitch(
            xknx,
            group_address_state=group_address_active_state,
            sync_state=sync_state,
            device_name=self.name,
            feature_name="Active",
            after_update_cb=self.after_update,
        )

        self.command_value = RemoteValueScaling(
            xknx,
            group_address_state=group_address_command_value_state,
            sync_state=sync_state,
            device_name=self.name,
            feature_name="Command value",
            after_update_cb=self.after_update,
        )

        self.fan_speed: RemoteValueDptValue1Ucount | RemoteValueScaling
        self.fan_speed_mode = fan_speed_mode

        if self.fan_speed_mode == FanSpeedMode.STEP:
            self.fan_speed = RemoteValueDptValue1Ucount(
                xknx,
                group_address_fan_speed,
                group_address_fan_speed_state,
                sync_state=sync_state,
                device_name=self.name,
                feature_name="Fan Speed",
                after_update_cb=self.after_update,
            )
        else:
            self.fan_speed = RemoteValueScaling(
                xknx,
                group_address_fan_speed,
                group_address_fan_speed_state,
                sync_state=sync_state,
                device_name=self.name,
                feature_name="Fan Speed",
                after_update_cb=self.after_update,
                range_from=0,
                range_to=100,
            )

        self.swing = RemoteValueSwitch(
            xknx,
            group_address_swing,
            group_address_swing_state,
            sync_state=sync_state,
            device_name=self.name,
            after_update_cb=self.after_update,
        )

        self.horizontal_swing = RemoteValueSwitch(
            xknx,
            group_address_horizontal_swing,
            group_address_horizontal_swing_state,
            sync_state=sync_state,
            device_name=self.name,
            after_update_cb=self.after_update,
        )

        self.mode = mode

        self.humidity = RemoteValueNumeric(
            xknx,
            group_address_state=group_address_humidity_state,
            sync_state=sync_state,
            value_type="humidity",
            device_name=self.name,
            feature_name="Current humidity",
            after_update_cb=self.after_update,
        )

    def _iter_remote_values(self) -> Iterator[RemoteValue[Any]]:
        """Iterate the devices RemoteValue classes."""
        yield self.temperature
        yield self.target_temperature
        yield self._setpoint_shift
        yield self.on
        yield self.active
        yield self.command_value
        yield self.fan_speed
        yield self.swing
        yield self.horizontal_swing
        yield self.humidity

    def has_group_address(self, group_address: DeviceGroupAddress) -> bool:
        """Test if device has given group address."""
        if self.mode is not None and self.mode.has_group_address(group_address):
            return True
        return super().has_group_address(group_address)

    @property
    def is_on(self) -> bool:
        """Return power status."""
        # None will return False
        return bool(self.on.value)

    @property
    def is_active(self) -> bool | None:
        """Return if currently active. None if unknown."""
        if self.active.value is not None:
            return self.active.value
        if self.command_value.value is not None:
            return bool(self.command_value.value)
        return None

    async def turn_on(self) -> None:
        """Set power status to on."""
        self.on.on()

    async def turn_off(self) -> None:
        """Set power status to off."""
        self.on.off()

    @property
    def initialized_for_setpoint_shift_calculations(self) -> bool:
        """Test if object is initialized for setpoint shift calculations."""
        return (
            self._setpoint_shift.initialized
            and self._setpoint_shift.value is not None
            and self.target_temperature.initialized
            and self.target_temperature.value is not None
        )

    async def set_target_temperature(self, target_temperature: float) -> None:
        """Send new target temperature or setpoint_shift to KNX bus."""
        if self.base_temperature is not None:
            # implies initialized_for_setpoint_shift_calculations
            temperature_delta = target_temperature - self.base_temperature
            await self.set_setpoint_shift(temperature_delta)
        else:
            validated_temp = self.validate_value(
                target_temperature, self.min_temp, self.max_temp
            )
            self.target_temperature.set(validated_temp)

    async def set_fan_speed(self, speed: int) -> None:
        """Set the fan to a designated speed."""
        self.fan_speed.set(speed)

    async def set_swing(self, swing: bool) -> None:
        """Set swing to the designated state."""
        self.swing.set(swing)

    async def set_horizontal_swing(self, horizontal_swing: bool) -> None:
        """Set swing to the designated state."""
        self.horizontal_swing.set(horizontal_swing)

    @property
    def base_temperature(self) -> float | None:
        """
        Return the base temperature when setpoint_shift is initialized.

        Base temperature is the default temperature (setpoint-shift=0) for the active climate mode.
        As this value is usually not available via KNX, we have to derive this from the current
        target temperature and the current set point shift.
        """
        # implies self.initialized_for_setpoint_shift_calculations in a mypy compatible way:
        if (
            self.target_temperature.value is not None
            and self._setpoint_shift.value is not None
        ):
            return self.target_temperature.value - self._setpoint_shift.value
        return None

    @property
    def setpoint_shift(self) -> float | None:
        """Return current offset from base temperature in Kelvin."""
        return self._setpoint_shift.value

    def validate_value(
        self, value: float, min_value: float | None, max_value: float | None
    ) -> float:
        """Check boundaries of temperature and return valid temperature value."""
        if (min_value is not None) and (value < min_value):
            logger.warning("Min value exceeded at %s: %s", self.name, value)
            return min_value
        if (max_value is not None) and (value > max_value):
            logger.warning("Max value exceeded at %s: %s", self.name, value)
            return max_value
        return value

    async def set_setpoint_shift(self, offset: float) -> None:
        """Send new temperature offset to KNX bus."""
        validated_offset = self.validate_value(
            offset, self.setpoint_shift_min, self.setpoint_shift_max
        )
        base_temperature = self.base_temperature
        self._setpoint_shift.set(validated_offset)
        # broadcast new target temperature and set internally
        if self.target_temperature.writable and base_temperature is not None:
            self.target_temperature.set(base_temperature + validated_offset)

    @property
    def target_temperature_max(self) -> float | None:
        """Return the highest possible target temperature."""
        if self.max_temp is not None:
            return self.max_temp
        if self.base_temperature is not None:
            # implies initialized_for_setpoint_shift_calculations
            return self.base_temperature + self.setpoint_shift_max
        return None

    @property
    def target_temperature_min(self) -> float | None:
        """Return the lowest possible target temperature."""
        if self.min_temp is not None:
            return self.min_temp
        if self.base_temperature is not None:
            # implies initialized_for_setpoint_shift_calculations
            return self.base_temperature + self.setpoint_shift_min
        return None

    @property
    def current_fan_speed(self) -> int | None:
        """Return current speed of fan."""
        return self.fan_speed.value

    @property
    def current_swing(self) -> bool | None:
        """Return current swing state."""
        return self.swing.value

    @property
    def current_horizontal_swing(self) -> bool | None:
        """Return current horizontal swing state."""
        return self.horizontal_swing.value

    def process_group_write(self, telegram: Telegram) -> None:
        """Process incoming and outgoing GROUP WRITE telegram."""
        for remote_value in self._iter_remote_values():
            remote_value.process(telegram)

        if self.mode is not None:
            self.mode.process_group_write(telegram)

    async def sync(self, wait_for_result: bool = False) -> None:
        """Read states of device from KNX bus."""
        await super().sync(wait_for_result=wait_for_result)
        if self.mode is not None:
            await self.mode.sync(wait_for_result=wait_for_result)

    def __str__(self) -> str:
        """Return object as readable string."""
        return (
            f'<Climate name="{self.name}" '
            f"temperature={self.temperature.group_addr_str()} "
            f"target_temperature={self.target_temperature.group_addr_str()} "
            f'temperature_step="{self.temperature_step}" '
            f"setpoint_shift={self._setpoint_shift.group_addr_str()} "
            f'setpoint_shift_max="{self.setpoint_shift_max}" '
            f'setpoint_shift_min="{self.setpoint_shift_min}" '
            f"group_address_on_off={self.on.group_addr_str()} "
            f"group_address_fan_speed={self.fan_speed.group_addr_str()} "
            f"group_address_swing={self.swing.group_addr_str()} "
            f"group_address_horizontal_swing={self.horizontal_swing.group_addr_str()} "
            "/>"
        )