Rework Onewire tests to enable disabled entities (#58014)

Co-authored-by: epenet <epenet@users.noreply.github.com>
pull/58019/head
epenet 2021-10-19 09:10:26 +02:00 committed by GitHub
parent a3cae17d88
commit 29c062fcc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 104 additions and 95 deletions

View File

@ -1,5 +1,4 @@
"""Constants for 1-Wire integration."""
from pi1wire import InvalidCRCException, UnsupportResponseException
from pyownet.protocol import Error as ProtocolError
@ -35,6 +34,8 @@ from homeassistant.const import (
TEMP_CELSIUS,
)
ATTR_DEFAULT_DISABLED = "default_disabled"
MANUFACTURER = "Maxim Integrated"
MOCK_OWPROXY_DEVICES = {
@ -62,7 +63,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
],
},
@ -106,7 +107,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "binary_sensor.12_111111111111_sensed_b",
@ -115,7 +116,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
],
SENSOR_DOMAIN: [
@ -126,7 +127,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "25.1",
ATTR_UNIT_OF_MEASUREMENT: TEMP_CELSIUS,
ATTR_DEVICE_CLASS: DEVICE_CLASS_TEMPERATURE,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
{
@ -136,7 +137,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "1025.1",
ATTR_UNIT_OF_MEASUREMENT: PRESSURE_MBAR,
ATTR_DEVICE_CLASS: DEVICE_CLASS_PRESSURE,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
],
@ -148,7 +149,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.12_111111111111_pio_b",
@ -157,7 +158,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.12_111111111111_latch_a",
@ -166,7 +167,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.12_111111111111_latch_b",
@ -175,7 +176,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
],
},
@ -308,7 +309,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "72.8",
ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE,
ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
{
@ -318,7 +319,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "73.8",
ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE,
ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
{
@ -328,7 +329,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "74.8",
ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE,
ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
{
@ -338,7 +339,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "75.8",
ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE,
ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
{
@ -348,7 +349,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "unknown",
ATTR_UNIT_OF_MEASUREMENT: PERCENTAGE,
ATTR_DEVICE_CLASS: DEVICE_CLASS_HUMIDITY,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
{
@ -358,7 +359,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "969.3",
ATTR_UNIT_OF_MEASUREMENT: PRESSURE_MBAR,
ATTR_DEVICE_CLASS: DEVICE_CLASS_PRESSURE,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
{
@ -368,7 +369,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "65.9",
ATTR_UNIT_OF_MEASUREMENT: LIGHT_LUX,
ATTR_DEVICE_CLASS: DEVICE_CLASS_ILLUMINANCE,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
{
@ -378,7 +379,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "3.0",
ATTR_UNIT_OF_MEASUREMENT: ELECTRIC_POTENTIAL_VOLT,
ATTR_DEVICE_CLASS: DEVICE_CLASS_VOLTAGE,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
{
@ -388,7 +389,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "4.7",
ATTR_UNIT_OF_MEASUREMENT: ELECTRIC_POTENTIAL_VOLT,
ATTR_DEVICE_CLASS: DEVICE_CLASS_VOLTAGE,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
{
@ -398,7 +399,7 @@ MOCK_OWPROXY_DEVICES = {
"result": "1.0",
ATTR_UNIT_OF_MEASUREMENT: ELECTRIC_CURRENT_AMPERE,
ATTR_DEVICE_CLASS: DEVICE_CLASS_CURRENT,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
ATTR_STATE_CLASS: STATE_CLASS_MEASUREMENT,
},
],
@ -443,7 +444,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "binary_sensor.29_111111111111_sensed_1",
@ -452,7 +453,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "binary_sensor.29_111111111111_sensed_2",
@ -461,7 +462,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "binary_sensor.29_111111111111_sensed_3",
@ -470,7 +471,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "binary_sensor.29_111111111111_sensed_4",
@ -479,7 +480,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "binary_sensor.29_111111111111_sensed_5",
@ -488,7 +489,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "binary_sensor.29_111111111111_sensed_6",
@ -497,7 +498,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "binary_sensor.29_111111111111_sensed_7",
@ -506,7 +507,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
],
SWITCH_DOMAIN: [
@ -517,7 +518,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_pio_1",
@ -526,7 +527,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_pio_2",
@ -535,7 +536,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_pio_3",
@ -544,7 +545,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_pio_4",
@ -553,7 +554,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_pio_5",
@ -562,7 +563,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_pio_6",
@ -571,7 +572,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_pio_7",
@ -580,7 +581,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_latch_0",
@ -589,7 +590,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_latch_1",
@ -598,7 +599,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_latch_2",
@ -607,7 +608,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_latch_3",
@ -616,7 +617,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_latch_4",
@ -625,7 +626,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_latch_5",
@ -634,7 +635,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_latch_6",
@ -643,7 +644,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.29_111111111111_latch_7",
@ -652,7 +653,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
],
},
@ -674,7 +675,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "binary_sensor.3a_111111111111_sensed_b",
@ -683,7 +684,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
],
SWITCH_DOMAIN: [
@ -694,7 +695,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_ON,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
{
"entity_id": "switch.3a_111111111111_pio_b",
@ -703,7 +704,7 @@ MOCK_OWPROXY_DEVICES = {
"result": STATE_OFF,
ATTR_UNIT_OF_MEASUREMENT: None,
ATTR_DEVICE_CLASS: None,
"disabled": True,
ATTR_DEFAULT_DISABLED: True,
},
],
},

View File

@ -1,16 +1,14 @@
"""Tests for 1-Wire devices connected on OWServer."""
import copy
from unittest.mock import MagicMock, patch
import pytest
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.onewire.binary_sensor import DEVICE_BINARY_SENSORS
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from . import setup_owproxy_mock_devices
from .const import MOCK_OWPROXY_DEVICES
from .const import ATTR_DEFAULT_DISABLED, MOCK_OWPROXY_DEVICES
from tests.common import mock_registry
@ -31,26 +29,28 @@ async def test_owserver_binary_sensor(
"""
entity_registry = mock_registry(hass)
setup_owproxy_mock_devices(owproxy, BINARY_SENSOR_DOMAIN, [device_id])
mock_device = MOCK_OWPROXY_DEVICES[device_id]
expected_entities = mock_device.get(BINARY_SENSOR_DOMAIN, [])
# Force enable binary sensors
patch_device_binary_sensors = copy.deepcopy(DEVICE_BINARY_SENSORS)
if device_binary_sensor := patch_device_binary_sensors.get(device_id[0:2]):
for item in device_binary_sensor:
item.entity_registry_enabled_default = True
with patch.dict(
"homeassistant.components.onewire.binary_sensor.DEVICE_BINARY_SENSORS",
patch_device_binary_sensors,
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
setup_owproxy_mock_devices(owproxy, BINARY_SENSOR_DOMAIN, [device_id])
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
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["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})
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["entity_id"]
registry_entry = entity_registry.entities.get(entity_id)

View File

@ -18,7 +18,7 @@ from homeassistant.core import HomeAssistant
from homeassistant.setup import async_setup_component
from . import setup_owproxy_mock_devices, setup_sysbus_mock_devices
from .const import MOCK_OWPROXY_DEVICES, MOCK_SYSBUS_DEVICES
from .const import ATTR_DEFAULT_DISABLED, MOCK_OWPROXY_DEVICES, MOCK_SYSBUS_DEVICES
from tests.common import assert_setup_component, mock_device_registry, mock_registry
@ -122,7 +122,6 @@ async def test_sensors_on_owserver_coupler(
registry_entry = entity_registry.entities.get(entity_id)
assert registry_entry is not None
assert registry_entry.unique_id == expected_sensor["unique_id"]
assert registry_entry.disabled == expected_sensor.get("disabled", False)
state = hass.states.get(entity_id)
assert state.state == expected_sensor["result"]
for attr in (ATTR_DEVICE_CLASS, ATTR_STATE_CLASS, ATTR_UNIT_OF_MEASUREMENT):
@ -140,16 +139,28 @@ async def test_owserver_setup_valid_device(
entity_registry = mock_registry(hass)
device_registry = mock_device_registry(hass)
setup_owproxy_mock_devices(owproxy, SENSOR_DOMAIN, [device_id])
mock_device = MOCK_OWPROXY_DEVICES[device_id]
expected_entities = mock_device.get(SENSOR_DOMAIN, [])
setup_owproxy_mock_devices(owproxy, SENSOR_DOMAIN, [device_id])
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
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["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})
setup_owproxy_mock_devices(owproxy, SENSOR_DOMAIN, [device_id])
await hass.config_entries.async_reload(config_entry.entry_id)
await hass.async_block_till_done()
if len(expected_entities) > 0:
device_info = mock_device["device_info"]
assert len(device_registry.devices) == 1
@ -165,17 +176,13 @@ async def test_owserver_setup_valid_device(
registry_entry = entity_registry.entities.get(entity_id)
assert registry_entry is not None
assert registry_entry.unique_id == expected_entity["unique_id"]
assert registry_entry.disabled == expected_entity.get("disabled", False)
state = hass.states.get(entity_id)
if registry_entry.disabled:
assert state is None
else:
assert state.state == expected_entity["result"]
for attr in (ATTR_DEVICE_CLASS, ATTR_STATE_CLASS, ATTR_UNIT_OF_MEASUREMENT):
assert state.attributes.get(attr) == expected_entity[attr]
assert state.attributes["device_file"] == expected_entity.get(
"device_file", registry_entry.unique_id
)
assert state.state == expected_entity["result"]
for attr in (ATTR_DEVICE_CLASS, ATTR_STATE_CLASS, ATTR_UNIT_OF_MEASUREMENT):
assert state.attributes.get(attr) == expected_entity[attr]
assert state.attributes["device_file"] == expected_entity.get(
"device_file", registry_entry.unique_id
)
@pytest.mark.usefixtures("sysbus")

View File

@ -1,17 +1,15 @@
"""Tests for 1-Wire devices connected on OWServer."""
import copy
from unittest.mock import MagicMock, patch
import pytest
from homeassistant.components.onewire.switch import DEVICE_SWITCHES
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_ENTITY_ID, SERVICE_TOGGLE, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from . import setup_owproxy_mock_devices
from .const import MOCK_OWPROXY_DEVICES
from .const import ATTR_DEFAULT_DISABLED, MOCK_OWPROXY_DEVICES
from tests.common import mock_registry
@ -32,25 +30,28 @@ async def test_owserver_switch(
"""
entity_registry = mock_registry(hass)
setup_owproxy_mock_devices(owproxy, SWITCH_DOMAIN, [device_id])
mock_device = MOCK_OWPROXY_DEVICES[device_id]
expected_entities = mock_device.get(SWITCH_DOMAIN, [])
# Force enable switches
patch_device_switches = copy.deepcopy(DEVICE_SWITCHES)
if device_switch := patch_device_switches.get(device_id[0:2]):
for item in device_switch:
item.entity_registry_enabled_default = True
with patch.dict(
"homeassistant.components.onewire.switch.DEVICE_SWITCHES", patch_device_switches
):
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
setup_owproxy_mock_devices(owproxy, SWITCH_DOMAIN, [device_id])
await hass.config_entries.async_setup(config_entry.entry_id)
await hass.async_block_till_done()
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["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})
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()
for expected_entity in expected_entities:
entity_id = expected_entity["entity_id"]
registry_entry = entity_registry.entities.get(entity_id)