From b568dd3060acd195f3f6d728be23a3cd8ba6c5b2 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sat, 22 Apr 2023 17:20:27 +0200 Subject: [PATCH] Refactor SnapshotAssertion is SFR Box (#90775) --- .../sfr_box/snapshots/test_binary_sensor.ambr | 92 ++-- .../sfr_box/snapshots/test_button.ambr | 24 +- .../sfr_box/snapshots/test_sensor.ambr | 470 +++++++++--------- .../components/sfr_box/test_binary_sensor.py | 7 +- tests/components/sfr_box/test_button.py | 7 +- tests/components/sfr_box/test_sensor.py | 20 +- 6 files changed, 304 insertions(+), 316 deletions(-) diff --git a/tests/components/sfr_box/snapshots/test_binary_sensor.ambr b/tests/components/sfr_box/snapshots/test_binary_sensor.ambr index 56619e3434e..b308b5ab3af 100644 --- a/tests/components/sfr_box/snapshots/test_binary_sensor.ambr +++ b/tests/components/sfr_box/snapshots/test_binary_sensor.ambr @@ -88,31 +88,31 @@ }), ]) # --- -# name: test_binary_sensors[adsl][binary_sensor.sfr_box_dsl_status] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'connectivity', - 'friendly_name': 'SFR Box DSL status', +# name: test_binary_sensors[adsl].2 + list([ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': 'SFR Box WAN status', + }), + 'context': , + 'entity_id': 'binary_sensor.sfr_box_wan_status', + 'last_changed': , + 'last_updated': , + 'state': 'on', }), - 'context': , - 'entity_id': 'binary_sensor.sfr_box_dsl_status', - 'last_changed': , - 'last_updated': , - 'state': 'on', - }) -# --- -# name: test_binary_sensors[adsl][binary_sensor.sfr_box_wan_status] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'connectivity', - 'friendly_name': 'SFR Box WAN status', + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': 'SFR Box DSL status', + }), + 'context': , + 'entity_id': 'binary_sensor.sfr_box_dsl_status', + 'last_changed': , + 'last_updated': , + 'state': 'on', }), - 'context': , - 'entity_id': 'binary_sensor.sfr_box_wan_status', - 'last_changed': , - 'last_updated': , - 'state': 'on', - }) + ]) # --- # name: test_binary_sensors[ftth] list([ @@ -203,29 +203,29 @@ }), ]) # --- -# name: test_binary_sensors[ftth][binary_sensor.sfr_box_ftth_status] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'connectivity', - 'friendly_name': 'SFR Box FTTH status', +# name: test_binary_sensors[ftth].2 + list([ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': 'SFR Box WAN status', + }), + 'context': , + 'entity_id': 'binary_sensor.sfr_box_wan_status', + 'last_changed': , + 'last_updated': , + 'state': 'on', }), - 'context': , - 'entity_id': 'binary_sensor.sfr_box_ftth_status', - 'last_changed': , - 'last_updated': , - 'state': 'off', - }) -# --- -# name: test_binary_sensors[ftth][binary_sensor.sfr_box_wan_status] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'connectivity', - 'friendly_name': 'SFR Box WAN status', + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'connectivity', + 'friendly_name': 'SFR Box FTTH status', + }), + 'context': , + 'entity_id': 'binary_sensor.sfr_box_ftth_status', + 'last_changed': , + 'last_updated': , + 'state': 'off', }), - 'context': , - 'entity_id': 'binary_sensor.sfr_box_wan_status', - 'last_changed': , - 'last_updated': , - 'state': 'on', - }) + ]) # --- diff --git a/tests/components/sfr_box/snapshots/test_button.ambr b/tests/components/sfr_box/snapshots/test_button.ambr index e2aca4f28d0..dc6ccc1f25d 100644 --- a/tests/components/sfr_box/snapshots/test_button.ambr +++ b/tests/components/sfr_box/snapshots/test_button.ambr @@ -60,16 +60,18 @@ }), ]) # --- -# name: test_buttons[button.sfr_box_restart] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'restart', - 'friendly_name': 'SFR Box Restart', +# name: test_buttons.2 + list([ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'restart', + 'friendly_name': 'SFR Box Restart', + }), + 'context': , + 'entity_id': 'button.sfr_box_restart', + 'last_changed': , + 'last_updated': , + 'state': 'unknown', }), - 'context': , - 'entity_id': 'button.sfr_box_restart', - 'last_changed': , - 'last_updated': , - 'state': 'unknown', - }) + ]) # --- diff --git a/tests/components/sfr_box/snapshots/test_sensor.ambr b/tests/components/sfr_box/snapshots/test_sensor.ambr index 287354cbfef..2390ba625eb 100644 --- a/tests/components/sfr_box/snapshots/test_sensor.ambr +++ b/tests/components/sfr_box/snapshots/test_sensor.ambr @@ -1,30 +1,32 @@ # serializer version: 1 # name: test_sensors - DeviceRegistryEntrySnapshot({ - 'area_id': None, - 'config_entries': , - 'configuration_url': 'http://192.168.0.1', - 'connections': set({ + list([ + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'configuration_url': 'http://192.168.0.1', + 'connections': set({ + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'sfr_box', + 'e4:5d:51:00:11:22', + ), + }), + 'is_new': False, + 'manufacturer': None, + 'model': 'NB6VAC-FXC-r0', + 'name': 'SFR Box', + 'name_by_user': None, + 'suggested_area': None, + 'sw_version': 'NB6VAC-MAIN-R4.0.44k', + 'via_device_id': None, }), - 'disabled_by': None, - 'entry_type': None, - 'hw_version': None, - 'id': , - 'identifiers': set({ - tuple( - 'sfr_box', - 'e4:5d:51:00:11:22', - ), - }), - 'is_new': False, - 'manufacturer': None, - 'model': 'NB6VAC-FXC-r0', - 'name': 'SFR Box', - 'name_by_user': None, - 'suggested_area': None, - 'sw_version': 'NB6VAC-MAIN-R4.0.44k', - 'via_device_id': None, - }) + ]) # --- # name: test_sensors.1 list([ @@ -499,242 +501,216 @@ }), ]) # --- -# name: test_sensors[sensor.sfr_box_dsl_attenuation_down] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'signal_strength', - 'friendly_name': 'SFR Box DSL attenuation down', - 'state_class': , - 'unit_of_measurement': 'dB', +# name: test_sensors.2 + list([ + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': 'SFR Box Network infrastructure', + 'options': list([ + 'adsl', + 'ftth', + 'gprs', + 'unknown', + ]), + }), + 'context': , + 'entity_id': 'sensor.sfr_box_network_infrastructure', + 'last_changed': , + 'last_updated': , + 'state': 'adsl', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_attenuation_down', - 'last_changed': , - 'last_updated': , - 'state': '28.5', - }) -# --- -# name: test_sensors[sensor.sfr_box_dsl_attenuation_up] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'signal_strength', - 'friendly_name': 'SFR Box DSL attenuation up', - 'state_class': , - 'unit_of_measurement': 'dB', + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'voltage', + 'friendly_name': 'SFR Box Voltage', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.sfr_box_voltage', + 'last_changed': , + 'last_updated': , + 'state': '12251', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_attenuation_up', - 'last_changed': , - 'last_updated': , - 'state': '20.8', - }) -# --- -# name: test_sensors[sensor.sfr_box_dsl_counter] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'friendly_name': 'SFR Box DSL counter', + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'temperature', + 'friendly_name': 'SFR Box Temperature', + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.sfr_box_temperature', + 'last_changed': , + 'last_updated': , + 'state': '27.56', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_counter', - 'last_changed': , - 'last_updated': , - 'state': '16', - }) -# --- -# name: test_sensors[sensor.sfr_box_dsl_crc] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'friendly_name': 'SFR Box DSL CRC', + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': 'SFR Box WAN mode', + 'options': list([ + 'adsl_ppp', + 'adsl_routed', + 'ftth_routed', + 'grps_ppp', + 'unknown', + ]), + }), + 'context': , + 'entity_id': 'sensor.sfr_box_wan_mode', + 'last_changed': , + 'last_updated': , + 'state': 'adsl_routed', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_crc', - 'last_changed': , - 'last_updated': , - 'state': '0', - }) -# --- -# name: test_sensors[sensor.sfr_box_dsl_line_mode] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'friendly_name': 'SFR Box DSL line mode', + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SFR Box DSL line mode', + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_line_mode', + 'last_changed': , + 'last_updated': , + 'state': 'ADSL2+', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_line_mode', - 'last_changed': , - 'last_updated': , - 'state': 'ADSL2+', - }) -# --- -# name: test_sensors[sensor.sfr_box_dsl_line_status] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'enum', - 'friendly_name': 'SFR Box DSL line status', - 'options': list([ - 'no_defect', - 'of_frame', - 'loss_of_signal', - 'loss_of_power', - 'loss_of_signal_quality', - 'unknown', - ]), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SFR Box DSL counter', + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_counter', + 'last_changed': , + 'last_updated': , + 'state': '16', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_line_status', - 'last_changed': , - 'last_updated': , - 'state': 'no_defect', - }) -# --- -# name: test_sensors[sensor.sfr_box_dsl_noise_down] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'signal_strength', - 'friendly_name': 'SFR Box DSL noise down', - 'state_class': , - 'unit_of_measurement': 'dB', + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'SFR Box DSL CRC', + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_crc', + 'last_changed': , + 'last_updated': , + 'state': '0', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_noise_down', - 'last_changed': , - 'last_updated': , - 'state': '5.8', - }) -# --- -# name: test_sensors[sensor.sfr_box_dsl_noise_up] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'signal_strength', - 'friendly_name': 'SFR Box DSL noise up', - 'state_class': , - 'unit_of_measurement': 'dB', + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'signal_strength', + 'friendly_name': 'SFR Box DSL noise down', + 'state_class': , + 'unit_of_measurement': 'dB', + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_noise_down', + 'last_changed': , + 'last_updated': , + 'state': '5.8', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_noise_up', - 'last_changed': , - 'last_updated': , - 'state': '6.0', - }) -# --- -# name: test_sensors[sensor.sfr_box_dsl_rate_down] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'data_rate', - 'friendly_name': 'SFR Box DSL rate down', - 'state_class': , - 'unit_of_measurement': , + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'signal_strength', + 'friendly_name': 'SFR Box DSL noise up', + 'state_class': , + 'unit_of_measurement': 'dB', + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_noise_up', + 'last_changed': , + 'last_updated': , + 'state': '6.0', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_rate_down', - 'last_changed': , - 'last_updated': , - 'state': '5549', - }) -# --- -# name: test_sensors[sensor.sfr_box_dsl_rate_up] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'data_rate', - 'friendly_name': 'SFR Box DSL rate up', - 'state_class': , - 'unit_of_measurement': , + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'signal_strength', + 'friendly_name': 'SFR Box DSL attenuation down', + 'state_class': , + 'unit_of_measurement': 'dB', + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_attenuation_down', + 'last_changed': , + 'last_updated': , + 'state': '28.5', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_rate_up', - 'last_changed': , - 'last_updated': , - 'state': '187', - }) -# --- -# name: test_sensors[sensor.sfr_box_dsl_training] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'enum', - 'friendly_name': 'SFR Box DSL training', - 'options': list([ - 'idle', - 'g_994_training', - 'g_992_started', - 'g_922_channel_analysis', - 'g_992_message_exchange', - 'g_993_started', - 'g_993_channel_analysis', - 'g_993_message_exchange', - 'showtime', - 'unknown', - ]), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'signal_strength', + 'friendly_name': 'SFR Box DSL attenuation up', + 'state_class': , + 'unit_of_measurement': 'dB', + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_attenuation_up', + 'last_changed': , + 'last_updated': , + 'state': '20.8', }), - 'context': , - 'entity_id': 'sensor.sfr_box_dsl_training', - 'last_changed': , - 'last_updated': , - 'state': 'showtime', - }) -# --- -# name: test_sensors[sensor.sfr_box_network_infrastructure] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'enum', - 'friendly_name': 'SFR Box Network infrastructure', - 'options': list([ - 'adsl', - 'ftth', - 'gprs', - 'unknown', - ]), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_rate', + 'friendly_name': 'SFR Box DSL rate down', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_rate_down', + 'last_changed': , + 'last_updated': , + 'state': '5549', }), - 'context': , - 'entity_id': 'sensor.sfr_box_network_infrastructure', - 'last_changed': , - 'last_updated': , - 'state': 'adsl', - }) -# --- -# name: test_sensors[sensor.sfr_box_temperature] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'temperature', - 'friendly_name': 'SFR Box Temperature', - 'unit_of_measurement': , + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'data_rate', + 'friendly_name': 'SFR Box DSL rate up', + 'state_class': , + 'unit_of_measurement': , + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_rate_up', + 'last_changed': , + 'last_updated': , + 'state': '187', }), - 'context': , - 'entity_id': 'sensor.sfr_box_temperature', - 'last_changed': , - 'last_updated': , - 'state': '27.56', - }) -# --- -# name: test_sensors[sensor.sfr_box_voltage] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'voltage', - 'friendly_name': 'SFR Box Voltage', - 'unit_of_measurement': , + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': 'SFR Box DSL line status', + 'options': list([ + 'no_defect', + 'of_frame', + 'loss_of_signal', + 'loss_of_power', + 'loss_of_signal_quality', + 'unknown', + ]), + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_line_status', + 'last_changed': , + 'last_updated': , + 'state': 'no_defect', }), - 'context': , - 'entity_id': 'sensor.sfr_box_voltage', - 'last_changed': , - 'last_updated': , - 'state': '12251', - }) -# --- -# name: test_sensors[sensor.sfr_box_wan_mode] - StateSnapshot({ - 'attributes': ReadOnlyDict({ - 'device_class': 'enum', - 'friendly_name': 'SFR Box WAN mode', - 'options': list([ - 'adsl_ppp', - 'adsl_routed', - 'ftth_routed', - 'grps_ppp', - 'unknown', - ]), + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'device_class': 'enum', + 'friendly_name': 'SFR Box DSL training', + 'options': list([ + 'idle', + 'g_994_training', + 'g_992_started', + 'g_922_channel_analysis', + 'g_992_message_exchange', + 'g_993_started', + 'g_993_channel_analysis', + 'g_993_message_exchange', + 'showtime', + 'unknown', + ]), + }), + 'context': , + 'entity_id': 'sensor.sfr_box_dsl_training', + 'last_changed': , + 'last_updated': , + 'state': 'showtime', }), - 'context': , - 'entity_id': 'sensor.sfr_box_wan_mode', - 'last_changed': , - 'last_updated': , - 'state': 'adsl_routed', - }) + ]) # --- diff --git a/tests/components/sfr_box/test_binary_sensor.py b/tests/components/sfr_box/test_binary_sensor.py index db6124bec3d..65f3c8f8c0e 100644 --- a/tests/components/sfr_box/test_binary_sensor.py +++ b/tests/components/sfr_box/test_binary_sensor.py @@ -38,15 +38,18 @@ async def test_binary_sensors( await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() + # Ensure devices are correctly registered device_entries = dr.async_entries_for_config_entry( device_registry, config_entry.entry_id ) assert device_entries == snapshot + # Ensure entities are correctly registered entity_entries = er.async_entries_for_config_entry( entity_registry, config_entry.entry_id ) assert entity_entries == snapshot - for entity in entity_entries: - assert hass.states.get(entity.entity_id) == snapshot(name=entity.entity_id) + # Ensure entity states are correct + states = [hass.states.get(ent.entity_id) for ent in entity_entries] + assert states == snapshot diff --git a/tests/components/sfr_box/test_button.py b/tests/components/sfr_box/test_button.py index 40a71feb084..5a833056291 100644 --- a/tests/components/sfr_box/test_button.py +++ b/tests/components/sfr_box/test_button.py @@ -36,18 +36,21 @@ async def test_buttons( await hass.config_entries.async_setup(config_entry_with_auth.entry_id) await hass.async_block_till_done() + # Ensure devices are correctly registered device_entries = dr.async_entries_for_config_entry( device_registry, config_entry_with_auth.entry_id ) assert device_entries == snapshot + # Ensure entities are correctly registered entity_entries = er.async_entries_for_config_entry( entity_registry, config_entry_with_auth.entry_id ) assert entity_entries == snapshot - for entity in entity_entries: - assert hass.states.get(entity.entity_id) == snapshot(name=entity.entity_id) + # Ensure entity states are correct + states = [hass.states.get(ent.entity_id) for ent in entity_entries] + assert states == snapshot async def test_reboot(hass: HomeAssistant, config_entry_with_auth: ConfigEntry) -> None: diff --git a/tests/components/sfr_box/test_sensor.py b/tests/components/sfr_box/test_sensor.py index ff84defb832..c374837c5a7 100644 --- a/tests/components/sfr_box/test_sensor.py +++ b/tests/components/sfr_box/test_sensor.py @@ -5,7 +5,6 @@ from unittest.mock import patch import pytest from syrupy.assertion import SnapshotAssertion -from homeassistant.components.sfr_box import DOMAIN from homeassistant.config_entries import ConfigEntry from homeassistant.const import Platform from homeassistant.core import HomeAssistant @@ -32,19 +31,24 @@ async def test_sensors( await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() - device_entry = device_registry.async_get_device({(DOMAIN, "e4:5d:51:00:11:22")}) - assert device_entry == snapshot + # Ensure devices are correctly registered + device_entries = dr.async_entries_for_config_entry( + device_registry, config_entry.entry_id + ) + assert device_entries == snapshot + # Ensure entities are correctly registered entity_entries = er.async_entries_for_config_entry( entity_registry, config_entry.entry_id ) assert entity_entries == snapshot - for entity in entity_entries: - entity_registry.async_update_entity(entity.entity_id, **{"disabled_by": None}) - + # Some entities are disabled, enable them and reload before checking states + for ent in entity_entries: + entity_registry.async_update_entity(ent.entity_id, **{"disabled_by": None}) await hass.config_entries.async_reload(config_entry.entry_id) await hass.async_block_till_done() - for entity in entity_entries: - assert hass.states.get(entity.entity_id) == snapshot(name=entity.entity_id) + # Ensure entity states are correct + states = [hass.states.get(ent.entity_id) for ent in entity_entries] + assert states == snapshot