Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
SukramJ committed Aug 22, 2024
1 parent c04a4b2 commit d079ff5
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 29 deletions.
2 changes: 0 additions & 2 deletions example_local.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,6 @@ async def example_run(self):
interface_configs={interface_config},
client_session=None,
default_callback_port=48888,
enable_server=False,
).create_central()

# Add callbacks to handle the events and see what happens on the system.
Expand All @@ -462,7 +461,6 @@ async def example_run(self):
client_config=_ClientConfig(
central=self.central,
interface_config=interface_config,
ip_addr="127.0.0.1",
),
local_resources=local_resources,
)
Expand Down
34 changes: 19 additions & 15 deletions hahomematic/central/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ def __init__(self, central_config: CentralConfig) -> None:
self._connection_state: Final = central_config.connection_state
self._looper = Looper()
self._xml_rpc_server: xmlrpc.XmlRpcServer | None = None
self.xml_rpc_server_ip_addr: str = IP_ANY_V4
self.xml_rpc_server_port: int = PORT_ANY
self._xml_rpc_server_ip_addr: str = IP_ANY_V4
self._xml_rpc_server_port: int = PORT_ANY

# Caches for CCU data
self.data_cache: Final[CentralDataCache] = CentralDataCache(central=self)
Expand Down Expand Up @@ -266,6 +266,16 @@ def sysvar_entities(self) -> tuple[GenericSystemVariable, ...]:
"""Return the sysvar entities."""
return tuple(self._sysvar_entities.values())

@property
def xml_rpc_server_ip_addr(self) -> str:
"""Return the xml rpc server ip address."""
return self._xml_rpc_server_ip_addr

@property
def xml_rpc_server_port(self) -> int:
"""Return the xml rpc server port."""
return self._xml_rpc_server_port

def add_sysvar_entity(self, sysvar_entity: GenericSystemVariable) -> None:
"""Add new program button."""
if (ccu_var_name := sysvar_entity.ccu_var_name) is not None:
Expand Down Expand Up @@ -305,10 +315,12 @@ async def start(self) -> None:
if self._started:
_LOGGER.debug("START: Central %s already started", self._name)
return
if ip_addr := await self._identify_ip_addr(
port=tuple(self.config.interface_configs)[0].port
if self.config.interface_configs and (
ip_addr := await self._identify_ip_addr(
port=tuple(self.config.interface_configs)[0].port
)
):
self.xml_rpc_server_ip_addr = ip_addr
self._xml_rpc_server_ip_addr = ip_addr
self._xml_rpc_server = (
xmlrpc.create_xml_rpc_server(
ip_addr=ip_addr,
Expand All @@ -319,7 +331,7 @@ async def start(self) -> None:
)
if self._xml_rpc_server:
self._xml_rpc_server.add_central(self)
self.xml_rpc_server_port = self._xml_rpc_server.port if self._xml_rpc_server else 0
self._xml_rpc_server_port = self._xml_rpc_server.port if self._xml_rpc_server else 0

await self.parameter_visibility.load()
if self.config.start_direct:
Expand Down Expand Up @@ -452,7 +464,6 @@ async def _create_clients(self) -> bool:
if client := await hmcl.create_client(
central=self,
interface_config=interface_config,
ip_addr=self.xml_rpc_server_ip_addr,
):
if (
available_interfaces := client.system_information.available_interfaces
Expand Down Expand Up @@ -567,16 +578,9 @@ async def validate_config_and_get_system_information(self) -> SystemInformation:
if len(self.config.interface_configs) == 0:
raise NoClients("validate_config: No clients defined.")

ip_addr = (
self.xml_rpc_server_ip_addr
if self.started
else await self._identify_ip_addr(tuple(self.config.interface_configs)[0].port)
)
system_information = SystemInformation()
for interface_config in self.config.interface_configs:
client = await hmcl.create_client(
central=self, interface_config=interface_config, ip_addr=ip_addr
)
client = await hmcl.create_client(central=self, interface_config=interface_config)
if not system_information.serial:
system_information = client.system_information
except NoClients:
Expand Down
10 changes: 4 additions & 6 deletions hahomematic/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,6 @@ def __init__(
self,
central: hmcu.CentralUnit,
interface_config: InterfaceConfig,
ip_addr: str,
) -> None:
self.central: Final = central
self.version: str = "0"
Expand All @@ -1001,7 +1000,9 @@ def __init__(
self.interface: Final = interface_config.interface
self.interface_id: Final = interface_config.interface_id
self._callback_host: Final[str] = (
central.config.callback_host if central.config.callback_host else ip_addr
central.config.callback_host
if central.config.callback_host
else central.xml_rpc_server_ip_addr
)
self._callback_port: Final[int] = (
central.config.callback_port
Expand Down Expand Up @@ -1117,12 +1118,9 @@ def _init_validate(self) -> None:
async def create_client(
central: hmcu.CentralUnit,
interface_config: InterfaceConfig,
ip_addr: str,
) -> Client:
"""Return a new client for with a given interface_config."""
return await _ClientConfig(
central=central, interface_config=interface_config, ip_addr=ip_addr
).get_client()
return await _ClientConfig(central=central, interface_config=interface_config).get_client()


def get_client(interface_id: str) -> Client | None:
Expand Down
3 changes: 1 addition & 2 deletions tests/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ async def get_unpatched_default_central(
client_config=_ClientConfig(
central=central,
interface_config=interface_config,
ip_addr="127.0.0.1",
),
local_resources=LocalRessources(
address_device_translation=address_device_translation,
Expand Down Expand Up @@ -130,7 +129,7 @@ async def get_default_central(
return_value=const.PROGRAM_DATA if add_programs else [],
).start()
patch(
"hahomematic.central.CentralUnit._identify_callback_ip", return_value="127.0.0.1"
"hahomematic.central.CentralUnit._identify_ip_addr", return_value="127.0.0.1"
).start()

await central.start()
Expand Down
8 changes: 4 additions & 4 deletions tests/test_central.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ async def test_device_export(
(TEST_DEVICES, True, False, False, None, None),
],
)
async def test_identify_callback_ip(
async def test_identify_ip_addr(
central_client_factory: tuple[CentralUnit, Client | Mock, helper.Factory],
) -> None:
"""Test identify_callback_ip."""
"""Test identify_ip_addr."""
central, _, _ = central_client_factory
assert await central._identify_callback_ip(port=54321) == "127.0.0.1"
assert await central._identify_ip_addr(port=54321) == "127.0.0.1"
central.config.host = "no_host"
assert await central._identify_callback_ip(port=54321) == "127.0.0.1"
assert await central._identify_ip_addr(port=54321) == "127.0.0.1"


@pytest.mark.parametrize(
Expand Down

0 comments on commit d079ff5

Please sign in to comment.