Skip to content

Commit

Permalink
Use AwesomeVersion for api_version
Browse files Browse the repository at this point in the history
  • Loading branch information
DCSBL committed Jan 23, 2025
1 parent 595ff72 commit 82091d0
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 39 deletions.
21 changes: 20 additions & 1 deletion homewizard_energy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,27 @@
from enum import StrEnum
from typing import Any

from awesomeversion import AwesomeVersion
from mashumaro.config import BaseConfig
from mashumaro.exceptions import MissingField
from mashumaro.mixins.orjson import DataClassORJSONMixin
from mashumaro.types import SerializationStrategy

from .const import LOGGER, MODEL_TO_ID, MODEL_TO_NAME, Model
from .utils import get_awesome_version


class AwesomeVersionSerializationStrategy(SerializationStrategy, use_annotations=True):
"""Serialization strategy for AwesomeVersion objects."""

def serialize(self, value: AwesomeVersion) -> str:
"""Serialize AwesomeVersion object to string."""
return str(value)

def deserialize(self, value: str) -> AwesomeVersion | None:
"""Deserialize string to AwesomeVersion object."""
version = get_awesome_version(value)
return version


class BaseModel(DataClassORJSONMixin):
Expand All @@ -22,6 +38,9 @@ class Config(BaseConfig):
"""Mashumaro configuration."""

serialize_by_alias = True
serialization_strategy = { # noqa: RUF012
AwesomeVersion: AwesomeVersionSerializationStrategy()
}
omit_none = True


Expand Down Expand Up @@ -95,7 +114,7 @@ class Device(BaseModel):
product_name: str = field()
product_type: str = field()
serial: str = field()
api_version: str = field()
api_version: AwesomeVersion = field()
firmware_version: str = field()

@classmethod
Expand Down
13 changes: 13 additions & 0 deletions homewizard_energy/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"""Utilities for Python HomeWizard Energy."""

from functools import lru_cache

from awesomeversion import AwesomeVersion


@lru_cache
def get_awesome_version(version: str) -> AwesomeVersion:
"""Return a cached AwesomeVersion object."""
if version.lower() == "v1":
return AwesomeVersion("1.0.0")
return AwesomeVersion(version)
13 changes: 12 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ multidict = "^6.0.5" ## To fix aiohttp dependency at python 3.12
backoff = "^2.2.1"
orjson = "^3.10"
mashumaro = "^3.15"
awesomeversion = "^24.6.0"

[tool.poetry.dev-dependencies]
aresponses = "^3.0.0"
Expand Down
42 changes: 21 additions & 21 deletions tests/v1/__snapshots__/test_v1_homewizard_energy.ambr

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions tests/v1/__snapshots__/test_v1_models.ambr
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# serializer version: 1
# name: test_combined_remaps_legacy_brightness_to_system
CombinedModels(device=Device(model_name='Wi-Fi Energy Socket', id='appliance/energysocket/3c39e7aabbcc', product_name='Energy Socket', product_type='HWE-SKT', serial='3c39e7aabbcc', api_version='v1', firmware_version='2.11'), measurement=None, state=State(power_on=False, switch_lock=False, brightness=255), system=System(wifi_ssid=None, wifi_rssi_db=None, cloud_enabled=None, uptime_s=None, status_led_brightness_pct=100.0, api_v1_enabled=None))
CombinedModels(device=Device(model_name='Wi-Fi Energy Socket', id='appliance/energysocket/3c39e7aabbcc', product_name='Energy Socket', product_type='HWE-SKT', serial='3c39e7aabbcc', api_version=<AwesomeVersion SemVer '1.0.0'>, firmware_version='2.11'), measurement=None, state=State(power_on=False, switch_lock=False, brightness=255), system=System(wifi_ssid=None, wifi_rssi_db=None, cloud_enabled=None, uptime_s=None, status_led_brightness_pct=100.0, api_v1_enabled=None))
# ---
# name: test_combined_remaps_legacy_wifi_ssid_to_system
CombinedModels(device=Device(model_name='Wi-Fi Energy Socket', id='appliance/energysocket/3c39e7aabbcc', product_name='Energy Socket', product_type='HWE-SKT', serial='3c39e7aabbcc', api_version='v1', firmware_version='2.11'), measurement=Measurement(wifi_ssid='My Wi-Fi', wifi_strength=100, energy_import_kwh=10830.511, energy_import_t1_kwh=10830.511, energy_import_t2_kwh=None, energy_import_t3_kwh=None, energy_import_t4_kwh=None, energy_export_kwh=2948.827, energy_export_t1_kwh=2948.827, energy_export_t2_kwh=None, energy_export_t3_kwh=None, energy_export_t4_kwh=None, power_w=123.0, power_l1_w=123.0, power_l2_w=None, power_l3_w=None, voltage_v=None, voltage_l1_v=None, voltage_l2_v=None, voltage_l3_v=None, current_a=None, current_l1_a=None, current_l2_a=None, current_l3_a=None, apparent_power_va=None, apparent_power_l1_va=None, apparent_power_l2_va=None, apparent_power_l3_va=None, reactive_power_var=None, reactive_power_l1_var=None, reactive_power_l2_var=None, reactive_power_l3_var=None, power_factor=None, power_factor_l1=None, power_factor_l2=None, power_factor_l3=None, frequency_hz=None, timestamp=None, protocol_version=None, meter_model=None, unique_id=None, tariff=None, voltage_sag_l1_count=None, voltage_sag_l2_count=None, voltage_sag_l3_count=None, voltage_swell_l1_count=None, voltage_swell_l2_count=None, voltage_swell_l3_count=None, any_power_fail_count=None, long_power_fail_count=None, average_power_15m_w=None, monthly_power_peak_w=None, monthly_power_peak_timestamp=None, external_devices=None, active_liter_lpm=None, total_liter_m3=None, cycles=None, state_of_charge_pct=None), state=None, system=System(wifi_ssid='My Wi-Fi', wifi_rssi_db=None, cloud_enabled=None, uptime_s=None, status_led_brightness_pct=None, api_v1_enabled=None))
CombinedModels(device=Device(model_name='Wi-Fi Energy Socket', id='appliance/energysocket/3c39e7aabbcc', product_name='Energy Socket', product_type='HWE-SKT', serial='3c39e7aabbcc', api_version=<AwesomeVersion SemVer '1.0.0'>, firmware_version='2.11'), measurement=Measurement(wifi_ssid='My Wi-Fi', wifi_strength=100, energy_import_kwh=10830.511, energy_import_t1_kwh=10830.511, energy_import_t2_kwh=None, energy_import_t3_kwh=None, energy_import_t4_kwh=None, energy_export_kwh=2948.827, energy_export_t1_kwh=2948.827, energy_export_t2_kwh=None, energy_export_t3_kwh=None, energy_export_t4_kwh=None, power_w=123.0, power_l1_w=123.0, power_l2_w=None, power_l3_w=None, voltage_v=None, voltage_l1_v=None, voltage_l2_v=None, voltage_l3_v=None, current_a=None, current_l1_a=None, current_l2_a=None, current_l3_a=None, apparent_power_va=None, apparent_power_l1_va=None, apparent_power_l2_va=None, apparent_power_l3_va=None, reactive_power_var=None, reactive_power_l1_var=None, reactive_power_l2_var=None, reactive_power_l3_var=None, power_factor=None, power_factor_l1=None, power_factor_l2=None, power_factor_l3=None, frequency_hz=None, timestamp=None, protocol_version=None, meter_model=None, unique_id=None, tariff=None, voltage_sag_l1_count=None, voltage_sag_l2_count=None, voltage_sag_l3_count=None, voltage_swell_l1_count=None, voltage_swell_l2_count=None, voltage_swell_l3_count=None, any_power_fail_count=None, long_power_fail_count=None, average_power_15m_w=None, monthly_power_peak_w=None, monthly_power_peak_timestamp=None, external_devices=None, active_liter_lpm=None, total_liter_m3=None, cycles=None, state_of_charge_pct=None), state=None, system=System(wifi_ssid='My Wi-Fi', wifi_rssi_db=None, cloud_enabled=None, uptime_s=None, status_led_brightness_pct=None, api_v1_enabled=None))
# ---
# name: test_data[HWE-KWH1-fixtures3]
Measurement(wifi_ssid='My Wi-Fi', wifi_strength=74, energy_import_kwh=122.376, energy_import_t1_kwh=122.376, energy_import_t2_kwh=None, energy_import_t3_kwh=None, energy_import_t4_kwh=None, energy_export_kwh=579.813, energy_export_t1_kwh=579.813, energy_export_t2_kwh=None, energy_export_t3_kwh=None, energy_export_t4_kwh=None, power_w=-45.252, power_l1_w=-45.252, power_l2_w=None, power_l3_w=None, voltage_v=228.472, voltage_l1_v=None, voltage_l2_v=None, voltage_l3_v=None, current_a=0.273, current_l1_a=None, current_l2_a=None, current_l3_a=None, apparent_power_va=74.052, apparent_power_l1_va=None, apparent_power_l2_va=None, apparent_power_l3_va=None, reactive_power_var=-58.612, reactive_power_l1_var=None, reactive_power_l2_var=None, reactive_power_l3_var=None, power_factor=0.611, power_factor_l1=None, power_factor_l2=None, power_factor_l3=None, frequency_hz=50.0, timestamp=None, protocol_version=None, meter_model=None, unique_id=None, tariff=None, voltage_sag_l1_count=None, voltage_sag_l2_count=None, voltage_sag_l3_count=None, voltage_swell_l1_count=None, voltage_swell_l2_count=None, voltage_swell_l3_count=None, any_power_fail_count=None, long_power_fail_count=None, average_power_15m_w=None, monthly_power_peak_w=None, monthly_power_peak_timestamp=None, external_devices=None, active_liter_lpm=None, total_liter_m3=None, cycles=None, state_of_charge_pct=None)
Expand Down Expand Up @@ -42,25 +42,25 @@
Measurement(wifi_ssid=None, wifi_strength=None, energy_import_kwh=None, energy_import_t1_kwh=None, energy_import_t2_kwh=None, energy_import_t3_kwh=None, energy_import_t4_kwh=None, energy_export_kwh=None, energy_export_t1_kwh=None, energy_export_t2_kwh=None, energy_export_t3_kwh=None, energy_export_t4_kwh=None, power_w=None, power_l1_w=None, power_l2_w=None, power_l3_w=None, voltage_v=None, voltage_l1_v=None, voltage_l2_v=None, voltage_l3_v=None, current_a=None, current_l1_a=None, current_l2_a=None, current_l3_a=None, apparent_power_va=None, apparent_power_l1_va=None, apparent_power_l2_va=None, apparent_power_l3_va=None, reactive_power_var=None, reactive_power_l1_var=None, reactive_power_l2_var=None, reactive_power_l3_var=None, power_factor=None, power_factor_l1=None, power_factor_l2=None, power_factor_l3=None, frequency_hz=None, timestamp=None, protocol_version=None, meter_model=None, unique_id=None, tariff=None, voltage_sag_l1_count=None, voltage_sag_l2_count=None, voltage_sag_l3_count=None, voltage_swell_l1_count=None, voltage_swell_l2_count=None, voltage_swell_l3_count=None, any_power_fail_count=None, long_power_fail_count=None, average_power_15m_w=None, monthly_power_peak_w=None, monthly_power_peak_timestamp=None, external_devices={'None_NGLYD': ExternalDevice(unique_id='NGLYD', type=None, value=111.111, unit='m3', timestamp=datetime.datetime(2021, 6, 6, 14, 0, 10))}, active_liter_lpm=None, total_liter_m3=None, cycles=None, state_of_charge_pct=None)
# ---
# name: test_device[HWE-KWH1-fixtures3]
Device(model_name='Wi-Fi kWh Meter 1-phase', id='appliance/energymeter/3c39e7aabbcc', product_name='kWh Meter', product_type='HWE-KWH1', serial='3c39e7aabbcc', api_version='v1', firmware_version='2.11')
Device(model_name='Wi-Fi kWh Meter 1-phase', id='appliance/energymeter/3c39e7aabbcc', product_name='kWh Meter', product_type='HWE-KWH1', serial='3c39e7aabbcc', api_version=<AwesomeVersion SemVer '1.0.0'>, firmware_version='2.11')
# ---
# name: test_device[HWE-KWH3-fixtures4]
Device(model_name='Wi-Fi kWh Meter 3-phase', id='appliance/energymeter/3c39e7aabbcc', product_name='kWh Meter 3-phase', product_type='HWE-KWH3', serial='3c39e7aabbcc', api_version='v1', firmware_version='2.11')
Device(model_name='Wi-Fi kWh Meter 3-phase', id='appliance/energymeter/3c39e7aabbcc', product_name='kWh Meter 3-phase', product_type='HWE-KWH3', serial='3c39e7aabbcc', api_version=<AwesomeVersion SemVer '1.0.0'>, firmware_version='2.11')
# ---
# name: test_device[HWE-P1-fixtures0]
Device(model_name='Wi-Fi P1 Meter', id='appliance/p1dongle/3c39e7aabbcc', product_name='P1 Meter', product_type='HWE-P1', serial='3c39e7aabbcc', api_version='v1', firmware_version='2.11')
Device(model_name='Wi-Fi P1 Meter', id='appliance/p1dongle/3c39e7aabbcc', product_name='P1 Meter', product_type='HWE-P1', serial='3c39e7aabbcc', api_version=<AwesomeVersion SemVer '1.0.0'>, firmware_version='2.11')
# ---
# name: test_device[HWE-SKT-fixtures1]
Device(model_name='Wi-Fi Energy Socket', id='appliance/energysocket/3c39e7aabbcc', product_name='Energy Socket', product_type='HWE-SKT', serial='3c39e7aabbcc', api_version='v1', firmware_version='2.11')
Device(model_name='Wi-Fi Energy Socket', id='appliance/energysocket/3c39e7aabbcc', product_name='Energy Socket', product_type='HWE-SKT', serial='3c39e7aabbcc', api_version=<AwesomeVersion SemVer '1.0.0'>, firmware_version='2.11')
# ---
# name: test_device[HWE-WTR-fixtures2]
Device(model_name='Wi-Fi Watermeter', id='appliance/watermeter/3c39e7aabbcc', product_name='Water Meter', product_type='HWE-WTR', serial='3c39e7aabbcc', api_version='v1', firmware_version='2.11')
Device(model_name='Wi-Fi Watermeter', id='appliance/watermeter/3c39e7aabbcc', product_name='Water Meter', product_type='HWE-WTR', serial='3c39e7aabbcc', api_version=<AwesomeVersion SemVer '1.0.0'>, firmware_version='2.11')
# ---
# name: test_device[SDM230-wifi-fixtures5]
Device(model_name='Wi-Fi kWh Meter 1-phase', id='appliance/energymeter/3c39e7aabbcc', product_name='kWh Meter', product_type='SDM230-wifi', serial='3c39e7aabbcc', api_version='v1', firmware_version='2.11')
Device(model_name='Wi-Fi kWh Meter 1-phase', id='appliance/energymeter/3c39e7aabbcc', product_name='kWh Meter', product_type='SDM230-wifi', serial='3c39e7aabbcc', api_version=<AwesomeVersion SemVer '1.0.0'>, firmware_version='2.11')
# ---
# name: test_device[SDM630-wifi-fixtures6]
Device(model_name='Wi-Fi kWh Meter 3-phase', id='appliance/energymeter/3c39e7aabbcc', product_name='kWh Meter 3-phase', product_type='SDM630-wifi', serial='3c39e7aabbcc', api_version='v1', firmware_version='2.11')
Device(model_name='Wi-Fi kWh Meter 3-phase', id='appliance/energymeter/3c39e7aabbcc', product_name='kWh Meter 3-phase', product_type='SDM630-wifi', serial='3c39e7aabbcc', api_version=<AwesomeVersion SemVer '1.0.0'>, firmware_version='2.11')
# ---
# name: test_state[HWE-SKT-fixtures0]
State(power_on=False, switch_lock=False, brightness=255)
Expand Down
Loading

0 comments on commit 82091d0

Please sign in to comment.