diff --git a/homeassistant/components/knx/storage/knx_selector.py b/homeassistant/components/knx/storage/knx_selector.py index 8bba12633c8..5adf242c16b 100644 --- a/homeassistant/components/knx/storage/knx_selector.py +++ b/homeassistant/components/knx/storage/knx_selector.py @@ -8,6 +8,7 @@ import voluptuous as vol 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 .util import dpt_string_to_dict class AllSerializeFirst(vol.All): @@ -179,14 +180,6 @@ class GASelector(KNXSelectorBase): def serialize(self) -> dict[str, Any]: """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] = { "write": {"required": self.write_required} if self.write else False, "state": {"required": self.state_required} if self.state else False, @@ -197,12 +190,12 @@ class GASelector(KNXSelectorBase): { "value": item.value, "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 ] 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 { "type": self.selector_type, diff --git a/homeassistant/components/knx/storage/serialize.py b/homeassistant/components/knx/storage/serialize.py index 37ee53df40b..1b453a24562 100644 --- a/homeassistant/components/knx/storage/serialize.py +++ b/homeassistant/components/knx/storage/serialize.py @@ -35,7 +35,9 @@ def knx_serializer( 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.""" if knx_schema := KNX_SCHEMA_FOR_PLATFORM.get(platform): return convert(knx_schema, custom_serializer=knx_serializer) # type: ignore[no-any-return] diff --git a/homeassistant/components/knx/storage/util.py b/homeassistant/components/knx/storage/util.py index a3831070a7e..978a0568455 100644 --- a/homeassistant/components/knx/storage/util.py +++ b/homeassistant/components/knx/storage/util.py @@ -3,11 +3,29 @@ from functools import partial from typing import Any +from xknx.typing import DPTMainSubDict + from homeassistant.helpers.typing import ConfigType 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: """Get the value from a nested dictionary.""" for key in keys: