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

Refactor Manufacturer Identification #92

Merged
merged 8 commits into from
Feb 8, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 3 additions & 3 deletions custom_components/sat/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -489,14 +489,14 @@ async def async_step_manufacturer(self, _user_input: dict[str, Any] | None = Non

try:
manufacturers = ManufacturerFactory.resolve_by_member_id(coordinator.member_id)
default_manufacturer = manufacturers[0].name if len(manufacturers) > 0 else -1
default_manufacturer = manufacturers[0].friendly_name if len(manufacturers) > 0 else -1
finally:
await coordinator.async_will_remove_from_hass()

options = []
for name, _info in MANUFACTURERS.items():
for name, _member_id in MANUFACTURERS.items():
manufacturer = ManufacturerFactory.resolve_by_name(name)
options.append({"value": name, "label": manufacturer.name})
options.append({"value": name, "label": manufacturer.friendly_name})

return self.async_show_form(
last_step=True,
Expand Down
65 changes: 37 additions & 28 deletions custom_components/sat/manufacturer.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,63 @@
from abc import abstractmethod
from abc import ABC, abstractmethod
from typing import Optional, List, Type

from typing import List, Optional
from custom_components.sat.helpers import snake_case

MANUFACTURERS = {
"ATAG": {"module": "atag", "class": "ATAG", "id": 4},
"Baxi": {"module": "baxi", "class": "Baxi", "id": 4},
"Brotge": {"module": "brotge", "class": "Brotge", "id": 4},
"Geminox": {"module": "geminox", "class": "Geminox", "id": 4},
"Ideal": {"module": "ideal", "class": "Ideal", "id": 6},
"Ferroli": {"module": "ferroli", "class": "Ferroli", "id": 9},
"DeDietrich": {"module": "dedietrich", "class": "DeDietrich", "id": 11},
"Vaillant": {"module": "vaillant", "class": "Vaillant", "id": 24},
"Immergas": {"module": "immergas", "class": "Immergas", "id": 27},
"Sime": {"module": "sime", "class": "Sime", "id": 27},
"Viessmann": {"module": "viessmann", "class": "Viessmann", "id": 33},
"Radiant": {"module": "radiant", "class": "Radiant", "id": 41},
"Nefit": {"module": "nefit", "class": "Nefit", "id": 131},
"Intergas": {"module": "intergas", "class": "Intergas", "id": 173},
"Other": {"module": "other", "class": "Other", "id": -1},
"Atag": 4,
"Baxi": 4,
"Brotge": 4,
"DeDietrich": 4,
"Ferroli": 9,
"Geminox": 4,
"Ideal": 6,
"Immergas": 27,
"Intergas": 173,
"Itho": 29,
"Nefit": 131,
"Radiant": 41,
"Remeha": 11,
"Sime": 27,
"Vaillant": 24,
"Viessmann": 33,
"Worcester": 95,
"Other": -1,
}


class Manufacturer:
class Manufacturer(ABC):
def __init__(self, member_id: int):
self._member_id = member_id

@property
def member_id(self) -> int:
return self._member_id

@property
@abstractmethod
def name(self) -> str:
def friendly_name(self) -> str:
pass


class ManufacturerFactory:
@staticmethod
def resolve_by_name(name: str) -> Optional[Manufacturer]:
"""Resolve a Manufacturer instance by its name."""
manufacturer = MANUFACTURERS.get(name)
if not manufacturer:
if not (member_id := MANUFACTURERS.get(name)):
return None

return ManufacturerFactory._import_class(manufacturer["module"], manufacturer["class"])()
return ManufacturerFactory._import_class(snake_case(name), name)(member_id)

@staticmethod
def resolve_by_member_id(member_id: int) -> List[Manufacturer]:
"""Resolve a list of Manufacturer instances by member ID."""
return [
ManufacturerFactory._import_class(info["module"], info["class"])()
for name, info in MANUFACTURERS.items()
if info["id"] == member_id
ManufacturerFactory._import_class(snake_case(name), name)(identifier)
for name, identifier in MANUFACTURERS.items()
if member_id == identifier
]

@staticmethod
def _import_class(module_name: str, class_name: str):
def _import_class(module_name: str, class_name: str) -> Type[Manufacturer]:
"""Dynamically import and return a Manufacturer class."""
module = __import__(f"custom_components.sat.manufacturers.{module_name}", fromlist=[class_name])
return getattr(module, class_name)
return getattr(__import__(f"custom_components.sat.manufacturers.{module_name}", fromlist=[class_name]), class_name)
4 changes: 2 additions & 2 deletions custom_components/sat/manufacturers/atag.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ..manufacturer import Manufacturer


class ATAG(Manufacturer):
class Atag(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'ATAG'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/baxi.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Baxi(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Baxi'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/brotge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Brotge(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'BRÖTGE'
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class DeDietrich(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'De Dietrich'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/ferroli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Ferroli(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Ferroli'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/geminox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Geminox(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Geminox'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/ideal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Ideal(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Ideal'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/immergas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Immergas(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Immergas'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/intergas.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Intergas(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Intergas'
7 changes: 7 additions & 0 deletions custom_components/sat/manufacturers/itho.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ..manufacturer import Manufacturer


class Itho(Manufacturer):
@property
def friendly_name(self) -> str:
return 'Itho'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/nefit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Nefit(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Nefit'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/other.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Other(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Other'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/radiant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Radiant(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Radiant'
7 changes: 7 additions & 0 deletions custom_components/sat/manufacturers/remeha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ..manufacturer import Manufacturer


class Remeha(Manufacturer):
@property
def friendly_name(self) -> str:
return 'Remeha'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/sime.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Sime(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Sime'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/vaillant.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Vaillant(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Vaillant'
2 changes: 1 addition & 1 deletion custom_components/sat/manufacturers/viessmann.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

class Viessmann(Manufacturer):
@property
def name(self) -> str:
def friendly_name(self) -> str:
return 'Viessmann'
7 changes: 7 additions & 0 deletions custom_components/sat/manufacturers/worcester.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from ..manufacturer import Manufacturer


class Worcester(Manufacturer):
@property
def friendly_name(self) -> str:
return 'Worcester Bosch'
13 changes: 7 additions & 6 deletions tests/test_manufacturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

def test_resolve_by_name():
"""Test resolving manufacturers by name."""
for name, data in MANUFACTURERS.items():
# Test valid name
for name, module in MANUFACTURERS.items():
manufacturer = ManufacturerFactory.resolve_by_name(name)
assert manufacturer is not None, f"Manufacturer '{name}' should not be None"
assert manufacturer.__class__.__name__ == data["class"]
assert manufacturer.__class__.__name__ == name

# Test invalid name
manufacturer = ManufacturerFactory.resolve_by_name("InvalidName")
Expand All @@ -16,9 +15,11 @@ def test_resolve_by_name():

def test_resolve_by_member_id():
"""Test resolving manufacturers by member ID."""
member_id_to_names = {data["id"]: [] for data in MANUFACTURERS.values()}
for name, data in MANUFACTURERS.items():
member_id_to_names[data["id"]].append(name)
manufacturers = ManufacturerFactory.all()
member_id_to_names = {manufacturer.identifier: [] for manufacturer in manufacturers}

for manufacturer in manufacturers:
member_id_to_names[manufacturer.identifier].append(type(manufacturer).__name__)

for member_id, names in member_id_to_names.items():
manufacturers = ManufacturerFactory.resolve_by_member_id(member_id)
Expand Down
Loading