AI review suggestions

This commit is contained in:
farmio 2025-07-29 09:01:33 +02:00
parent cb6c41bf95
commit ffcc1e7213
3 changed files with 24 additions and 11 deletions

View File

@ -8,6 +8,7 @@ import voluptuous as vol
from ..validation import ga_validator, maybe_ga_validator, sync_state_validator from ..validation import ga_validator, maybe_ga_validator, sync_state_validator
from .const import CONF_DPT, CONF_GA_PASSIVE, CONF_GA_STATE, CONF_GA_WRITE from .const import CONF_DPT, CONF_GA_PASSIVE, CONF_GA_STATE, CONF_GA_WRITE
from .util import dpt_string_to_dict
class AllSerializeFirst(vol.All): class AllSerializeFirst(vol.All):
@ -179,14 +180,6 @@ class GASelector(KNXSelectorBase):
def serialize(self) -> dict[str, Any]: def serialize(self) -> dict[str, Any]:
"""Serialize the selector to a dictionary.""" """Serialize the selector to a dictionary."""
def dpt_to_dict(dpt: str) -> dict[str, int | None]:
"""Convert a DPT string to a dictionary."""
dpt_num = dpt.split(".")
return {
"main": int(dpt_num[0]),
"sub": int(dpt_num[1]) if len(dpt_num) > 1 else None,
}
options: dict[str, Any] = { options: dict[str, Any] = {
"write": {"required": self.write_required} if self.write else False, "write": {"required": self.write_required} if self.write else False,
"state": {"required": self.state_required} if self.state else False, "state": {"required": self.state_required} if self.state else False,
@ -197,12 +190,12 @@ class GASelector(KNXSelectorBase):
{ {
"value": item.value, "value": item.value,
"translation_key": item.value.replace(".", "_"), "translation_key": item.value.replace(".", "_"),
"dpt": dpt_to_dict(item.value), # used to filter DPTs in dropdown "dpt": dpt_string_to_dict(item.value), # used for filtering GAs
} }
for item in self.dpt for item in self.dpt
] ]
if self.valid_dpt is not None: if self.valid_dpt is not None:
options["validDPTs"] = [dpt_to_dict(dpt) for dpt in self.valid_dpt] options["validDPTs"] = [dpt_string_to_dict(dpt) for dpt in self.valid_dpt]
return { return {
"type": self.selector_type, "type": self.selector_type,

View File

@ -35,7 +35,9 @@ def knx_serializer(
return UNSUPPORTED # type: ignore[no-any-return] return UNSUPPORTED # type: ignore[no-any-return]
def get_serialized_schema(platform: Platform) -> dict | list | None: def get_serialized_schema(
platform: Platform,
) -> dict[str, Any] | list[dict[str, Any]] | None:
"""Get the schema for a specific platform.""" """Get the schema for a specific platform."""
if knx_schema := KNX_SCHEMA_FOR_PLATFORM.get(platform): if knx_schema := KNX_SCHEMA_FOR_PLATFORM.get(platform):
return convert(knx_schema, custom_serializer=knx_serializer) # type: ignore[no-any-return] return convert(knx_schema, custom_serializer=knx_serializer) # type: ignore[no-any-return]

View File

@ -3,11 +3,29 @@
from functools import partial from functools import partial
from typing import Any from typing import Any
from xknx.typing import DPTMainSubDict
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from .const import CONF_DPT, CONF_GA_PASSIVE, CONF_GA_STATE, CONF_GA_WRITE from .const import CONF_DPT, CONF_GA_PASSIVE, CONF_GA_STATE, CONF_GA_WRITE
def dpt_string_to_dict(dpt: str) -> DPTMainSubDict:
"""Convert a DPT string to a typed dictionary with main and sub components.
Examples:
>>> dpt_string_to_dict("1.010")
{'main': 1, 'sub': 10}
>>> dpt_string_to_dict("5")
{'main': 5, 'sub': None}
"""
dpt_num = dpt.split(".")
return DPTMainSubDict(
main=int(dpt_num[0]),
sub=int(dpt_num[1]) if len(dpt_num) > 1 else None,
)
def nested_get(dic: ConfigType, *keys: str, default: Any | None = None) -> Any: def nested_get(dic: ConfigType, *keys: str, default: Any | None = None) -> Any:
"""Get the value from a nested dictionary.""" """Get the value from a nested dictionary."""
for key in keys: for key in keys: