Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy performance fix #435

Merged
merged 3 commits into from
May 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions scripts/generate_multilevel_sensor_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ def generate_int_enum_class_definition(
enum_ref_url: str | None = None,
get_id_func: Callable | None = None,
docstring_info: str = "",
base_class: str = "IntEnum",
) -> List[str]:
"""Generate an IntEnum class definition as an array of lines of string."""
class_def = []
class_def.append(f"class {class_name}(IntEnum):")
class_def.append(f"class {class_name}({base_class}):")
docstring = (
f'"""Enum for known {docstring_info} multilevel sensor types."""'.replace(
" ", " "
Expand All @@ -133,6 +134,14 @@ def generate_int_enum_class_definition(
return class_def


def generate_int_enum_base_class(class_name: str, docstring: str):
cdce8p marked this conversation as resolved.
Show resolved Hide resolved
"""Generate an IntEnum base class definition."""
class_def: list[str] = []
class_def.append(f"class {class_name}(IntEnum):")
class_def.append(f"\t{docstring}")
return class_def


SENSOR_TYPE_URL = (
f"https://github.com/{GITHUB_PROJECT}/blob/{BRANCH_NAME}/{SENSOR_TYPES_FILE_PATH}"
)
Expand All @@ -146,7 +155,7 @@ def generate_int_enum_class_definition(
"# ----------------------------------------------------------------------------------- #",
"",
"from enum import IntEnum",
"from typing import Dict, Set, Type, Union",
"from typing import Dict, Set, Type",
'CC_SPECIFIC_SCALE = "scale"',
'CC_SPECIFIC_SENSOR_TYPE = "sensorType"',
]
Expand All @@ -160,6 +169,13 @@ def generate_int_enum_class_definition(
)
)

lines.extend(
generate_int_enum_base_class(
"MultilevelSensorScaleType",
docstring='"""Common base class for multilevel sensor scale enums."""',
)
)

unit_name_to_enum_map = defaultdict(list)
for scale_name, scale_dict in scales.items():
lines.extend(
Expand All @@ -168,6 +184,7 @@ def generate_int_enum_class_definition(
scale_dict,
SENSOR_TYPE_URL,
docstring_info=f"scales for {scale_name}",
base_class="MultilevelSensorScaleType",
)
)
for unit_name in scale_dict.keys():
Expand All @@ -180,10 +197,6 @@ def generate_int_enum_class_definition(
for unit_name, enum_list in unit_name_to_enum_map.items():
unit_name_to_enum_map[unit_name] = sorted(enum_list)

scale_class_names = [format_for_class_name(scale_name) for scale_name in scales]
lines.extend(
[f"MultilevelSensorScaleType = Union[{', '.join(sorted(scale_class_names))}]", ""]
)

multilevel_sensor_type_to_scale_map_line = (
"MULTILEVEL_SENSOR_TYPE_TO_SCALE_MAP: Dict[MultilevelSensorType, "
Expand Down
16 changes: 9 additions & 7 deletions zwave_js_server/const/command_class/meter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Constants for Meter CC."""
from enum import IntEnum
from typing import Dict, Set, Type, Union
from typing import Dict, Set, Type

VALUE_PROPERTY = "value"

Expand All @@ -27,7 +27,11 @@ class MeterType(IntEnum):
COOLING = 5


class ElectricScale(IntEnum):
class MeterScaleType(IntEnum):
"""Common base class for meter scale enums."""


class ElectricScale(MeterScaleType):
"""Enum with all known electric meter scale values."""

KILOWATT_HOUR = 0
Expand All @@ -41,15 +45,15 @@ class ElectricScale(IntEnum):
KILOVOLT_AMPERE_REACTIVE_HOUR = 8


class GasScale(IntEnum):
class GasScale(MeterScaleType):
"""Enum with all known gas meter scale values."""

CUBIC_METER = 0
CUBIC_FEET = 1
PULSE_COUNT = 3


class WaterScale(IntEnum):
class WaterScale(MeterScaleType):
"""Enum with all known water meter scale values."""

CUBIC_METER = 0
Expand All @@ -58,16 +62,14 @@ class WaterScale(IntEnum):
PULSE_COUNT = 3


class HeatingScale(IntEnum):
class HeatingScale(MeterScaleType):
"""Enum with all known heating meter scale values."""

KILOWATT_HOUR = 0


CoolingScale = HeatingScale

MeterScaleType = Union[CoolingScale, ElectricScale, GasScale, HeatingScale, WaterScale]

METER_TYPE_TO_SCALE_ENUM_MAP: Dict[MeterType, Type[MeterScaleType]] = {
MeterType.ELECTRIC: ElectricScale,
MeterType.GAS: GasScale,
Expand Down
Loading