Skip to content

Commit

Permalink
Move Device model to root and re-arrange Product (#441)
Browse files Browse the repository at this point in the history
  • Loading branch information
DCSBL authored Dec 27, 2024
1 parent bb94152 commit 909caea
Show file tree
Hide file tree
Showing 17 changed files with 314 additions and 315 deletions.
20 changes: 0 additions & 20 deletions homewizard_energy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,3 @@ async def has_v2_api(host: str, websession: ClientSession | None = None) -> bool
finally:
if not websession_provided:
await websession.close()


def get_verification_hostname(model: str, serial_number: str) -> str:
"""Get the verification name for the device."""
model_map = {
"HWE-P1": "p1dongle",
"HWE-SKT": "energysocket",
"HWE-WTR": "watermeter",
"HWE-DSP": "display",
"HWE-KWH1": "energymeter",
"SDM230-wifi": "energymeter",
"HWE-KWH3": "energymeter",
"SDM630-wifi": "energymeter",
"HWE-BAT": "battery",
}

if model not in model_map:
raise ValueError(f"Unsupported model: {model}")

return f"appliance/{model_map[model]}/{serial_number}"
95 changes: 0 additions & 95 deletions homewizard_energy/brand.py

This file was deleted.

168 changes: 168 additions & 0 deletions homewizard_energy/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
"""Common models for HomeWizard Energy API."""

from __future__ import annotations

from dataclasses import dataclass

MODELS = {
"HWE-P1": "p1dongle",
"HWE-SKT": "energysocket",
"HWE-WTR": "watermeter",
"HWE-DSP": "display",
"HWE-KWH1": "energymeter",
"SDM230-wifi": "energymeter",
"HWE-KWH3": "energymeter",
"SDM630-wifi": "energymeter",
"HWE-BAT": "battery",
}


def get_verification_hostname(model: str, serial_number: str) -> str:
"""Helper method to convert device model and serial to identifier
The identifier is used to verify the device in the HomeWizard Energy API via HTTPS.
"""

if model not in MODELS:
raise ValueError(f"Unsupported model: {model}")

return f"appliance/{MODELS[model]}/{serial_number}"


@dataclass
class Device:
"""Represent Device config."""

product: Product | None
product_name: str | None
product_type: str | None
serial: str | None
api_version: str | None
firmware_version: str | None
id: str | None

@staticmethod
def from_dict(data: dict[str, str]) -> Device:
"""Return Device object from API response.
This model is common for both v1 and v2 API.
Args:
data: The data from the HomeWizard Energy `api` API.
Returns:
A Device object.
"""

try:
_id = get_verification_hostname(
data.get("product_type"), data.get("serial")
)
except ValueError:
_id = None

return Device(
product=Product.from_type(data.get("product_type")),
product_name=data.get("product_name"),
product_type=data.get("product_type"),
serial=data.get("serial"),
api_version=data.get("api_version"),
firmware_version=data.get("firmware_version"),
id=_id,
)


@dataclass(frozen=True)
class Product:
"""Represent a product."""

model: str
name: str | None
url: str | None
description: str | None

def __str__(self):
"""Return a string representation of the product."""
return f"HomeWizard {self.name} - {self.model}"

def __hash__(self):
"""Hash a product for unit-test snapshots."""
return hash((self.model, self.name, self.url, self.description))

def __eq__(self, other):
"""Compare two products."""
if isinstance(other, Product):
return (
self.model == other.model
and self.name == other.name
and self.url == other.url
and self.description == other.description
)
return False

@staticmethod
def from_type(product_type: str, _: str | None = None) -> Product | None:
"""Return a Product object from a type.
:param type: The type of the product.
:param locale: The locale to use for the description. Currently ignored. Should be an ISO 639-1 language code.
"""

# pylint: disable=unused-argument

for product in PRODUCTS:
if product.model == product_type:
return product
return None


PRODUCTS = {
Product(
"HWE-P1",
"Wi-Fi P1 Meter",
"https://www.homewizard.com/p1-meter/",
"The HomeWizard P1 Meter gives you detailed insight in your electricity-, gas consumption and solar surplus.",
),
Product(
"HWE-SKT",
"Wi-Fi Energy Socket",
"https://www.homewizard.com/energy-socket/",
"Measure and switch every device.",
),
Product(
"HWE-WTR",
"Wi-Fi Watermeter",
"https://www.homewizard.com/watermeter/",
"Real-time water consumption insights",
),
Product(
"HWE-KWH1",
"Wi-Fi kWh Meter 1-phase",
"https://www.homewizard.com/kwh-meter/",
"Measure solar panels, car chargers and more.",
),
Product(
"HWE-KWH3",
"Wi-Fi kWh Meter 3-phase",
"https://www.homewizard.com/kwh-meter/",
"Measure solar panels, car chargers and more.",
),
Product(
"SDM230-wifi",
"Wi-Fi kWh Meter 1-phase",
"https://www.homewizard.com/kwh-meter/",
"Measure solar panels, car chargers and more.",
),
Product(
"SDM630-wifi",
"Wi-Fi kWh Meter 3-phase",
"https://www.homewizard.com/kwh-meter/",
"Measure solar panels, car chargers and more.",
),
Product(
"HWE-BAT",
"Plug-In Battery",
"https://www.homewizard.com/plug-in-battery/",
"Solar energy, day and night.",
),
}
3 changes: 2 additions & 1 deletion homewizard_energy/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
UnsupportedError,
)

from ..models import Device
from .const import SUPPORTED_API_VERSION
from .models import Data, Device, State, System
from .models import Data, State, System

_LOGGER = logging.getLogger(__name__)

Expand Down
35 changes: 1 addition & 34 deletions homewizard_energy/v1/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Models for HomeWizard Energy."""
"""Models for HomeWizard Energy v1."""

from __future__ import annotations

Expand All @@ -8,39 +8,6 @@
from enum import Enum
from typing import Any

from homewizard_energy.brand import Product, from_type


@dataclass
class Device:
"""Represent Device config."""

product: Product | None
product_name: str | None
product_type: str | None
serial: str | None
api_version: str | None
firmware_version: str | None

@staticmethod
def from_dict(data: dict[str, str]) -> Device:
"""Return Device object from API response.
Args:
data: The data from the HomeWizard Energy `api` API.
Returns:
A Device object.
"""
return Device(
product=from_type(data.get("product_type")),
product_name=data.get("product_name"),
product_type=data.get("product_type"),
serial=data.get("serial"),
api_version=data.get("api_version"),
firmware_version=data.get("firmware_version"),
)


@dataclass
class Data:
Expand Down
3 changes: 2 additions & 1 deletion homewizard_energy/v2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
UnauthorizedError,
)

from ..models import Device
from .cacert import CACERT
from .models import Device, Measurement, System, SystemUpdate
from .models import Measurement, System, SystemUpdate

_LOGGER = logging.getLogger(__name__)

Expand Down
Loading

0 comments on commit 909caea

Please sign in to comment.