Cleanup duplicate code in Onewire tests (#58082)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-10-20 11:43:59 +02:00 committed by GitHub
parent 008b784fc5
commit dfd2501c2c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 156 deletions

View file

@ -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(

View file

@ -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,
},
],
},

View file

@ -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)

View file

@ -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)

View file

@ -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
)