File: meter.py

package info (click to toggle)
zwave-js-server-python 0.67.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,820 kB
  • sloc: python: 15,886; sh: 21; javascript: 16; makefile: 2
file content (38 lines) | stat: -rw-r--r-- 1,258 bytes parent folder | download | duplicates (2)
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
"""Meter Command Class specific utility functions for values."""

from __future__ import annotations

from ...const import CommandClass
from ...const.command_class.meter import (
    CC_SPECIFIC_METER_TYPE,
    CC_SPECIFIC_SCALE,
    METER_TYPE_TO_SCALE_ENUM_MAP,
    MeterScaleType,
    MeterType,
)
from ...exceptions import InvalidCommandClass, UnknownValueData
from ...model.value import Value


def get_meter_type(value: Value) -> MeterType:
    """Get the MeterType for a given value."""
    if value.command_class != CommandClass.METER:
        raise InvalidCommandClass(value, CommandClass.METER)
    try:
        return MeterType(value.metadata.cc_specific[CC_SPECIFIC_METER_TYPE])
    except ValueError:
        raise UnknownValueData(
            value, f"metadata.cc_specific.{CC_SPECIFIC_METER_TYPE}"
        ) from None


def get_meter_scale_type(value: Value) -> MeterScaleType:
    """Get the ScaleType for a given value."""
    meter_type = get_meter_type(value)
    scale_enum = METER_TYPE_TO_SCALE_ENUM_MAP[meter_type]
    try:
        return scale_enum(value.metadata.cc_specific[CC_SPECIFIC_SCALE])
    except ValueError:
        raise UnknownValueData(
            value, f"metadata.cc_specific.{CC_SPECIFIC_SCALE}"
        ) from None