File: energy_production.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 (40 lines) | stat: -rw-r--r-- 1,489 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
39
40
"""Energy Production Command Class specific utility functions for values."""

from __future__ import annotations

from ...const import CommandClass
from ...const.command_class.energy_production import (
    CC_SPECIFIC_PARAMETER,
    CC_SPECIFIC_SCALE,
    ENERGY_PRODUCTION_PARAMETER_TO_SCALE_ENUM_MAP,
    EnergyProductionParameter,
    EnergyProductionScaleType,
)
from ...exceptions import InvalidCommandClass, UnknownValueData
from ...model.value import Value


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


def get_energy_production_scale_type(value: Value) -> EnergyProductionScaleType:
    """Get the ScaleType for a given value."""
    parameter = get_energy_production_parameter(value)
    scale_enum = ENERGY_PRODUCTION_PARAMETER_TO_SCALE_ENUM_MAP[parameter]
    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