From c043142b861a10375b3f41bc5db5ab9358841e52 Mon Sep 17 00:00:00 2001 From: Michael <35783820+mib1185@users.noreply.github.com> Date: Sat, 5 Oct 2024 19:16:34 +0200 Subject: [PATCH] Improve handling of call deflection switches in AVM Fritz!Tools (#127592) improve handling of call_deflection switches --- homeassistant/components/fritz/switch.py | 6 +- tests/components/fritz/const.py | 8 + .../fritz/snapshots/test_switch.ambr | 183 ++++++++++++++++-- tests/components/fritz/test_switch.py | 21 +- 4 files changed, 181 insertions(+), 37 deletions(-) diff --git a/homeassistant/components/fritz/switch.py b/homeassistant/components/fritz/switch.py index dfcb1162c3e..372af89cc9e 100644 --- a/homeassistant/components/fritz/switch.py +++ b/homeassistant/components/fritz/switch.py @@ -46,9 +46,7 @@ async def _async_deflection_entities_list( _LOGGER.debug("Setting up %s switches", SWITCH_TYPE_DEFLECTION) - if ( - call_deflections := avm_wrapper.data.get("call_deflections") - ) is None or not isinstance(call_deflections, dict): + if not (call_deflections := avm_wrapper.data["call_deflections"]): _LOGGER.debug("The FRITZ!Box has no %s options", SWITCH_TYPE_DEFLECTION) return [] @@ -72,7 +70,7 @@ async def _async_port_entities_list( # Query port forwardings and setup a switch for each forward for the current device resp = await avm_wrapper.async_get_num_port_mapping(avm_wrapper.device_conn_type) if not resp: - _LOGGER.debug("The FRITZ!Box has no %s options", SWITCH_TYPE_DEFLECTION) + _LOGGER.debug("The FRITZ!Box has no %s options", SWITCH_TYPE_PORTFORWARD) return [] port_forwards_count: int = resp["NewPortMappingNumberOfEntries"] diff --git a/tests/components/fritz/const.py b/tests/components/fritz/const.py index 0d1222dfcda..0817cc5d804 100644 --- a/tests/components/fritz/const.py +++ b/tests/components/fritz/const.py @@ -904,6 +904,14 @@ MOCK_HOST_ATTRIBUTES_DATA = [ }, ] +MOCK_CALL_DEFLECTION_DATA = { + "X_AVM-DE_OnTel1": { + "GetDeflections": { + "NewDeflectionList": "00fromAll+1234657890eImmediately" + } + } +} + MOCK_USER_DATA = MOCK_CONFIG[DOMAIN][CONF_DEVICES][0] MOCK_USER_INPUT_ADVANCED = MOCK_USER_DATA MOCK_USER_INPUT_SIMPLE = { diff --git a/tests/components/fritz/snapshots/test_switch.ambr b/tests/components/fritz/snapshots/test_switch.ambr index 048f6e005ec..b34a3626fe2 100644 --- a/tests/components/fritz/snapshots/test_switch.ambr +++ b/tests/components/fritz/snapshots/test_switch.ambr @@ -1,5 +1,5 @@ # serializer version: 1 -# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.mock_title_wi_fi_wifi_2_4ghz-entry] +# name: test_switch_setup[fc_data0][switch.mock_title_wi_fi_wifi_2_4ghz-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -32,7 +32,7 @@ 'unit_of_measurement': None, }) # --- -# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.mock_title_wi_fi_wifi_2_4ghz-state] +# name: test_switch_setup[fc_data0][switch.mock_title_wi_fi_wifi_2_4ghz-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Mock Title Wi-Fi WiFi (2.4Ghz)', @@ -46,7 +46,7 @@ 'state': 'on', }) # --- -# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.mock_title_wi_fi_wifi_5ghz-entry] +# name: test_switch_setup[fc_data0][switch.mock_title_wi_fi_wifi_5ghz-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -79,7 +79,7 @@ 'unit_of_measurement': None, }) # --- -# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.mock_title_wi_fi_wifi_5ghz-state] +# name: test_switch_setup[fc_data0][switch.mock_title_wi_fi_wifi_5ghz-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Mock Title Wi-Fi WiFi (5Ghz)', @@ -93,7 +93,7 @@ 'state': 'on', }) # --- -# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.printer_internet_access-entry] +# name: test_switch_setup[fc_data0][switch.printer_internet_access-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -126,7 +126,7 @@ 'unit_of_measurement': None, }) # --- -# name: test_switch_setup[fc_data0-expected_wifi_names0][switch.printer_internet_access-state] +# name: test_switch_setup[fc_data0][switch.printer_internet_access-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'printer Internet Access', @@ -140,7 +140,7 @@ 'state': 'on', }) # --- -# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.mock_title_wi_fi_wifi-entry] +# name: test_switch_setup[fc_data1][switch.mock_title_wi_fi_wifi-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -173,7 +173,7 @@ 'unit_of_measurement': None, }) # --- -# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.mock_title_wi_fi_wifi-state] +# name: test_switch_setup[fc_data1][switch.mock_title_wi_fi_wifi-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Mock Title Wi-Fi WiFi', @@ -187,7 +187,7 @@ 'state': 'on', }) # --- -# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.mock_title_wi_fi_wifi2-entry] +# name: test_switch_setup[fc_data1][switch.mock_title_wi_fi_wifi2-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -220,7 +220,7 @@ 'unit_of_measurement': None, }) # --- -# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.mock_title_wi_fi_wifi2-state] +# name: test_switch_setup[fc_data1][switch.mock_title_wi_fi_wifi2-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Mock Title Wi-Fi WiFi2', @@ -234,7 +234,7 @@ 'state': 'on', }) # --- -# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.printer_internet_access-entry] +# name: test_switch_setup[fc_data1][switch.printer_internet_access-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -267,7 +267,7 @@ 'unit_of_measurement': None, }) # --- -# name: test_switch_setup[fc_data1-expected_wifi_names1][switch.printer_internet_access-state] +# name: test_switch_setup[fc_data1][switch.printer_internet_access-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'printer Internet Access', @@ -281,7 +281,7 @@ 'state': 'on', }) # --- -# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.mock_title_wi_fi_wifi_2_4ghz-entry] +# name: test_switch_setup[fc_data2][switch.mock_title_wi_fi_wifi_2_4ghz-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -314,7 +314,7 @@ 'unit_of_measurement': None, }) # --- -# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.mock_title_wi_fi_wifi_2_4ghz-state] +# name: test_switch_setup[fc_data2][switch.mock_title_wi_fi_wifi_2_4ghz-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Mock Title Wi-Fi WiFi (2.4Ghz)', @@ -328,7 +328,7 @@ 'state': 'on', }) # --- -# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.mock_title_wi_fi_wifi_5ghz-entry] +# name: test_switch_setup[fc_data2][switch.mock_title_wi_fi_wifi_5ghz-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -361,7 +361,7 @@ 'unit_of_measurement': None, }) # --- -# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.mock_title_wi_fi_wifi_5ghz-state] +# name: test_switch_setup[fc_data2][switch.mock_title_wi_fi_wifi_5ghz-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'Mock Title Wi-Fi WiFi+ (5Ghz)', @@ -375,7 +375,7 @@ 'state': 'on', }) # --- -# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.printer_internet_access-entry] +# name: test_switch_setup[fc_data2][switch.printer_internet_access-entry] EntityRegistryEntrySnapshot({ 'aliases': set({ }), @@ -408,7 +408,154 @@ 'unit_of_measurement': None, }) # --- -# name: test_switch_setup[fc_data2-expected_wifi_names2][switch.printer_internet_access-state] +# name: test_switch_setup[fc_data2][switch.printer_internet_access-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'printer Internet Access', + 'icon': 'mdi:router-wireless-settings', + }), + 'context': , + 'entity_id': 'switch.printer_internet_access', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_setup[fc_data3][switch.mock_title_call_deflection_0-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.mock_title_call_deflection_0', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:phone-forward', + 'original_name': 'Call deflection 0', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-call_deflection_0', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data3][switch.mock_title_call_deflection_0-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'deflection_to_number': '+1234657890', + 'friendly_name': 'Mock Title Call deflection 0', + 'icon': 'mdi:phone-forward', + 'mode': 'Immediately', + 'number': None, + 'outgoing': None, + 'phonebook_id': None, + 'type': 'fromAll', + }), + 'context': , + 'entity_id': 'switch.mock_title_call_deflection_0', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'off', + }) +# --- +# name: test_switch_setup[fc_data3][switch.mock_title_wi_fi_mywifi-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.mock_title_wi_fi_mywifi', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:wifi', + 'original_name': 'Mock Title Wi-Fi MyWifi', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': '1C:ED:6F:12:34:11-wi_fi_mywifi', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data3][switch.mock_title_wi_fi_mywifi-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Mock Title Wi-Fi MyWifi', + 'icon': 'mdi:wifi', + }), + 'context': , + 'entity_id': 'switch.mock_title_wi_fi_mywifi', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_setup[fc_data3][switch.printer_internet_access-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.printer_internet_access', + 'has_entity_name': False, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': 'mdi:router-wireless-settings', + 'original_name': 'printer Internet Access', + 'platform': 'fritz', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': None, + 'unique_id': 'AA:BB:CC:00:11:22_internet_access', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_setup[fc_data3][switch.printer_internet_access-state] StateSnapshot({ 'attributes': ReadOnlyDict({ 'friendly_name': 'printer Internet Access', diff --git a/tests/components/fritz/test_switch.py b/tests/components/fritz/test_switch.py index 1542645758e..fdf76d54588 100644 --- a/tests/components/fritz/test_switch.py +++ b/tests/components/fritz/test_switch.py @@ -12,7 +12,7 @@ from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.helpers import entity_registry as er -from .const import MOCK_FB_SERVICES, MOCK_USER_DATA +from .const import MOCK_CALL_DEFLECTION_DATA, MOCK_FB_SERVICES, MOCK_USER_DATA from tests.common import MockConfigEntry, snapshot_platform @@ -169,24 +169,18 @@ MOCK_WLANCONFIGS_DIFF2_SSID: dict[str, dict] = { @pytest.mark.parametrize( - ("fc_data", "expected_wifi_names"), + ("fc_data"), [ - ( - {**MOCK_FB_SERVICES, **MOCK_WLANCONFIGS_SAME_SSID}, - ["WiFi (2.4Ghz)", "WiFi (5Ghz)"], - ), - ({**MOCK_FB_SERVICES, **MOCK_WLANCONFIGS_DIFF_SSID}, ["WiFi", "WiFi2"]), - ( - {**MOCK_FB_SERVICES, **MOCK_WLANCONFIGS_DIFF2_SSID}, - ["WiFi (2.4Ghz)", "WiFi+ (5Ghz)"], - ), + ({**MOCK_FB_SERVICES, **MOCK_WLANCONFIGS_SAME_SSID}), + ({**MOCK_FB_SERVICES, **MOCK_WLANCONFIGS_DIFF_SSID}), + ({**MOCK_FB_SERVICES, **MOCK_WLANCONFIGS_DIFF2_SSID}), + ({**MOCK_FB_SERVICES, **MOCK_CALL_DEFLECTION_DATA}), ], ) @pytest.mark.usefixtures("entity_registry_enabled_by_default") async def test_switch_setup( hass: HomeAssistant, entity_registry: er.EntityRegistry, - expected_wifi_names: list[str], fc_class_mock, fh_class_mock, snapshot: SnapshotAssertion, @@ -199,7 +193,4 @@ async def test_switch_setup( assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done(wait_background_tasks=True) - states = hass.states.async_all() - assert len(states) == 3 - await snapshot_platform(hass, entity_registry, snapshot, entry.entry_id)