Skip to content

Commit

Permalink
Improve type conversion in proxy (#1178)
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ authored Aug 31, 2023
1 parent f246f7e commit dce359a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
8 changes: 4 additions & 4 deletions hahomematic/platforms/custom/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import logging
from typing import Any, Final

from hahomematic.const import HmPlatform
from hahomematic.const import HmParamsetKey, HmPlatform
from hahomematic.decorators import bind_collector
from hahomematic.platforms import device as hmd
from hahomematic.platforms.custom import definition as hmed
Expand Down Expand Up @@ -524,7 +524,7 @@ async def enable_away_mode_by_calendar(
"""Enable the away mode by calendar on thermostat."""
await self._client.put_paramset(
address=self._attr_channel_address,
paramset_key="VALUES",
paramset_key=HmParamsetKey.VALUES,
value={
"CONTROL_MODE": _HMIP_MODE_AWAY,
"PARTY_TIME_END": end.strftime(_PARTY_DATE_FORMAT),
Expand All @@ -534,7 +534,7 @@ async def enable_away_mode_by_calendar(

await self._client.put_paramset(
address=self._attr_channel_address,
paramset_key="VALUES",
paramset_key=HmParamsetKey.VALUES,
value={
"SET_POINT_TEMPERATURE": away_temperature,
},
Expand All @@ -552,7 +552,7 @@ async def disable_away_mode(self) -> None:
"""Disable the away mode on thermostat."""
await self._client.put_paramset(
address=self._attr_channel_address,
paramset_key="VALUES",
paramset_key=HmParamsetKey.VALUES,
value={
"CONTROL_MODE": _HMIP_MODE_AWAY,
"PARTY_TIME_START": _PARTY_INIT_DATE,
Expand Down
38 changes: 29 additions & 9 deletions hahomematic/xml_rpc_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ async def __async_request(self, *args, **kwargs): # type: ignore[no-untyped-def
raise NoConnection(message) from ose
except xmlrpc.client.Fault as fex:
raise ClientException(fex) from fex
except TypeError as terr:
raise ClientException(terr) from terr
except xmlrpc.client.ProtocolError as per:
if not self._connection_state.has_issue(issuer=self):
if per.errmsg == "Unauthorized":
Expand All @@ -137,12 +139,30 @@ def _cleanup_args(*args: Any) -> Any:
"""Cleanup the type of args."""
if len(args[1]) == 0:
return args
new_args: list[Any] = []
for arg in args[1]:
if isinstance(arg, StrEnum):
new_args.append(str(arg))
elif isinstance(arg, IntEnum):
new_args.append(int(arg))
else:
new_args.append(arg)
return (args[0], tuple(new_args))
if len(args) == 2:
new_args: list[Any] = []
for data in args[1]:
if isinstance(data, dict):
new_args.append(_cleanup_paramset(paramset=data))
else:
new_args.append(_cleanup_parameter(value=data))
return (args[0], tuple(new_args))
_LOGGER.error("XmlRpcProxy command: Too many arguments")
return args


def _cleanup_parameter(value: Any) -> Any:
"""Cleanup a single parameter."""
if isinstance(value, StrEnum):
return str(value)
if isinstance(value, IntEnum):
return int(value)
return value


def _cleanup_paramset(paramset: dict[str, Any]) -> dict[str, Any]:
"""Cleanup a single parameter."""
new_paramset: dict[str, Any] = {}
for name, value in paramset.items():
new_paramset[name] = _cleanup_parameter(value=value)
return new_paramset
4 changes: 2 additions & 2 deletions tests/test_central.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ async def test_central_services(factory: helper.Factory) -> None:
assert mock_client.method_calls[-1] == call.get_all_system_variables(include_internal=True)

assert len(mock_client.method_calls) == 37
await central.load_and_refresh_entity_data(paramset_key="MASTER")
await central.load_and_refresh_entity_data(paramset_key=HmParamsetKey.MASTER)
assert len(mock_client.method_calls) == 37
await central.load_and_refresh_entity_data(paramset_key="VALUES")
await central.load_and_refresh_entity_data(paramset_key=HmParamsetKey.VALUES)
assert len(mock_client.method_calls) == 70

await central.get_system_variable(name="SysVar_Name")
Expand Down

0 comments on commit dce359a

Please sign in to comment.