Skip to content

Commit

Permalink
Merge pull request #183 from loopj/updated-object-choice-lookup
Browse files Browse the repository at this point in the history
Update approach to looking up SystemObject choices
  • Loading branch information
loopj authored Jan 26, 2025
2 parents 6142f2c + 14d23dd commit 93b75fc
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 37 deletions.
3 changes: 0 additions & 3 deletions src/aiovantage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,6 @@ async def initialize(self, fetch_state: bool = True) -> None:
*[controller.initialize(fetch_state) for controller in self._controllers]
)

# Start the event stream
await self.event_stream.start()

def subscribe(self, callback: EventCallback[SystemObject]) -> Callable[[], None]:
"""Subscribe to state changes for all objects.
Expand Down
11 changes: 5 additions & 6 deletions src/aiovantage/command_client/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import asyncio
import logging
import re
from collections.abc import Sequence
from dataclasses import dataclass
from decimal import Decimal
from ssl import SSLContext
from types import TracebackType
from typing import Any

from typing_extensions import Self

Expand All @@ -29,8 +28,8 @@ class CommandResponse:
"""Wrapper for command responses."""

command: str
args: Sequence[str]
data: Sequence[str]
args: list[str]
data: list[str]


class CommandClient:
Expand Down Expand Up @@ -78,7 +77,7 @@ def close(self) -> None:
async def command(
self,
command: str,
*params: str | int | float | Decimal,
*params: Any,
force_quotes: bool = False,
connection: CommandConnection | None = None,
) -> CommandResponse:
Expand Down Expand Up @@ -112,7 +111,7 @@ async def command(

async def raw_request(
self, request: str, connection: CommandConnection | None = None
) -> Sequence[str]:
) -> list[str]:
"""Send a raw command to the Host Command service and return all response lines.
Handles authentication if required, and raises an exception if the response line
Expand Down
37 changes: 13 additions & 24 deletions src/aiovantage/config_client/interfaces/types.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
"""ObjectChoice type definition."""

import inspect
from dataclasses import dataclass, field
from functools import cache
from types import ModuleType
from typing import Any
from importlib import import_module
from inspect import getmembers, isclass

from aiovantage import objects


def get_all_module_classes(module: ModuleType) -> list[type[Any]]:
"""Get all classes from a module."""
classes: list[type[Any]] = []
for _, cls in inspect.getmembers(module, inspect.isclass):
classes.append(cls)

return classes
from aiovantage.objects import SystemObject


@cache
def get_all_object_choices(module: ModuleType) -> list[dict[str, Any]]:
def get_all_object_choices() -> list[dict[str, str | type[SystemObject]]]:
"""Get all object choices from a module."""
choices: list[dict[str, Any]] = []
for cls in get_all_module_classes(module):
# Get the name of the XML element
meta = getattr(cls, "Meta", None)
name = getattr(meta, "name", cls.__qualname__)

# Add the xsdata choice
choices.append({"name": name, "type": cls})
# Load all SystemObject types into memory
module = import_module("aiovantage.objects")

return choices
# Build and return choices list
return [
{"name": cls.vantage_type(), "type": cls}
for _, cls in getmembers(module, isclass)
if issubclass(cls, SystemObject)
]


@dataclass
Expand All @@ -47,6 +36,6 @@ class ObjectChoice:
choice: object = field(
metadata={
"type": "Wildcard",
"choices": get_all_object_choices(objects), # type: ignore
"choices": get_all_object_choices(),
},
)
7 changes: 3 additions & 4 deletions src/aiovantage/objects/system_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ class SystemObject:
},
)

@property
def vantage_type(self) -> str:
"""Return the Vantage type of the object."""
cls = type(self)
@classmethod
def vantage_type(cls) -> str:
"""Return the Vantage type for this object."""
cls_meta = getattr(cls, "Meta", None)
return getattr(cls_meta, "name", cls.__qualname__)

0 comments on commit 93b75fc

Please sign in to comment.