Use snapshots in devolo Home Network button tests (#95141)

Use snapshots
pull/97175/head
Guido Schmitz 2023-07-25 00:20:09 +02:00 committed by GitHub
parent cce9d938f6
commit 6717e40114
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 393 additions and 145 deletions

View File

@ -0,0 +1,345 @@
# serializer version: 1
# name: test_button[identify_device_with_a_blinking_led-async_identify_device_start]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Mock Title Identify device with a blinking LED',
'icon': 'mdi:led-on',
}),
'context': <ANY>,
'entity_id': 'button.mock_title_identify_device_with_a_blinking_led',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button[identify_device_with_a_blinking_led-async_identify_device_start].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'button.mock_title_identify_device_with_a_blinking_led',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:led-on',
'original_name': 'Identify device with a blinking LED',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'identify',
'unique_id': '1234567890_identify',
'unit_of_measurement': None,
})
# ---
# name: test_button[identify_device_with_a_blinking_led-plcnet-async_identify_device_start]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Mock Title Identify device with a blinking LED',
'icon': 'mdi:led-on',
}),
'context': <ANY>,
'entity_id': 'button.mock_title_identify_device_with_a_blinking_led',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button[identify_device_with_a_blinking_led-plcnet-async_identify_device_start].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': <EntityCategory.DIAGNOSTIC: 'diagnostic'>,
'entity_id': 'button.mock_title_identify_device_with_a_blinking_led',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:led-on',
'original_name': 'Identify device with a blinking LED',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'identify',
'unique_id': '1234567890_identify',
'unit_of_measurement': None,
})
# ---
# name: test_button[restart_device-async_restart]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'restart',
'friendly_name': 'Mock Title Restart device',
}),
'context': <ANY>,
'entity_id': 'button.mock_title_restart_device',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button[restart_device-async_restart].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'button.mock_title_restart_device',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <ButtonDeviceClass.RESTART: 'restart'>,
'original_icon': None,
'original_name': 'Restart device',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'restart',
'unique_id': '1234567890_restart',
'unit_of_measurement': None,
})
# ---
# name: test_button[restart_device-device-async_restart]
StateSnapshot({
'attributes': ReadOnlyDict({
'device_class': 'restart',
'friendly_name': 'Mock Title Restart device',
}),
'context': <ANY>,
'entity_id': 'button.mock_title_restart_device',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button[restart_device-device-async_restart].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': <EntityCategory.CONFIG: 'config'>,
'entity_id': 'button.mock_title_restart_device',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': <ButtonDeviceClass.RESTART: 'restart'>,
'original_icon': None,
'original_name': 'Restart device',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'restart',
'unique_id': '1234567890_restart',
'unit_of_measurement': None,
})
# ---
# name: test_button[start_plc_pairing-async_pair_device]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Mock Title Start PLC pairing',
'icon': 'mdi:plus-network-outline',
}),
'context': <ANY>,
'entity_id': 'button.mock_title_start_plc_pairing',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button[start_plc_pairing-async_pair_device].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': None,
'entity_id': 'button.mock_title_start_plc_pairing',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:plus-network-outline',
'original_name': 'Start PLC pairing',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'pairing',
'unique_id': '1234567890_pairing',
'unit_of_measurement': None,
})
# ---
# name: test_button[start_plc_pairing-plcnet-async_pair_device]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Mock Title Start PLC pairing',
'icon': 'mdi:plus-network-outline',
}),
'context': <ANY>,
'entity_id': 'button.mock_title_start_plc_pairing',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button[start_plc_pairing-plcnet-async_pair_device].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': None,
'entity_id': 'button.mock_title_start_plc_pairing',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:plus-network-outline',
'original_name': 'Start PLC pairing',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'pairing',
'unique_id': '1234567890_pairing',
'unit_of_measurement': None,
})
# ---
# name: test_button[start_wps-async_start_wps]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Mock Title Start WPS',
'icon': 'mdi:wifi-plus',
}),
'context': <ANY>,
'entity_id': 'button.mock_title_start_wps',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button[start_wps-async_start_wps].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': None,
'entity_id': 'button.mock_title_start_wps',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:wifi-plus',
'original_name': 'Start WPS',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'start_wps',
'unique_id': '1234567890_start_wps',
'unit_of_measurement': None,
})
# ---
# name: test_button[start_wps-device-async_start_wps]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Mock Title Start WPS',
'icon': 'mdi:wifi-plus',
}),
'context': <ANY>,
'entity_id': 'button.mock_title_start_wps',
'last_changed': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button[start_wps-device-async_start_wps].1
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'button',
'entity_category': None,
'entity_id': 'button.mock_title_start_wps',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_icon': 'mdi:wifi-plus',
'original_name': 'Start WPS',
'platform': 'devolo_home_network',
'supported_features': 0,
'translation_key': 'start_wps',
'unique_id': '1234567890_start_wps',
'unit_of_measurement': None,
})
# ---

View File

@ -3,19 +3,18 @@ from unittest.mock import AsyncMock
from devolo_plc_api.exceptions.device import DevicePasswordProtected, DeviceUnavailable
import pytest
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.button import (
DOMAIN as PLATFORM,
SERVICE_PRESS,
ButtonDeviceClass,
)
from homeassistant.components.devolo_home_network.const import DOMAIN
from homeassistant.config_entries import SOURCE_REAUTH
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.entity import EntityCategory
from . import configure_integration
from .mock import MockDevice
@ -40,164 +39,68 @@ async def test_button_setup(hass: HomeAssistant) -> None:
await hass.config_entries.async_unload(entry.entry_id)
@pytest.mark.freeze_time("2023-01-13 12:00:00+00:00")
async def test_identify_device(
hass: HomeAssistant, mock_device: MockDevice, entity_registry: er.EntityRegistry
) -> None:
"""Test start PLC pairing button."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
state_key = f"{PLATFORM}.{device_name}_identify_device_with_a_blinking_led"
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state is not None
assert state.state == STATE_UNKNOWN
assert (
entity_registry.async_get(state_key).entity_category
is EntityCategory.DIAGNOSTIC
)
# Emulate button press
await hass.services.async_call(
PLATFORM,
SERVICE_PRESS,
{ATTR_ENTITY_ID: state_key},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state.state == "2023-01-13T12:00:00+00:00"
assert mock_device.plcnet.async_identify_device_start.call_count == 1
await hass.config_entries.async_unload(entry.entry_id)
@pytest.mark.freeze_time("2023-01-13 12:00:00+00:00")
async def test_start_plc_pairing(hass: HomeAssistant, mock_device: MockDevice) -> None:
"""Test start PLC pairing button."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
state_key = f"{PLATFORM}.{device_name}_start_plc_pairing"
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state is not None
assert state.state == STATE_UNKNOWN
# Emulate button press
await hass.services.async_call(
PLATFORM,
SERVICE_PRESS,
{ATTR_ENTITY_ID: state_key},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state.state == "2023-01-13T12:00:00+00:00"
assert mock_device.plcnet.async_pair_device.call_count == 1
await hass.config_entries.async_unload(entry.entry_id)
@pytest.mark.freeze_time("2023-01-13 12:00:00+00:00")
async def test_restart(
hass: HomeAssistant, mock_device: MockDevice, entity_registry: er.EntityRegistry
) -> None:
"""Test restart button."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
state_key = f"{PLATFORM}.{device_name}_restart_device"
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state is not None
assert state.state == STATE_UNKNOWN
assert state.attributes["device_class"] == ButtonDeviceClass.RESTART
assert entity_registry.async_get(state_key).entity_category is EntityCategory.CONFIG
# Emulate button press
await hass.services.async_call(
PLATFORM,
SERVICE_PRESS,
{ATTR_ENTITY_ID: state_key},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state.state == "2023-01-13T12:00:00+00:00"
assert mock_device.device.async_restart.call_count == 1
await hass.config_entries.async_unload(entry.entry_id)
@pytest.mark.freeze_time("2023-01-13 12:00:00+00:00")
async def test_start_wps(hass: HomeAssistant, mock_device: MockDevice) -> None:
"""Test start WPS button."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
state_key = f"{PLATFORM}.{device_name}_start_wps"
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state is not None
assert state.state == STATE_UNKNOWN
# Emulate button press
await hass.services.async_call(
PLATFORM,
SERVICE_PRESS,
{ATTR_ENTITY_ID: state_key},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state.state == "2023-01-13T12:00:00+00:00"
assert mock_device.device.async_start_wps.call_count == 1
await hass.config_entries.async_unload(entry.entry_id)
@pytest.mark.parametrize(
("name", "trigger_method"),
("name", "api_name", "trigger_method"),
[
["identify_device_with_a_blinking_led", "async_identify_device_start"],
["start_plc_pairing", "async_pair_device"],
["restart_device", "async_restart"],
["start_wps", "async_start_wps"],
[
"identify_device_with_a_blinking_led",
"plcnet",
"async_identify_device_start",
],
[
"start_plc_pairing",
"plcnet",
"async_pair_device",
],
[
"restart_device",
"device",
"async_restart",
],
[
"start_wps",
"device",
"async_start_wps",
],
],
)
async def test_device_failure(
@pytest.mark.freeze_time("2023-01-13 12:00:00+00:00")
async def test_button(
hass: HomeAssistant,
mock_device: MockDevice,
entity_registry: er.EntityRegistry,
snapshot: SnapshotAssertion,
name: str,
api_name: str,
trigger_method: str,
) -> None:
"""Test device failure."""
"""Test a button."""
entry = configure_integration(hass)
device_name = entry.title.replace(" ", "_").lower()
state_key = f"{PLATFORM}.{device_name}_{name}"
setattr(mock_device.device, trigger_method, AsyncMock())
api = getattr(mock_device.device, trigger_method)
api.side_effect = DeviceUnavailable
setattr(mock_device.plcnet, trigger_method, AsyncMock())
api = getattr(mock_device.plcnet, trigger_method)
api.side_effect = DeviceUnavailable
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert hass.states.get(state_key) == snapshot
assert entity_registry.async_get(state_key) == snapshot
# Emulate button press
await hass.services.async_call(
PLATFORM,
SERVICE_PRESS,
{ATTR_ENTITY_ID: state_key},
blocking=True,
)
await hass.async_block_till_done()
state = hass.states.get(state_key)
assert state.state == "2023-01-13T12:00:00+00:00"
api = getattr(mock_device, api_name)
assert getattr(api, trigger_method).call_count == 1
# Emulate device failure
setattr(api, trigger_method, AsyncMock())
getattr(api, trigger_method).side_effect = DeviceUnavailable
with pytest.raises(HomeAssistantError):
await hass.services.async_call(
PLATFORM,