From dfd2501c2c135e4aa5558518bf51ad633253dfb5 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 20 Oct 2021 11:43:59 +0200 Subject: [PATCH] Cleanup duplicate code in Onewire tests (#58082) Co-authored-by: epenet --- tests/components/onewire/__init__.py | 47 ++++++++++- tests/components/onewire/const.py | 80 ++----------------- .../components/onewire/test_binary_sensor.py | 32 ++------ tests/components/onewire/test_sensor.py | 42 +++------- tests/components/onewire/test_switch.py | 35 +++----- 5 files changed, 80 insertions(+), 156 deletions(-) diff --git a/tests/components/onewire/__init__.py b/tests/components/onewire/__init__.py index 639379e7acc..8223b1bc841 100644 --- a/tests/components/onewire/__init__.py +++ b/tests/components/onewire/__init__.py @@ -1,14 +1,59 @@ """Tests for 1-Wire integration.""" from __future__ import annotations +from types import MappingProxyType from typing import Any from unittest.mock import MagicMock from pyownet.protocol import ProtocolError from homeassistant.components.onewire.const import DEFAULT_SYSBUS_MOUNT_DIR +from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_registry import EntityRegistry -from .const import ATTR_INJECT_READS, MOCK_OWPROXY_DEVICES, MOCK_SYSBUS_DEVICES +from .const import ( + ATTR_DEFAULT_DISABLED, + ATTR_DEVICE_FILE, + ATTR_INJECT_READS, + ATTR_UNIQUE_ID, + FIXED_ATTRIBUTES, + MOCK_OWPROXY_DEVICES, + MOCK_SYSBUS_DEVICES, +) + + +def check_and_enable_disabled_entities( + entity_registry: EntityRegistry, expected_entities: MappingProxyType +) -> None: + """Ensure that the expected_entities are correctly disabled.""" + for expected_entity in expected_entities: + if expected_entity.get(ATTR_DEFAULT_DISABLED): + entity_id = expected_entity[ATTR_ENTITY_ID] + registry_entry = entity_registry.entities.get(entity_id) + assert registry_entry.disabled + assert registry_entry.disabled_by == "integration" + entity_registry.async_update_entity(entity_id, **{"disabled_by": None}) + + +def check_entities( + hass: HomeAssistant, + entity_registry: EntityRegistry, + expected_entities: MappingProxyType, +) -> None: + """Ensure that the expected_entities are correct.""" + for expected_entity in expected_entities: + entity_id = expected_entity[ATTR_ENTITY_ID] + registry_entry = entity_registry.entities.get(entity_id) + assert registry_entry is not None + assert registry_entry.unique_id == expected_entity[ATTR_UNIQUE_ID] + state = hass.states.get(entity_id) + assert state.state == expected_entity[ATTR_STATE] + assert state.attributes[ATTR_DEVICE_FILE] == expected_entity.get( + ATTR_DEVICE_FILE, registry_entry.unique_id + ) + for attr in FIXED_ATTRIBUTES: + assert state.attributes.get(attr) == expected_entity.get(attr) def setup_owproxy_mock_devices( diff --git a/tests/components/onewire/const.py b/tests/components/onewire/const.py index c7384d0fc2d..93006ee4f81 100644 --- a/tests/components/onewire/const.py +++ b/tests/components/onewire/const.py @@ -43,6 +43,12 @@ ATTR_DEVICE_INFO = "device_info" ATTR_INJECT_READS = "inject_reads" ATTR_UNIQUE_ID = "unique_id" +FIXED_ATTRIBUTES = ( + ATTR_DEVICE_CLASS, + ATTR_STATE_CLASS, + ATTR_UNIT_OF_MEASUREMENT, +) + MANUFACTURER = "Maxim Integrated" MOCK_OWPROXY_DEVICES = { @@ -65,12 +71,10 @@ MOCK_OWPROXY_DEVICES = { SWITCH_DOMAIN: [ { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.05_111111111111_pio", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/05.111111111111/PIO", - ATTR_UNIT_OF_MEASUREMENT: None, }, ], }, @@ -109,21 +113,17 @@ MOCK_OWPROXY_DEVICES = { BINARY_SENSOR_DOMAIN: [ { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.12_111111111111_sensed_a", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/12.111111111111/sensed.A", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.12_111111111111_sensed_b", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/12.111111111111/sensed.B", - ATTR_UNIT_OF_MEASUREMENT: None, }, ], SENSOR_DOMAIN: [ @@ -151,39 +151,31 @@ MOCK_OWPROXY_DEVICES = { SWITCH_DOMAIN: [ { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.12_111111111111_pio_a", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/12.111111111111/PIO.A", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.12_111111111111_pio_b", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/12.111111111111/PIO.B", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.12_111111111111_latch_a", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/12.111111111111/latch.A", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.12_111111111111_latch_b", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/12.111111111111/latch.B", - ATTR_UNIT_OF_MEASUREMENT: None, }, ], }, @@ -199,7 +191,6 @@ MOCK_OWPROXY_DEVICES = { }, SENSOR_DOMAIN: [ { - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "sensor.1d_111111111111_counter_a", ATTR_INJECT_READS: b" 251123", ATTR_STATE: "251123", @@ -208,7 +199,6 @@ MOCK_OWPROXY_DEVICES = { ATTR_UNIT_OF_MEASUREMENT: "count", }, { - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "sensor.1d_111111111111_counter_b", ATTR_INJECT_READS: b" 248125", ATTR_STATE: "248125", @@ -243,7 +233,6 @@ MOCK_OWPROXY_DEVICES = { }, SENSOR_DOMAIN: [ { - ATTR_DEVICE_CLASS: None, ATTR_DEVICE_FILE: "/1F.111111111111/main/1D.111111111111/counter.A", ATTR_ENTITY_ID: "sensor.1d_111111111111_counter_a", ATTR_INJECT_READS: b" 251123", @@ -253,7 +242,6 @@ MOCK_OWPROXY_DEVICES = { ATTR_UNIT_OF_MEASUREMENT: "count", }, { - ATTR_DEVICE_CLASS: None, ATTR_DEVICE_FILE: "/1F.111111111111/main/1D.111111111111/counter.B", ATTR_ENTITY_ID: "sensor.1d_111111111111_counter_b", ATTR_INJECT_READS: b" 248125", @@ -446,221 +434,173 @@ MOCK_OWPROXY_DEVICES = { BINARY_SENSOR_DOMAIN: [ { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.29_111111111111_sensed_0", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/29.111111111111/sensed.0", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.29_111111111111_sensed_1", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/sensed.1", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.29_111111111111_sensed_2", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/sensed.2", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.29_111111111111_sensed_3", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/sensed.3", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.29_111111111111_sensed_4", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/sensed.4", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.29_111111111111_sensed_5", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/sensed.5", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.29_111111111111_sensed_6", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/sensed.6", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.29_111111111111_sensed_7", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/sensed.7", - ATTR_UNIT_OF_MEASUREMENT: None, }, ], SWITCH_DOMAIN: [ { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_pio_0", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/29.111111111111/PIO.0", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_pio_1", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/PIO.1", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_pio_2", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/29.111111111111/PIO.2", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_pio_3", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/PIO.3", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_pio_4", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, - ATTR_UNIT_OF_MEASUREMENT: None, ATTR_UNIQUE_ID: "/29.111111111111/PIO.4", }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_pio_5", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/PIO.5", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_pio_6", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/29.111111111111/PIO.6", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_pio_7", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/PIO.7", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_latch_0", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/29.111111111111/latch.0", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_latch_1", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/latch.1", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_latch_2", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/29.111111111111/latch.2", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_latch_3", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/latch.3", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_latch_4", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/29.111111111111/latch.4", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_latch_5", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/latch.5", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_latch_6", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/29.111111111111/latch.6", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.29_111111111111_latch_7", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/29.111111111111/latch.7", - ATTR_UNIT_OF_MEASUREMENT: None, }, ], }, @@ -677,41 +617,33 @@ MOCK_OWPROXY_DEVICES = { BINARY_SENSOR_DOMAIN: [ { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.3a_111111111111_sensed_a", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/3A.111111111111/sensed.A", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "binary_sensor.3a_111111111111_sensed_b", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/3A.111111111111/sensed.B", - ATTR_UNIT_OF_MEASUREMENT: None, }, ], SWITCH_DOMAIN: [ { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.3a_111111111111_pio_a", ATTR_INJECT_READS: b" 1", ATTR_STATE: STATE_ON, ATTR_UNIQUE_ID: "/3A.111111111111/PIO.A", - ATTR_UNIT_OF_MEASUREMENT: None, }, { ATTR_DEFAULT_DISABLED: True, - ATTR_DEVICE_CLASS: None, ATTR_ENTITY_ID: "switch.3a_111111111111_pio_b", ATTR_INJECT_READS: b" 0", ATTR_STATE: STATE_OFF, ATTR_UNIQUE_ID: "/3A.111111111111/PIO.B", - ATTR_UNIT_OF_MEASUREMENT: None, }, ], }, diff --git a/tests/components/onewire/test_binary_sensor.py b/tests/components/onewire/test_binary_sensor.py index 5ac45997b41..ff9dd29c2c2 100644 --- a/tests/components/onewire/test_binary_sensor.py +++ b/tests/components/onewire/test_binary_sensor.py @@ -5,16 +5,14 @@ import pytest from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ATTR_ENTITY_ID, ATTR_STATE from homeassistant.core import HomeAssistant -from . import setup_owproxy_mock_devices -from .const import ( - ATTR_DEFAULT_DISABLED, - ATTR_DEVICE_FILE, - ATTR_UNIQUE_ID, - MOCK_OWPROXY_DEVICES, +from . import ( + check_and_enable_disabled_entities, + check_entities, + setup_owproxy_mock_devices, ) +from .const import MOCK_OWPROXY_DEVICES from tests.common import mock_registry @@ -44,26 +42,10 @@ async def test_owserver_binary_sensor( assert len(entity_registry.entities) == len(expected_entities) - # Ensure all entities are enabled - for expected_entity in expected_entities: - if expected_entity.get(ATTR_DEFAULT_DISABLED): - entity_id = expected_entity[ATTR_ENTITY_ID] - registry_entry = entity_registry.entities.get(entity_id) - assert registry_entry.disabled - assert registry_entry.disabled_by == "integration" - entity_registry.async_update_entity(entity_id, **{"disabled_by": None}) + check_and_enable_disabled_entities(entity_registry, expected_entities) setup_owproxy_mock_devices(owproxy, BINARY_SENSOR_DOMAIN, [device_id]) await hass.config_entries.async_reload(config_entry.entry_id) await hass.async_block_till_done() - for expected_entity in expected_entities: - entity_id = expected_entity[ATTR_ENTITY_ID] - registry_entry = entity_registry.entities.get(entity_id) - assert registry_entry is not None - assert registry_entry.unique_id == expected_entity[ATTR_UNIQUE_ID] - state = hass.states.get(entity_id) - assert state.state == expected_entity[ATTR_STATE] - assert state.attributes[ATTR_DEVICE_FILE] == expected_entity.get( - ATTR_DEVICE_FILE, registry_entry.unique_id - ) + check_entities(hass, entity_registry, expected_entities) diff --git a/tests/components/onewire/test_sensor.py b/tests/components/onewire/test_sensor.py index 8b229d1a743..23af72bac41 100644 --- a/tests/components/onewire/test_sensor.py +++ b/tests/components/onewire/test_sensor.py @@ -18,9 +18,13 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant -from . import setup_owproxy_mock_devices, setup_sysbus_mock_devices +from . import ( + check_and_enable_disabled_entities, + check_entities, + setup_owproxy_mock_devices, + setup_sysbus_mock_devices, +) from .const import ( - ATTR_DEFAULT_DISABLED, ATTR_DEVICE_FILE, ATTR_DEVICE_INFO, ATTR_INJECT_READS, @@ -97,7 +101,7 @@ async def test_sensors_on_owserver_coupler( state = hass.states.get(entity_id) assert state.state == expected_entity[ATTR_STATE] for attr in (ATTR_DEVICE_CLASS, ATTR_STATE_CLASS, ATTR_UNIT_OF_MEASUREMENT): - assert state.attributes.get(attr) == expected_entity[attr] + assert state.attributes.get(attr) == expected_entity.get(attr) assert state.attributes[ATTR_DEVICE_FILE] == expected_entity[ATTR_DEVICE_FILE] @@ -120,14 +124,7 @@ async def test_owserver_setup_valid_device( assert len(entity_registry.entities) == len(expected_entities) - # Ensure all entities are enabled - for expected_entity in expected_entities: - if expected_entity.get(ATTR_DEFAULT_DISABLED): - entity_id = expected_entity[ATTR_ENTITY_ID] - registry_entry = entity_registry.entities.get(entity_id) - assert registry_entry.disabled - assert registry_entry.disabled_by == "integration" - entity_registry.async_update_entity(entity_id, **{"disabled_by": None}) + check_and_enable_disabled_entities(entity_registry, expected_entities) setup_owproxy_mock_devices(owproxy, SENSOR_DOMAIN, [device_id]) await hass.config_entries.async_reload(config_entry.entry_id) @@ -143,18 +140,7 @@ async def test_owserver_setup_valid_device( assert registry_entry.name == device_info[ATTR_NAME] assert registry_entry.model == device_info[ATTR_MODEL] - for expected_entity in expected_entities: - entity_id = expected_entity[ATTR_ENTITY_ID] - registry_entry = entity_registry.entities.get(entity_id) - assert registry_entry is not None - assert registry_entry.unique_id == expected_entity[ATTR_UNIQUE_ID] - state = hass.states.get(entity_id) - assert state.state == expected_entity[ATTR_STATE] - for attr in (ATTR_DEVICE_CLASS, ATTR_STATE_CLASS, ATTR_UNIT_OF_MEASUREMENT): - assert state.attributes.get(attr) == expected_entity[attr] - assert state.attributes[ATTR_DEVICE_FILE] == expected_entity.get( - ATTR_DEVICE_FILE, registry_entry.unique_id - ) + check_entities(hass, entity_registry, expected_entities) @pytest.mark.usefixtures("sysbus") @@ -193,12 +179,4 @@ async def test_onewiredirect_setup_valid_device( assert registry_entry.name == device_info[ATTR_NAME] assert registry_entry.model == device_info[ATTR_MODEL] - for expected_entity in expected_entities: - entity_id = expected_entity[ATTR_ENTITY_ID] - registry_entry = entity_registry.entities.get(entity_id) - assert registry_entry is not None - assert registry_entry.unique_id == expected_entity[ATTR_UNIQUE_ID] - state = hass.states.get(entity_id) - assert state.state == expected_entity[ATTR_STATE] - for attr in (ATTR_DEVICE_CLASS, ATTR_STATE_CLASS, ATTR_UNIT_OF_MEASUREMENT): - assert state.attributes.get(attr) == expected_entity[attr] + check_entities(hass, entity_registry, expected_entities) diff --git a/tests/components/onewire/test_switch.py b/tests/components/onewire/test_switch.py index 2f8f96ee638..766a41a5862 100644 --- a/tests/components/onewire/test_switch.py +++ b/tests/components/onewire/test_switch.py @@ -14,13 +14,12 @@ from homeassistant.const import ( ) from homeassistant.core import HomeAssistant -from . import setup_owproxy_mock_devices -from .const import ( - ATTR_DEFAULT_DISABLED, - ATTR_DEVICE_FILE, - ATTR_UNIQUE_ID, - MOCK_OWPROXY_DEVICES, +from . import ( + check_and_enable_disabled_entities, + check_entities, + setup_owproxy_mock_devices, ) +from .const import MOCK_OWPROXY_DEVICES from tests.common import mock_registry @@ -50,31 +49,22 @@ async def test_owserver_switch( assert len(entity_registry.entities) == len(expected_entities) - # Ensure all entities are enabled - for expected_entity in expected_entities: - if expected_entity.get(ATTR_DEFAULT_DISABLED): - entity_id = expected_entity[ATTR_ENTITY_ID] - registry_entry = entity_registry.entities.get(entity_id) - assert registry_entry.disabled - assert registry_entry.disabled_by == "integration" - entity_registry.async_update_entity(entity_id, **{"disabled_by": None}) + check_and_enable_disabled_entities(entity_registry, expected_entities) setup_owproxy_mock_devices(owproxy, SWITCH_DOMAIN, [device_id]) await hass.config_entries.async_reload(config_entry.entry_id) await hass.async_block_till_done() + check_entities(hass, entity_registry, expected_entities) + + # Test TOGGLE service for expected_entity in expected_entities: entity_id = expected_entity[ATTR_ENTITY_ID] - registry_entry = entity_registry.entities.get(entity_id) - assert registry_entry is not None - assert registry_entry.unique_id == expected_entity[ATTR_UNIQUE_ID] - state = hass.states.get(entity_id) - assert state.state == expected_entity[ATTR_STATE] - if state.state == STATE_ON: + if expected_entity[ATTR_STATE] == STATE_ON: owproxy.return_value.read.side_effect = [b" 0"] expected_entity[ATTR_STATE] = STATE_OFF - elif state.state == STATE_OFF: + elif expected_entity[ATTR_STATE] == STATE_OFF: owproxy.return_value.read.side_effect = [b" 1"] expected_entity[ATTR_STATE] = STATE_ON @@ -88,6 +78,3 @@ async def test_owserver_switch( state = hass.states.get(entity_id) assert state.state == expected_entity[ATTR_STATE] - assert state.attributes[ATTR_DEVICE_FILE] == expected_entity.get( - ATTR_DEVICE_FILE, registry_entry.unique_id - )