From bebdaacf4782bce057023c1f84f37f0d0bfb42bf Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Wed, 9 Feb 2022 09:44:04 +0100 Subject: [PATCH] Change detection of router devices for Fritz (#65965) --- homeassistant/components/fritz/common.py | 13 +++++++++++-- homeassistant/components/fritz/diagnostics.py | 1 + homeassistant/components/fritz/switch.py | 12 ++++-------- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/fritz/common.py b/homeassistant/components/fritz/common.py index 078bb4b4225..ae506989e00 100644 --- a/homeassistant/components/fritz/common.py +++ b/homeassistant/components/fritz/common.py @@ -155,7 +155,8 @@ class FritzBoxTools(update_coordinator.DataUpdateCoordinator): self.hass = hass self.host = host self.mesh_role = MeshRoles.NONE - self.device_is_router: bool = True + self.device_conn_type: str | None = None + self.device_is_router: bool = False self.password = password self.port = port self.username = username @@ -213,7 +214,15 @@ class FritzBoxTools(update_coordinator.DataUpdateCoordinator): self._current_firmware = info.get("NewSoftwareVersion") self._update_available, self._latest_firmware = self._update_device_info() - self.device_is_router = "WANIPConn1" in self.connection.services + if "Layer3Forwarding1" in self.connection.services: + if connection_type := self.connection.call_action( + "Layer3Forwarding1", "GetDefaultConnectionService" + ).get("NewDefaultConnectionService"): + # Return NewDefaultConnectionService sample: "1.WANPPPConnection.1" + self.device_conn_type = connection_type[2:][:-2] + self.device_is_router = self.connection.call_action( + self.device_conn_type, "GetInfo" + ).get("NewEnable") @callback async def _async_update_data(self) -> None: diff --git a/homeassistant/components/fritz/diagnostics.py b/homeassistant/components/fritz/diagnostics.py index f35eca6b914..fa4ff6a7db8 100644 --- a/homeassistant/components/fritz/diagnostics.py +++ b/homeassistant/components/fritz/diagnostics.py @@ -25,6 +25,7 @@ async def async_get_config_entry_diagnostics( "current_firmware": avm_wrapper.current_firmware, "latest_firmware": avm_wrapper.latest_firmware, "update_available": avm_wrapper.update_available, + "connection_type": avm_wrapper.device_conn_type, "is_router": avm_wrapper.device_is_router, "mesh_role": avm_wrapper.mesh_role, "last_update success": avm_wrapper.last_update_success, diff --git a/homeassistant/components/fritz/switch.py b/homeassistant/components/fritz/switch.py index fec760fbe7a..daddae5720c 100644 --- a/homeassistant/components/fritz/switch.py +++ b/homeassistant/components/fritz/switch.py @@ -81,16 +81,12 @@ def port_entities_list( _LOGGER.debug("Setting up %s switches", SWITCH_TYPE_PORTFORWARD) entities_list: list[FritzBoxPortSwitch] = [] - connection_type = avm_wrapper.get_default_connection() - if not connection_type: + if not avm_wrapper.device_conn_type: _LOGGER.debug("The FRITZ!Box has no %s options", SWITCH_TYPE_PORTFORWARD) return [] - # Return NewDefaultConnectionService sample: "1.WANPPPConnection.1" - con_type: str = connection_type["NewDefaultConnectionService"][2:][:-2] - # Query port forwardings and setup a switch for each forward for the current device - resp = avm_wrapper.get_num_port_mapping(con_type) + resp = avm_wrapper.get_num_port_mapping(avm_wrapper.device_conn_type) if not resp: _LOGGER.debug("The FRITZ!Box has no %s options", SWITCH_TYPE_DEFLECTION) return [] @@ -107,7 +103,7 @@ def port_entities_list( for i in range(port_forwards_count): - portmap = avm_wrapper.get_port_mapping(con_type, i) + portmap = avm_wrapper.get_port_mapping(avm_wrapper.device_conn_type, i) if not portmap: _LOGGER.debug("The FRITZ!Box has no %s options", SWITCH_TYPE_DEFLECTION) continue @@ -133,7 +129,7 @@ def port_entities_list( portmap, port_name, i, - con_type, + avm_wrapper.device_conn_type, ) )