File: util.py

package info (click to toggle)
python-plugwise 1.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,080 kB
  • sloc: xml: 90,006; python: 5,344; sh: 326; makefile: 2
file content (330 lines) | stat: -rw-r--r-- 10,689 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
"""Plugwise protocol helpers."""

from __future__ import annotations

import datetime as dt
import re
from typing import cast

from plugwise.constants import (
    ATTR_UNIT_OF_MEASUREMENT,
    BINARY_SENSORS,
    DATA,
    ELECTRIC_POTENTIAL_VOLT,
    ENERGY_KILO_WATT_HOUR,
    HW_MODELS,
    NONE,
    OBSOLETE_MEASUREMENTS,
    PERCENTAGE,
    POWER_WATT,
    SENSORS,
    SPECIAL_FORMAT,
    SPECIALS,
    SWITCHES,
    UOM,
    BinarySensorType,
    GwEntityData,
    ModuleData,
    SensorType,
    SpecialType,
    SwitchType,
)

from defusedxml import ElementTree as etree
from munch import Munch


def check_alternative_location(loc: Munch, legacy: bool) -> Munch:
    """Helper-function for _power_data_peak_value()."""
    if in_alternative_location(loc, legacy):
        # Avoid double processing by skipping one peak-list option
        if loc.peak_select == "nl_offpeak":
            loc.found = False
            return loc

        loc.locator = f'./{loc.log_type}[type="{loc.measurement}"]/period/measurement'
        if legacy:
            loc.locator = (
                f"./{loc.meas_list[0]}_{loc.log_type}/"
                f'measurement[@directionality="{loc.meas_list[1]}"]'
            )

        if loc.logs.find(loc.locator) is None:
            loc.found = False
            return loc

        return loc

    loc.found = False
    return loc


def in_alternative_location(loc: Munch, legacy: bool) -> bool:
    """Look for P1 gas_consumed or phase data (without tariff).

    For legacy look for P1 legacy electricity_point_meter or gas_*_meter data.
    """
    present = "log" in loc.log_type and (
        "gas" in loc.measurement or "phase" in loc.measurement
    )
    if legacy:
        present = "meter" in loc.log_type and (
            "point" in loc.log_type or "gas" in loc.measurement
        )

    return present


def check_heater_central(xml: etree.Element) -> str:
    """Find the valid heater_central, helper-function for _appliance_info_finder().

    Solution for Core Issue #104433,
    for a system that has two heater_central appliances.
    """
    locator = "./appliance[type='heater_central']"
    hc_count = 0
    hc_list: list[dict[str, bool]] = []
    for heater_central in xml.findall(locator):
        hc_count += 1
        hc_id: str = heater_central.attrib["id"]
        has_actuators: bool = (
            heater_central.find("actuator_functionalities/") is not None
        )
        # Filter for Plug/Circle/Stealth heater_central -- Pw-Beta Issue #739
        if heater_central.find("name").text == "Central heating boiler":
            hc_list.append({hc_id: has_actuators})

    if not hc_list:
        return NONE  # pragma: no cover

    heater_central_id = list(hc_list[0].keys())[0]
    if hc_count > 1:
        for item in hc_list:
            hc_id, has_actuators = next(iter(item.items()))
            if has_actuators:
                heater_central_id = hc_id
                break

    return heater_central_id


def check_model(name: str | None, vendor_name: str | None) -> str | None:
    """Model checking before using version_to_model."""
    if vendor_name == "Plugwise" and ((model := version_to_model(name)) != "Unknown"):
        return model

    if name is not None and "lumi.plug" in name:
        return "Aqara Smart Plug"

    return None


def collect_power_values(
    data: GwEntityData, loc: Munch, tariff: str, legacy: bool = False
) -> None:
    """Something."""
    for loc.peak_select in ("nl_peak", "nl_offpeak"):
        loc.locator = (
            f'./{loc.log_type}[type="{loc.measurement}"]/period/'
            f'measurement[@{tariff}="{loc.peak_select}"]'
        )
        if legacy:
            loc.locator = (
                f"./{loc.meas_list[0]}_{loc.log_type}/measurement"
                f'[@directionality="{loc.meas_list[1]}"][@{tariff}="{loc.peak_select}"]'
            )

        loc = power_data_peak_value(loc, legacy)
        if not loc.found:
            continue

        power_data_energy_diff(loc.measurement, loc.net_string, loc.f_val, data)
        key = cast(SensorType, loc.key_string)
        data["sensors"][key] = loc.f_val


def common_match_cases(
    measurement: str,
    attrs: DATA | UOM,
    location: etree.Element,
    data: GwEntityData,
) -> None:
    """Helper-function for common match-case execution."""
    value = location.text in ("on", "true")
    match measurement:
        case _ as measurement if measurement in BINARY_SENSORS:
            bs_key = cast(BinarySensorType, measurement)
            data["binary_sensors"][bs_key] = value
        case _ as measurement if measurement in SENSORS:
            s_key = cast(SensorType, measurement)
            s_value = format_measure(
                location.text, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
            )
            data["sensors"][s_key] = s_value
        case _ as measurement if measurement in SWITCHES:
            sw_key = cast(SwitchType, measurement)
            data["switches"][sw_key] = value
        case _ as measurement if measurement in SPECIALS:
            sp_key = cast(SpecialType, measurement)
            data[sp_key] = value

    if "battery" in data["sensors"]:
        data["binary_sensors"]["low_battery"] = False


def count_data_items(count: int, data: GwEntityData) -> int:
    """When present, count the binary_sensors, sensors and switches dict-items, don't count the dicts.

    Also, count the remaining single data items, the amount of dicts present have already been pre-subtracted in the previous step.
    """
    if "binary_sensors" in data:
        count += len(data["binary_sensors"]) - 1
    if "sensors" in data:
        count += len(data["sensors"]) - 1
    if "switches" in data:
        count += len(data["switches"]) - 1

    count += len(data)
    return count


def escape_illegal_xml_characters(xmldata: str) -> str:
    """Replace illegal &-characters."""
    return re.sub(r"&([^a-zA-Z#])", r"&\1", xmldata)


def format_measure(measure: str, unit: str) -> float | int:
    """Format measure to correct type."""
    float_measure = float(measure)
    if unit == PERCENTAGE and 0 < float_measure <= 1:
        return int(float_measure * 100)

    if unit == ENERGY_KILO_WATT_HOUR:
        float_measure = float_measure / 1000

    if unit in SPECIAL_FORMAT:
        result = round(float_measure, 3)
    elif unit == ELECTRIC_POTENTIAL_VOLT:
        result = round(float_measure, 1)
    elif abs(float_measure) < 10:
        result = round(float_measure, 2)
    else:  # abs(float_measure) >= 10
        result = round(float_measure, 1)

    return result


def get_vendor_name(module: etree.Element, model_data: ModuleData) -> ModuleData:
    """Helper-function for _get_model_data()."""
    if (vendor_name := module.find("vendor_name").text) is not None:
        model_data["vendor_name"] = vendor_name
        if "Plugwise" in vendor_name:
            model_data["vendor_name"] = vendor_name.partition(" ")[0]

    return model_data


def power_data_energy_diff(
    measurement: str,
    net_string: SensorType,
    f_val: float | int,
    data: GwEntityData,
) -> None:
    """Calculate differential energy."""
    if (
        "electricity" in measurement
        and "phase" not in measurement
        and "interval" not in net_string
    ):
        diff = 1 if "consumed" in measurement else -1
        tmp_val = data["sensors"].get(net_string, 0)
        tmp_val += f_val * diff
        if isinstance(f_val, float):
            tmp_val = round(tmp_val, 3)

        data["sensors"][net_string] = tmp_val


def power_data_local_format(
    attrs: dict[str, str], key_string: str, val: str
) -> float | int:
    """Format power data."""
    # Special formatting of P1_MEASUREMENT POWER_WATT values, do not move to util-format_measure() function!
    if all(item in key_string for item in ("electricity", "cumulative")):
        return format_measure(val, ENERGY_KILO_WATT_HOUR)
    if (attrs_uom := getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)) == POWER_WATT:
        return int(round(float(val)))

    return format_measure(val, attrs_uom)


def power_data_peak_value(loc: Munch, legacy: bool) -> Munch:
    """Helper-function for _power_data_from_location() and _power_data_from_modules()."""
    loc.found = True
    if loc.logs.find(loc.locator) is None:
        loc = check_alternative_location(loc, legacy)
        if not loc.found:
            return loc

    if (peak := loc.peak_select.partition("_")[2]) == "offpeak":
        peak = "off_peak"
    log_found = loc.log_type.partition("_")[0]
    loc.key_string = f"{loc.measurement}_{peak}_{log_found}"
    if "gas" in loc.measurement or loc.log_type == "point_meter":
        loc.key_string = f"{loc.measurement}_{log_found}"
    # Only for P1 Actual -------------------#
    if "phase" in loc.measurement:
        loc.key_string = f"{loc.measurement}"
    # --------------------------------------#
    loc.net_string = f"net_electricity_{log_found}"
    val = loc.logs.find(loc.locator).text
    loc.f_val = power_data_local_format(loc.attrs, loc.key_string, val)

    return loc


def remove_empty_platform_dicts(data: GwEntityData) -> None:
    """Helper-function for removing any empty platform dicts."""
    if not data["binary_sensors"]:
        data.pop("binary_sensors")
    if not data["sensors"]:
        data.pop("sensors")
    if not data["switches"]:
        data.pop("switches")


def return_valid(value: etree.Element | None, default: etree.Element) -> etree.Element:
    """Return default when value is None."""
    return value if value is not None else default


def skip_obsolete_measurements(xml: etree.Element, measurement: str) -> bool:
    """Skipping known obsolete measurements."""
    locator = f".//logs/point_log[type='{measurement}']/updated_date"
    if (
        measurement in OBSOLETE_MEASUREMENTS
        and (updated_date_key := xml.find(locator)) is not None
    ):
        updated_date = updated_date_key.text.partition("T")[0]
        date_1 = dt.datetime.strptime(updated_date, "%Y-%m-%d")
        date_2 = dt.datetime.now()
        return int((date_2 - date_1).days) > 7

    return False


# NOTE: this function version_to_model is shared between Smile and USB
def version_to_model(version: str | None) -> str | None:
    """Translate hardware_version to device type."""

    if version is None:
        return version

    model = HW_MODELS.get(version)
    if model is None:
        model = HW_MODELS.get(version[4:10])
    if model is None:
        # Try again with reversed order
        model = HW_MODELS.get(version[-2:] + version[-4:-2] + version[-6:-4])

    return model if model is not None else "Unknown"