Clean up unnecessary registry mocks from mqtt tests (#87728)

* Clean up unnecessary registry mocks from mqtt tests

* Fix helper methods
This commit is contained in:
Franck Nijhof 2023-02-09 08:19:06 +01:00 committed by GitHub
parent 80bf632e2d
commit 76bf6f164f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 182 additions and 238 deletions

View file

@ -29,7 +29,7 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry, async_fire_mqtt_message, mock_registry from tests.common import MockConfigEntry, async_fire_mqtt_message
from tests.typing import MqttMockHAClientGenerator from tests.typing import MqttMockHAClientGenerator
DEFAULT_CONFIG_DEVICE_INFO_ID = { DEFAULT_CONFIG_DEVICE_INFO_ID = {
@ -1144,7 +1144,7 @@ async def help_test_entity_id_update_subscriptions(
config[mqtt.DOMAIN][domain]["state_topic"] = "test-topic" config[mqtt.DOMAIN][domain]["state_topic"] = "test-topic"
topics = ["avty-topic", "test-topic"] topics = ["avty-topic", "test-topic"]
assert len(topics) > 0 assert len(topics) > 0
registry = mock_registry(hass, {}) entity_registry = er.async_get(hass)
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -1161,7 +1161,9 @@ async def help_test_entity_id_update_subscriptions(
mqtt_mock.async_subscribe.assert_any_call(topic, ANY, ANY, ANY) mqtt_mock.async_subscribe.assert_any_call(topic, ANY, ANY, ANY)
mqtt_mock.async_subscribe.reset_mock() mqtt_mock.async_subscribe.reset_mock()
registry.async_update_entity(f"{domain}.test", new_entity_id=f"{domain}.milk") entity_registry.async_update_entity(
f"{domain}.test", new_entity_id=f"{domain}.milk"
)
await hass.async_block_till_done() await hass.async_block_till_done()
state = hass.states.get(f"{domain}.test") state = hass.states.get(f"{domain}.test")
@ -1191,8 +1193,7 @@ async def help_test_entity_id_update_discovery_update(
config[mqtt.DOMAIN][domain]["availability_topic"] = "avty-topic" config[mqtt.DOMAIN][domain]["availability_topic"] = "avty-topic"
topic = "avty-topic" topic = "avty-topic"
ent_registry = mock_registry(hass, {}) entity_registry = er.async_get(hass)
data = json.dumps(config[mqtt.DOMAIN][domain]) data = json.dumps(config[mqtt.DOMAIN][domain])
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1205,7 +1206,9 @@ async def help_test_entity_id_update_discovery_update(
state = hass.states.get(f"{domain}.test") state = hass.states.get(f"{domain}.test")
assert state and state.state == STATE_UNAVAILABLE assert state and state.state == STATE_UNAVAILABLE
ent_registry.async_update_entity(f"{domain}.test", new_entity_id=f"{domain}.milk") entity_registry.async_update_entity(
f"{domain}.test", new_entity_id=f"{domain}.milk"
)
await hass.async_block_till_done() await hass.async_block_till_done()
config[mqtt.DOMAIN][domain]["availability_topic"] = f"{topic}_2" config[mqtt.DOMAIN][domain]["availability_topic"] = f"{topic}_2"
@ -1497,14 +1500,13 @@ async def help_test_entity_debug_info_update_entity_id(
config["unique_id"] = "veryunique" config["unique_id"] = "veryunique"
config["platform"] = "mqtt" config["platform"] = "mqtt"
dev_registry = dr.async_get(hass) device_registry = dr.async_get(hass)
ent_registry = mock_registry(hass, {}) entity_registry = er.async_get(hass)
data = json.dumps(config) data = json.dumps(config)
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data) async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data)
await hass.async_block_till_done() await hass.async_block_till_done()
device = dev_registry.async_get_device({("mqtt", "helloworld")}) device = device_registry.async_get_device({("mqtt", "helloworld")})
assert device is not None assert device is not None
debug_info_data = debug_info.info_for_device(hass, device.id) debug_info_data = debug_info.info_for_device(hass, device.id)
@ -1521,7 +1523,9 @@ async def help_test_entity_debug_info_update_entity_id(
] ]
assert len(debug_info_data["triggers"]) == 0 assert len(debug_info_data["triggers"]) == 0
ent_registry.async_update_entity(f"{domain}.test", new_entity_id=f"{domain}.milk") entity_registry.async_update_entity(
f"{domain}.test", new_entity_id=f"{domain}.milk"
)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done() await hass.async_block_till_done()

View file

@ -15,7 +15,7 @@ from .test_common import (
help_test_setup_manual_entity_from_yaml, help_test_setup_manual_entity_from_yaml,
) )
from tests.common import async_fire_mqtt_message, mock_device_registry, mock_registry from tests.common import async_fire_mqtt_message
DEFAULT_CONFIG = { DEFAULT_CONFIG = {
mqtt.DOMAIN: { mqtt.DOMAIN: {
@ -34,18 +34,6 @@ def device_tracker_platform_only():
yield yield
@pytest.fixture
def device_reg(hass: HomeAssistant):
"""Return an empty, loaded, registry."""
return mock_device_registry(hass)
@pytest.fixture
def entity_reg(hass: HomeAssistant):
"""Return an empty, loaded, registry."""
return mock_registry(hass)
async def test_discover_device_tracker( async def test_discover_device_tracker(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
) -> None: ) -> None:
@ -222,8 +210,8 @@ async def test_device_tracker_discovery_update(
async def test_cleanup_device_tracker( async def test_cleanup_device_tracker(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client, hass_ws_client,
device_reg, device_registry,
entity_reg, entity_registry,
mqtt_mock_entry_no_yaml_config, mqtt_mock_entry_no_yaml_config,
) -> None: ) -> None:
"""Test discovered device is cleaned up when removed from registry.""" """Test discovered device is cleaned up when removed from registry."""
@ -242,9 +230,9 @@ async def test_cleanup_device_tracker(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device and registry entries are created # Verify device and registry entries are created
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is not None assert device_entry is not None
entity_entry = entity_reg.async_get("device_tracker.mqtt_unique") entity_entry = entity_registry.async_get("device_tracker.mqtt_unique")
assert entity_entry is not None assert entity_entry is not None
state = hass.states.get("device_tracker.mqtt_unique") state = hass.states.get("device_tracker.mqtt_unique")
@ -266,9 +254,9 @@ async def test_cleanup_device_tracker(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device and registry entries are cleared # Verify device and registry entries are cleared
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is None assert device_entry is None
entity_entry = entity_reg.async_get("device_tracker.mqtt_unique") entity_entry = entity_registry.async_get("device_tracker.mqtt_unique")
assert entity_entry is None assert entity_entry is None
# Verify state is removed # Verify state is removed

View file

@ -21,24 +21,10 @@ from tests.common import (
async_fire_mqtt_message, async_fire_mqtt_message,
async_get_device_automations, async_get_device_automations,
async_mock_service, async_mock_service,
mock_device_registry,
mock_registry,
) )
from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401 from tests.components.blueprint.conftest import stub_blueprint_populate # noqa: F401
@pytest.fixture
def device_reg(hass):
"""Return an empty, loaded, registry."""
return mock_device_registry(hass)
@pytest.fixture
def entity_reg(hass):
"""Return an empty, loaded, registry."""
return mock_registry(hass)
@pytest.fixture @pytest.fixture
def calls(hass): def calls(hass):
"""Track calls to a mock service.""" """Track calls to a mock service."""
@ -56,7 +42,7 @@ def binary_sensor_and_sensor_only():
async def test_get_triggers( async def test_get_triggers(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test we get the expected triggers from a discovered mqtt device.""" """Test we get the expected triggers from a discovered mqtt device."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -71,7 +57,7 @@ async def test_get_triggers(
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
expected_triggers = [ expected_triggers = [
{ {
"platform": "device", "platform": "device",
@ -90,7 +76,7 @@ async def test_get_triggers(
async def test_get_unknown_triggers( async def test_get_unknown_triggers(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test we don't get unknown triggers.""" """Test we don't get unknown triggers."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -103,7 +89,7 @@ async def test_get_unknown_triggers(
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data1) async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -135,7 +121,7 @@ async def test_get_unknown_triggers(
async def test_get_non_existing_triggers( async def test_get_non_existing_triggers(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test getting non existing triggers.""" """Test getting non existing triggers."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -148,7 +134,7 @@ async def test_get_non_existing_triggers(
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data1) async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
hass, DeviceAutomationType.TRIGGER, device_entry.id hass, DeviceAutomationType.TRIGGER, device_entry.id
) )
@ -157,7 +143,7 @@ async def test_get_non_existing_triggers(
@pytest.mark.no_fail_on_log_exception @pytest.mark.no_fail_on_log_exception
async def test_discover_bad_triggers( async def test_discover_bad_triggers(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test bad discovery message.""" """Test bad discovery message."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -172,7 +158,7 @@ async def test_discover_bad_triggers(
) )
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data0) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data0)
await hass.async_block_till_done() await hass.async_block_till_done()
assert device_reg.async_get_device({("mqtt", "0AFFD2")}) is None assert device_registry.async_get_device({("mqtt", "0AFFD2")}) is None
# Test sending correct data # Test sending correct data
data1 = ( data1 = (
@ -186,7 +172,7 @@ async def test_discover_bad_triggers(
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
expected_triggers = [ expected_triggers = [
{ {
"platform": "device", "platform": "device",
@ -205,7 +191,7 @@ async def test_discover_bad_triggers(
async def test_update_remove_triggers( async def test_update_remove_triggers(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test triggers can be updated and removed.""" """Test triggers can be updated and removed."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -234,7 +220,7 @@ async def test_update_remove_triggers(
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
expected_triggers1 = [ expected_triggers1 = [
{ {
"platform": "device", "platform": "device",
@ -267,12 +253,12 @@ async def test_update_remove_triggers(
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", "") async_fire_mqtt_message(hass, "homeassistant/device_automation/bla/config", "")
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is None assert device_entry is None
async def test_if_fires_on_mqtt_message( async def test_if_fires_on_mqtt_message(
hass: HomeAssistant, device_reg, calls, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, calls, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test triggers firing.""" """Test triggers firing."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -295,7 +281,7 @@ async def test_if_fires_on_mqtt_message(
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", data2) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", data2)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -348,7 +334,7 @@ async def test_if_fires_on_mqtt_message(
async def test_if_fires_on_mqtt_message_template( async def test_if_fires_on_mqtt_message_template(
hass: HomeAssistant, device_reg, calls, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, calls, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test triggers firing.""" """Test triggers firing."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -373,7 +359,7 @@ async def test_if_fires_on_mqtt_message_template(
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", data2) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", data2)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -426,7 +412,7 @@ async def test_if_fires_on_mqtt_message_template(
async def test_if_fires_on_mqtt_message_late_discover( async def test_if_fires_on_mqtt_message_late_discover(
hass: HomeAssistant, device_reg, calls, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, calls, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test triggers firing of MQTT device triggers discovered after setup.""" """Test triggers firing of MQTT device triggers discovered after setup."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -453,7 +439,7 @@ async def test_if_fires_on_mqtt_message_late_discover(
) )
async_fire_mqtt_message(hass, "homeassistant/sensor/bla0/config", data0) async_fire_mqtt_message(hass, "homeassistant/sensor/bla0/config", data0)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -510,7 +496,7 @@ async def test_if_fires_on_mqtt_message_late_discover(
async def test_if_fires_on_mqtt_message_after_update( async def test_if_fires_on_mqtt_message_after_update(
hass: HomeAssistant, device_reg, calls, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, calls, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test triggers firing after update.""" """Test triggers firing after update."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -530,7 +516,7 @@ async def test_if_fires_on_mqtt_message_after_update(
) )
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -587,7 +573,7 @@ async def test_if_fires_on_mqtt_message_after_update(
async def test_no_resubscribe_same_topic( async def test_no_resubscribe_same_topic(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test subscription to topics without change.""" """Test subscription to topics without change."""
mqtt_mock = await mqtt_mock_entry_no_yaml_config() mqtt_mock = await mqtt_mock_entry_no_yaml_config()
@ -600,7 +586,7 @@ async def test_no_resubscribe_same_topic(
) )
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -632,7 +618,7 @@ async def test_no_resubscribe_same_topic(
async def test_not_fires_on_mqtt_message_after_remove_by_mqtt( async def test_not_fires_on_mqtt_message_after_remove_by_mqtt(
hass: HomeAssistant, device_reg, calls, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, calls, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test triggers not firing after removal.""" """Test triggers not firing after removal."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -645,7 +631,7 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt(
) )
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -696,7 +682,7 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt(
async def test_not_fires_on_mqtt_message_after_remove_from_registry( async def test_not_fires_on_mqtt_message_after_remove_from_registry(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client, hass_ws_client,
device_reg, device_registry,
calls, calls,
mqtt_mock_entry_no_yaml_config, mqtt_mock_entry_no_yaml_config,
) -> None: ) -> None:
@ -717,7 +703,7 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry(
) )
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert await async_setup_component( assert await async_setup_component(
hass, hass,
@ -767,7 +753,7 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry(
async def test_attach_remove( async def test_attach_remove(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test attach and removal of trigger.""" """Test attach and removal of trigger."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -781,7 +767,7 @@ async def test_attach_remove(
) )
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
calls = [] calls = []
@ -823,7 +809,7 @@ async def test_attach_remove(
async def test_attach_remove_late( async def test_attach_remove_late(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test attach and removal of trigger .""" """Test attach and removal of trigger ."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -842,7 +828,7 @@ async def test_attach_remove_late(
) )
async_fire_mqtt_message(hass, "homeassistant/sensor/bla0/config", data0) async_fire_mqtt_message(hass, "homeassistant/sensor/bla0/config", data0)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
calls = [] calls = []
@ -887,7 +873,7 @@ async def test_attach_remove_late(
async def test_attach_remove_late2( async def test_attach_remove_late2(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test attach and removal of trigger .""" """Test attach and removal of trigger ."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -906,7 +892,7 @@ async def test_attach_remove_late2(
) )
async_fire_mqtt_message(hass, "homeassistant/sensor/bla0/config", data0) async_fire_mqtt_message(hass, "homeassistant/sensor/bla0/config", data0)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
calls = [] calls = []
@ -1061,8 +1047,7 @@ async def test_entity_device_info_update(
async def test_cleanup_trigger( async def test_cleanup_trigger(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client, hass_ws_client,
device_reg, device_registry,
entity_reg,
mqtt_mock_entry_no_yaml_config, mqtt_mock_entry_no_yaml_config,
) -> None: ) -> None:
"""Test trigger discovery topic is cleaned when device is removed from registry.""" """Test trigger discovery topic is cleaned when device is removed from registry."""
@ -1083,7 +1068,7 @@ async def test_cleanup_trigger(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is created # Verify device registry entry is created
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
@ -1107,7 +1092,7 @@ async def test_cleanup_trigger(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is cleared # Verify device registry entry is cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is None assert device_entry is None
# Verify retained discovery topic has been cleared # Verify retained discovery topic has been cleared
@ -1117,7 +1102,7 @@ async def test_cleanup_trigger(
async def test_cleanup_device( async def test_cleanup_device(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test removal from device registry when trigger is removed.""" """Test removal from device registry when trigger is removed."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -1134,7 +1119,7 @@ async def test_cleanup_device(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is created # Verify device registry entry is created
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
@ -1146,12 +1131,12 @@ async def test_cleanup_device(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is cleared # Verify device registry entry is cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is None assert device_entry is None
async def test_cleanup_device_several_triggers( async def test_cleanup_device_several_triggers(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test removal from device registry when the last trigger is removed.""" """Test removal from device registry when the last trigger is removed."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -1179,7 +1164,7 @@ async def test_cleanup_device_several_triggers(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is created # Verify device registry entry is created
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
@ -1193,7 +1178,7 @@ async def test_cleanup_device_several_triggers(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is not cleared # Verify device registry entry is not cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
@ -1206,12 +1191,12 @@ async def test_cleanup_device_several_triggers(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is cleared # Verify device registry entry is cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is None assert device_entry is None
async def test_cleanup_device_with_entity1( async def test_cleanup_device_with_entity1(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test removal from device registry for device with entity. """Test removal from device registry for device with entity.
@ -1241,7 +1226,7 @@ async def test_cleanup_device_with_entity1(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is created # Verify device registry entry is created
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
@ -1253,7 +1238,7 @@ async def test_cleanup_device_with_entity1(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is not cleared # Verify device registry entry is not cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
@ -1265,12 +1250,12 @@ async def test_cleanup_device_with_entity1(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is cleared # Verify device registry entry is cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is None assert device_entry is None
async def test_cleanup_device_with_entity2( async def test_cleanup_device_with_entity2(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test removal from device registry for device with entity. """Test removal from device registry for device with entity.
@ -1300,7 +1285,7 @@ async def test_cleanup_device_with_entity2(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is created # Verify device registry entry is created
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
@ -1312,7 +1297,7 @@ async def test_cleanup_device_with_entity2(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is not cleared # Verify device registry entry is not cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
@ -1324,7 +1309,7 @@ async def test_cleanup_device_with_entity2(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is cleared # Verify device registry entry is cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is None assert device_entry is None
@ -1406,7 +1391,7 @@ async def test_trigger_debug_info(
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, calls, device_reg, mqtt_mock, tmp_path hass: HomeAssistant, calls, device_registry, mqtt_mock, tmp_path
) -> None: ) -> None:
"""Test unloading the MQTT entry.""" """Test unloading the MQTT entry."""
@ -1419,7 +1404,7 @@ async def test_unload_entry(
) )
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1) async_fire_mqtt_message(hass, "homeassistant/device_automation/bla1/config", data1)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert await async_setup_component( assert await async_setup_component(
hass, hass,

View file

@ -35,9 +35,7 @@ from tests.common import (
MockConfigEntry, MockConfigEntry,
async_capture_events, async_capture_events,
async_fire_mqtt_message, async_fire_mqtt_message,
mock_device_registry,
mock_entity_platform, mock_entity_platform,
mock_registry,
) )
from tests.typing import ( from tests.typing import (
MqttMockHAClientGenerator, MqttMockHAClientGenerator,
@ -46,18 +44,6 @@ from tests.typing import (
) )
@pytest.fixture
def device_reg(hass: HomeAssistant) -> dr.DeviceRegistry:
"""Return an empty, loaded, registry."""
return mock_device_registry(hass)
@pytest.fixture
def entity_reg(hass: HomeAssistant) -> er.EntityRegistry:
"""Return an empty, loaded, registry."""
return mock_registry(hass)
@pytest.mark.parametrize( @pytest.mark.parametrize(
"mqtt_config_entry_data", "mqtt_config_entry_data",
[{mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_DISCOVERY: False}], [{mqtt.CONF_BROKER: "mock-broker", mqtt.CONF_DISCOVERY: False}],
@ -723,8 +709,8 @@ async def test_duplicate_removal(
async def test_cleanup_device( async def test_cleanup_device(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
device_reg: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_reg: er.EntityRegistry, entity_registry: er.EntityRegistry,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test discvered device is cleaned up when entry removed from device.""" """Test discvered device is cleaned up when entry removed from device."""
@ -742,9 +728,9 @@ async def test_cleanup_device(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device and registry entries are created # Verify device and registry entries are created
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is not None assert device_entry is not None
entity_entry = entity_reg.async_get("sensor.mqtt_sensor") entity_entry = entity_registry.async_get("sensor.mqtt_sensor")
assert entity_entry is not None assert entity_entry is not None
state = hass.states.get("sensor.mqtt_sensor") state = hass.states.get("sensor.mqtt_sensor")
@ -766,9 +752,9 @@ async def test_cleanup_device(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device and registry entries are cleared # Verify device and registry entries are cleared
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is None assert device_entry is None
entity_entry = entity_reg.async_get("sensor.mqtt_sensor") entity_entry = entity_registry.async_get("sensor.mqtt_sensor")
assert entity_entry is None assert entity_entry is None
# Verify state is removed # Verify state is removed
@ -785,8 +771,8 @@ async def test_cleanup_device(
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SENSOR]) @patch("homeassistant.components.mqtt.PLATFORMS", [Platform.SENSOR])
async def test_cleanup_device_mqtt( async def test_cleanup_device_mqtt(
hass: HomeAssistant, hass: HomeAssistant,
device_reg: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_reg: er.EntityRegistry, entity_registry: er.EntityRegistry,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test discvered device is cleaned up when removed through MQTT.""" """Test discvered device is cleaned up when removed through MQTT."""
@ -801,9 +787,9 @@ async def test_cleanup_device_mqtt(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device and registry entries are created # Verify device and registry entries are created
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is not None assert device_entry is not None
entity_entry = entity_reg.async_get("sensor.mqtt_sensor") entity_entry = entity_registry.async_get("sensor.mqtt_sensor")
assert entity_entry is not None assert entity_entry is not None
state = hass.states.get("sensor.mqtt_sensor") state = hass.states.get("sensor.mqtt_sensor")
@ -814,9 +800,9 @@ async def test_cleanup_device_mqtt(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device and registry entries are cleared # Verify device and registry entries are cleared
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is None assert device_entry is None
entity_entry = entity_reg.async_get("sensor.mqtt_sensor") entity_entry = entity_registry.async_get("sensor.mqtt_sensor")
assert entity_entry is None assert entity_entry is None
# Verify state is removed # Verify state is removed
@ -832,8 +818,8 @@ async def test_cleanup_device_mqtt(
async def test_cleanup_device_multiple_config_entries( async def test_cleanup_device_multiple_config_entries(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client: WebSocketGenerator, hass_ws_client: WebSocketGenerator,
device_reg: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_reg: er.EntityRegistry, entity_registry: er.EntityRegistry,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test discovered device is cleaned up when entry removed from device.""" """Test discovered device is cleaned up when entry removed from device."""
@ -844,7 +830,7 @@ async def test_cleanup_device_multiple_config_entries(
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
device_entry: dr.DeviceEntry | None = device_reg.async_get_or_create( device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={("mac", "12:34:56:AB:CD:EF")}, connections={("mac", "12:34:56:AB:CD:EF")},
) )
@ -880,13 +866,15 @@ async def test_cleanup_device_multiple_config_entries(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device and registry entries are created # Verify device and registry entries are created
device_entry = device_reg.async_get_device(set(), {("mac", "12:34:56:AB:CD:EF")}) device_entry = device_registry.async_get_device(
set(), {("mac", "12:34:56:AB:CD:EF")}
)
assert device_entry is not None assert device_entry is not None
assert device_entry.config_entries == { assert device_entry.config_entries == {
mqtt_config_entry.entry_id, mqtt_config_entry.entry_id,
config_entry.entry_id, config_entry.entry_id,
} }
entity_entry = entity_reg.async_get("sensor.mqtt_sensor") entity_entry = entity_registry.async_get("sensor.mqtt_sensor")
assert entity_entry is not None assert entity_entry is not None
state = hass.states.get("sensor.mqtt_sensor") state = hass.states.get("sensor.mqtt_sensor")
@ -909,9 +897,11 @@ async def test_cleanup_device_multiple_config_entries(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device is still there but entity is cleared # Verify device is still there but entity is cleared
device_entry = device_reg.async_get_device(set(), {("mac", "12:34:56:AB:CD:EF")}) device_entry = device_registry.async_get_device(
set(), {("mac", "12:34:56:AB:CD:EF")}
)
assert device_entry is not None assert device_entry is not None
entity_entry = entity_reg.async_get("sensor.mqtt_sensor") entity_entry = entity_registry.async_get("sensor.mqtt_sensor")
assert device_entry.config_entries == {config_entry.entry_id} assert device_entry.config_entries == {config_entry.entry_id}
assert entity_entry is None assert entity_entry is None
@ -933,15 +923,15 @@ async def test_cleanup_device_multiple_config_entries(
async def test_cleanup_device_multiple_config_entries_mqtt( async def test_cleanup_device_multiple_config_entries_mqtt(
hass: HomeAssistant, hass: HomeAssistant,
device_reg: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
entity_reg: er.EntityRegistry, entity_registry: er.EntityRegistry,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
) -> None: ) -> None:
"""Test discovered device is cleaned up when removed through MQTT.""" """Test discovered device is cleaned up when removed through MQTT."""
mqtt_mock = await mqtt_mock_entry_no_yaml_config() mqtt_mock = await mqtt_mock_entry_no_yaml_config()
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
device_entry: dr.DeviceEntry | None = device_reg.async_get_or_create( device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={("mac", "12:34:56:AB:CD:EF")}, connections={("mac", "12:34:56:AB:CD:EF")},
) )
@ -976,13 +966,15 @@ async def test_cleanup_device_multiple_config_entries_mqtt(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device and registry entries are created # Verify device and registry entries are created
device_entry = device_reg.async_get_device(set(), {("mac", "12:34:56:AB:CD:EF")}) device_entry = device_registry.async_get_device(
set(), {("mac", "12:34:56:AB:CD:EF")}
)
assert device_entry is not None assert device_entry is not None
assert device_entry.config_entries == { assert device_entry.config_entries == {
mqtt_config_entry.entry_id, mqtt_config_entry.entry_id,
config_entry.entry_id, config_entry.entry_id,
} }
entity_entry = entity_reg.async_get("sensor.mqtt_sensor") entity_entry = entity_registry.async_get("sensor.mqtt_sensor")
assert entity_entry is not None assert entity_entry is not None
state = hass.states.get("sensor.mqtt_sensor") state = hass.states.get("sensor.mqtt_sensor")
@ -997,9 +989,11 @@ async def test_cleanup_device_multiple_config_entries_mqtt(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device is still there but entity is cleared # Verify device is still there but entity is cleared
device_entry = device_reg.async_get_device(set(), {("mac", "12:34:56:AB:CD:EF")}) device_entry = device_registry.async_get_device(
set(), {("mac", "12:34:56:AB:CD:EF")}
)
assert device_entry is not None assert device_entry is not None
entity_entry = entity_reg.async_get("sensor.mqtt_sensor") entity_entry = entity_registry.async_get("sensor.mqtt_sensor")
assert device_entry.config_entries == {config_entry.entry_id} assert device_entry.config_entries == {config_entry.entry_id}
assert entity_entry is None assert entity_entry is None
@ -1467,7 +1461,7 @@ async def test_mqtt_discovery_unsubscribe_once(
async def test_clear_config_topic_disabled_entity( async def test_clear_config_topic_disabled_entity(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
device_reg: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
caplog: pytest.LogCaptureFixture, caplog: pytest.LogCaptureFixture,
) -> None: ) -> None:
"""Test the discovery topic is removed when a disabled entity is removed.""" """Test the discovery topic is removed when a disabled entity is removed."""
@ -1517,11 +1511,13 @@ async def test_clear_config_topic_disabled_entity(
assert hass.states.get("sensor.sbfspot_12345_2") is None # not unique assert hass.states.get("sensor.sbfspot_12345_2") is None # not unique
# Verify device is created # Verify device is created
device_entry = device_reg.async_get_device(set(), {("mac", "12:34:56:AB:CD:EF")}) device_entry = device_registry.async_get_device(
set(), {("mac", "12:34:56:AB:CD:EF")}
)
assert device_entry is not None assert device_entry is not None
# Remove the device from the registry # Remove the device from the registry
device_reg.async_remove_device(device_entry.id) device_registry.async_remove_device(device_entry.id)
await hass.async_block_till_done() await hass.async_block_till_done()
await hass.async_block_till_done() await hass.async_block_till_done()
@ -1541,7 +1537,7 @@ async def test_clear_config_topic_disabled_entity(
async def test_clean_up_registry_monitoring( async def test_clean_up_registry_monitoring(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
device_reg: dr.DeviceRegistry, device_registry: dr.DeviceRegistry,
tmp_path: Path, tmp_path: Path,
) -> None: ) -> None:
"""Test registry monitoring hook is removed after a reload.""" """Test registry monitoring hook is removed after a reload."""
@ -1581,7 +1577,9 @@ async def test_clean_up_registry_monitoring(
assert len(hooks) == 1 assert len(hooks) == 1
# Verify device is created # Verify device is created
device_entry = device_reg.async_get_device(set(), {("mac", "12:34:56:AB:CD:EF")}) device_entry = device_registry.async_get_device(
set(), {("mac", "12:34:56:AB:CD:EF")}
)
assert device_entry is not None assert device_entry is not None
# Enload the entry # Enload the entry
@ -1594,7 +1592,7 @@ async def test_clean_up_registry_monitoring(
async def test_unique_id_collission_has_priority( async def test_unique_id_collission_has_priority(
hass: HomeAssistant, hass: HomeAssistant,
mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator, mqtt_mock_entry_no_yaml_config: MqttMockHAClientGenerator,
entity_reg: er.EntityRegistry, entity_registry: er.EntityRegistry,
) -> None: ) -> None:
"""Test the unique_id collision detection has priority over registry disabled items.""" """Test the unique_id collision detection has priority over registry disabled items."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -1633,9 +1631,9 @@ async def test_unique_id_collission_has_priority(
assert hass.states.get("sensor.sbfspot_12345_2") is None # not unique assert hass.states.get("sensor.sbfspot_12345_2") is None # not unique
# Verify the first entity is created # Verify the first entity is created
assert entity_reg.async_get("sensor.sbfspot_12345_1") is not None assert entity_registry.async_get("sensor.sbfspot_12345_1") is not None
# Verify the second entity is not created because it is not unique # Verify the second entity is not created because it is not unique
assert entity_reg.async_get("sensor.sbfspot_12345_2") is None assert entity_registry.async_get("sensor.sbfspot_12345_2") is None
@pytest.mark.xfail(raises=MultipleInvalid) @pytest.mark.xfail(raises=MultipleInvalid)

View file

@ -43,8 +43,6 @@ from tests.common import (
MockConfigEntry, MockConfigEntry,
async_fire_mqtt_message, async_fire_mqtt_message,
async_fire_time_changed, async_fire_time_changed,
mock_device_registry,
mock_registry,
mock_restore_cache, mock_restore_cache,
) )
from tests.testing_config.custom_components.test.sensor import DEVICE_CLASSES from tests.testing_config.custom_components.test.sensor import DEVICE_CLASSES
@ -71,18 +69,6 @@ def mock_storage(hass_storage):
"""Autouse hass_storage for the TestCase tests.""" """Autouse hass_storage for the TestCase tests."""
@pytest.fixture
def device_reg(hass):
"""Return an empty, loaded, registry."""
return mock_device_registry(hass)
@pytest.fixture
def entity_reg(hass):
"""Return an empty, loaded, registry."""
return mock_registry(hass)
@pytest.fixture @pytest.fixture
def mock_mqtt(): def mock_mqtt():
"""Make sure connection is established.""" """Make sure connection is established."""
@ -1897,7 +1883,7 @@ async def test_mqtt_subscribes_topics_on_connect(
async def test_setup_entry_with_config_override( async def test_setup_entry_with_config_override(
hass, device_reg, mqtt_mock_entry_with_yaml_config hass, device_registry, mqtt_mock_entry_with_yaml_config
): ):
"""Test if the MQTT component loads with no config and config entry can be setup.""" """Test if the MQTT component loads with no config and config entry can be setup."""
data = ( data = (
@ -1920,12 +1906,12 @@ async def test_setup_entry_with_config_override(
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data) async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is not None assert device_entry is not None
async def test_update_incomplete_entry( async def test_update_incomplete_entry(
hass: HomeAssistant, device_reg, mqtt_client_mock, caplog hass: HomeAssistant, device_registry, mqtt_client_mock, caplog
): ):
"""Test if the MQTT component loads when config entry data is incomplete.""" """Test if the MQTT component loads when config entry data is incomplete."""
data = ( data = (
@ -1958,11 +1944,11 @@ async def test_update_incomplete_entry(
async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data) async_fire_mqtt_message(hass, "homeassistant/sensor/bla/config", data)
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is not None assert device_entry is not None
async def test_fail_no_broker(hass, device_reg, mqtt_client_mock, caplog): async def test_fail_no_broker(hass, device_registry, mqtt_client_mock, caplog):
"""Test if the MQTT component loads when broker configuration is missing.""" """Test if the MQTT component loads when broker configuration is missing."""
# Config entry data is incomplete # Config entry data is incomplete
entry = MockConfigEntry(domain=mqtt.DOMAIN, data={}) entry = MockConfigEntry(domain=mqtt.DOMAIN, data={})
@ -2091,7 +2077,7 @@ async def test_dump_service(hass, mqtt_mock_entry_no_yaml_config):
async def test_mqtt_ws_remove_discovered_device( async def test_mqtt_ws_remove_discovered_device(
hass, device_reg, entity_reg, hass_ws_client, mqtt_mock_entry_no_yaml_config hass, device_registry, hass_ws_client, mqtt_mock_entry_no_yaml_config
): ):
"""Test MQTT websocket device removal.""" """Test MQTT websocket device removal."""
assert await async_setup_component(hass, "config", {}) assert await async_setup_component(hass, "config", {})
@ -2108,7 +2094,7 @@ async def test_mqtt_ws_remove_discovered_device(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device entry is created # Verify device entry is created
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is not None assert device_entry is not None
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
@ -2125,12 +2111,12 @@ async def test_mqtt_ws_remove_discovered_device(
assert response["success"] assert response["success"]
# Verify device entry is cleared # Verify device entry is cleared
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is None assert device_entry is None
async def test_mqtt_ws_get_device_debug_info( async def test_mqtt_ws_get_device_debug_info(
hass, device_reg, hass_ws_client, mqtt_mock_entry_no_yaml_config hass, device_registry, hass_ws_client, mqtt_mock_entry_no_yaml_config
): ):
"""Test MQTT websocket device debug info.""" """Test MQTT websocket device debug info."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -2157,7 +2143,7 @@ async def test_mqtt_ws_get_device_debug_info(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device entry is created # Verify device entry is created
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is not None assert device_entry is not None
client = await hass_ws_client(hass) client = await hass_ws_client(hass)
@ -2193,7 +2179,7 @@ async def test_mqtt_ws_get_device_debug_info(
@patch("homeassistant.components.mqtt.PLATFORMS", [Platform.CAMERA]) @patch("homeassistant.components.mqtt.PLATFORMS", [Platform.CAMERA])
async def test_mqtt_ws_get_device_debug_info_binary( async def test_mqtt_ws_get_device_debug_info_binary(
hass, device_reg, hass_ws_client, mqtt_mock_entry_no_yaml_config hass, device_registry, hass_ws_client, mqtt_mock_entry_no_yaml_config
): ):
"""Test MQTT websocket device debug info.""" """Test MQTT websocket device debug info."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -2209,7 +2195,7 @@ async def test_mqtt_ws_get_device_debug_info_binary(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device entry is created # Verify device entry is created
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
assert device_entry is not None assert device_entry is not None
small_png = ( small_png = (
@ -2423,7 +2409,7 @@ async def test_debug_info_multiple_entities_triggers(
async def test_debug_info_non_mqtt( async def test_debug_info_non_mqtt(
hass, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass, device_registry, entity_registry, mqtt_mock_entry_no_yaml_config
): ):
"""Test we get empty debug_info for a device with non MQTT entities.""" """Test we get empty debug_info for a device with non MQTT entities."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -2433,12 +2419,12 @@ async def test_debug_info_non_mqtt(
config_entry = MockConfigEntry(domain="test", data={}) config_entry = MockConfigEntry(domain="test", data={})
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
device_entry = device_reg.async_get_or_create( device_entry = device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, connections={(dr.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")},
) )
for device_class in DEVICE_CLASSES: for device_class in DEVICE_CLASSES:
entity_reg.async_get_or_create( entity_registry.async_get_or_create(
DOMAIN, DOMAIN,
"test", "test",
platform.ENTITIES[device_class].unique_id, platform.ENTITIES[device_class].unique_id,

View file

@ -19,8 +19,6 @@ from tests.common import (
MockConfigEntry, MockConfigEntry,
async_fire_mqtt_message, async_fire_mqtt_message,
async_get_device_automations, async_get_device_automations,
mock_device_registry,
mock_registry,
) )
DEFAULT_CONFIG_DEVICE = { DEFAULT_CONFIG_DEVICE = {
@ -54,18 +52,6 @@ def binary_sensor_only():
yield yield
@pytest.fixture
def device_reg(hass):
"""Return an empty, loaded, registry."""
return mock_device_registry(hass)
@pytest.fixture
def entity_reg(hass):
"""Return an empty, loaded, registry."""
return mock_registry(hass)
@pytest.fixture @pytest.fixture
def tag_mock(): def tag_mock():
"""Fixture to mock tag.""" """Fixture to mock tag."""
@ -76,8 +62,7 @@ def tag_mock():
@pytest.mark.no_fail_on_log_exception @pytest.mark.no_fail_on_log_exception
async def test_discover_bad_tag( async def test_discover_bad_tag(
hass: HomeAssistant, hass: HomeAssistant,
device_reg, device_registry,
entity_reg,
mqtt_mock_entry_no_yaml_config, mqtt_mock_entry_no_yaml_config,
tag_mock, tag_mock,
) -> None: ) -> None:
@ -89,13 +74,13 @@ async def test_discover_bad_tag(
data0 = '{ "device":{"identifiers":["0AFFD2"]}, "topics": "foobar/tag_scanned" }' data0 = '{ "device":{"identifiers":["0AFFD2"]}, "topics": "foobar/tag_scanned" }'
async_fire_mqtt_message(hass, "homeassistant/tag/bla/config", data0) async_fire_mqtt_message(hass, "homeassistant/tag/bla/config", data0)
await hass.async_block_till_done() await hass.async_block_till_done()
assert device_reg.async_get_device({("mqtt", "0AFFD2")}) is None assert device_registry.async_get_device({("mqtt", "0AFFD2")}) is None
# Test sending correct data # Test sending correct data
async_fire_mqtt_message(hass, "homeassistant/tag/bla/config", json.dumps(config1)) async_fire_mqtt_message(hass, "homeassistant/tag/bla/config", json.dumps(config1))
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
# Fake tag scan. # Fake tag scan.
async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN) async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN)
await hass.async_block_till_done() await hass.async_block_till_done()
@ -103,7 +88,7 @@ async def test_discover_bad_tag(
async def test_if_fires_on_mqtt_message_with_device( async def test_if_fires_on_mqtt_message_with_device(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config, tag_mock hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config, tag_mock
) -> None: ) -> None:
"""Test tag scanning, with device.""" """Test tag scanning, with device."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -111,7 +96,7 @@ async def test_if_fires_on_mqtt_message_with_device(
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config))
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
# Fake tag scan. # Fake tag scan.
async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN) async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN)
@ -120,7 +105,7 @@ async def test_if_fires_on_mqtt_message_with_device(
async def test_if_fires_on_mqtt_message_without_device( async def test_if_fires_on_mqtt_message_without_device(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config, tag_mock hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, tag_mock
) -> None: ) -> None:
"""Test tag scanning, without device.""" """Test tag scanning, without device."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -136,7 +121,7 @@ async def test_if_fires_on_mqtt_message_without_device(
async def test_if_fires_on_mqtt_message_with_template( async def test_if_fires_on_mqtt_message_with_template(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config, tag_mock hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config, tag_mock
) -> None: ) -> None:
"""Test tag scanning, with device.""" """Test tag scanning, with device."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -144,7 +129,7 @@ async def test_if_fires_on_mqtt_message_with_template(
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config))
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
# Fake tag scan. # Fake tag scan.
async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN_JSON) async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN_JSON)
@ -153,7 +138,7 @@ async def test_if_fires_on_mqtt_message_with_template(
async def test_strip_tag_id( async def test_strip_tag_id(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config, tag_mock hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, tag_mock
) -> None: ) -> None:
"""Test strip whitespace from tag_id.""" """Test strip whitespace from tag_id."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -169,7 +154,7 @@ async def test_strip_tag_id(
async def test_if_fires_on_mqtt_message_after_update_with_device( async def test_if_fires_on_mqtt_message_after_update_with_device(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config, tag_mock hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config, tag_mock
) -> None: ) -> None:
"""Test tag scanning after update.""" """Test tag scanning after update."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -181,7 +166,7 @@ async def test_if_fires_on_mqtt_message_after_update_with_device(
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config1)) async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config1))
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
# Fake tag scan. # Fake tag scan.
async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN) async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN)
@ -216,7 +201,7 @@ async def test_if_fires_on_mqtt_message_after_update_with_device(
async def test_if_fires_on_mqtt_message_after_update_without_device( async def test_if_fires_on_mqtt_message_after_update_without_device(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config, tag_mock hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, tag_mock
) -> None: ) -> None:
"""Test tag scanning after update.""" """Test tag scanning after update."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -260,7 +245,7 @@ async def test_if_fires_on_mqtt_message_after_update_without_device(
async def test_if_fires_on_mqtt_message_after_update_with_template( async def test_if_fires_on_mqtt_message_after_update_with_template(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config, tag_mock hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config, tag_mock
) -> None: ) -> None:
"""Test tag scanning after update.""" """Test tag scanning after update."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -271,7 +256,7 @@ async def test_if_fires_on_mqtt_message_after_update_with_template(
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config1)) async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config1))
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
# Fake tag scan. # Fake tag scan.
async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN_JSON) async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN_JSON)
@ -306,7 +291,7 @@ async def test_if_fires_on_mqtt_message_after_update_with_template(
async def test_no_resubscribe_same_topic( async def test_no_resubscribe_same_topic(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test subscription to topics without change.""" """Test subscription to topics without change."""
mqtt_mock = await mqtt_mock_entry_no_yaml_config() mqtt_mock = await mqtt_mock_entry_no_yaml_config()
@ -314,7 +299,7 @@ async def test_no_resubscribe_same_topic(
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config))
await hass.async_block_till_done() await hass.async_block_till_done()
assert device_reg.async_get_device({("mqtt", "0AFFD2")}) assert device_registry.async_get_device({("mqtt", "0AFFD2")})
call_count = mqtt_mock.async_subscribe.call_count call_count = mqtt_mock.async_subscribe.call_count
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config))
@ -323,7 +308,7 @@ async def test_no_resubscribe_same_topic(
async def test_not_fires_on_mqtt_message_after_remove_by_mqtt_with_device( async def test_not_fires_on_mqtt_message_after_remove_by_mqtt_with_device(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config, tag_mock hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config, tag_mock
) -> None: ) -> None:
"""Test tag scanning after removal.""" """Test tag scanning after removal."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -331,7 +316,7 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt_with_device(
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config))
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
# Fake tag scan. # Fake tag scan.
async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN) async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN)
@ -357,7 +342,7 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt_with_device(
async def test_not_fires_on_mqtt_message_after_remove_by_mqtt_without_device( async def test_not_fires_on_mqtt_message_after_remove_by_mqtt_without_device(
hass: HomeAssistant, device_reg, mqtt_mock_entry_no_yaml_config, tag_mock hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, tag_mock
) -> None: ) -> None:
"""Test tag scanning not firing after removal.""" """Test tag scanning not firing after removal."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -392,7 +377,7 @@ async def test_not_fires_on_mqtt_message_after_remove_by_mqtt_without_device(
async def test_not_fires_on_mqtt_message_after_remove_from_registry( async def test_not_fires_on_mqtt_message_after_remove_from_registry(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client, hass_ws_client,
device_reg, device_registry,
mqtt_mock_entry_no_yaml_config, mqtt_mock_entry_no_yaml_config,
tag_mock, tag_mock,
) -> None: ) -> None:
@ -406,7 +391,7 @@ async def test_not_fires_on_mqtt_message_after_remove_from_registry(
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config))
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
# Fake tag scan. # Fake tag scan.
async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN) async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN)
@ -540,8 +525,7 @@ async def test_entity_device_info_update(
async def test_cleanup_tag( async def test_cleanup_tag(
hass: HomeAssistant, hass: HomeAssistant,
hass_ws_client, hass_ws_client,
device_reg, device_registry,
entity_reg,
mqtt_mock_entry_no_yaml_config, mqtt_mock_entry_no_yaml_config,
) -> None: ) -> None:
"""Test tag discovery topic is cleaned when device is removed from registry.""" """Test tag discovery topic is cleaned when device is removed from registry."""
@ -555,7 +539,7 @@ async def test_cleanup_tag(
config_entry = MockConfigEntry(domain="test") config_entry = MockConfigEntry(domain="test")
config_entry.add_to_hass(hass) config_entry.add_to_hass(hass)
device_reg.async_get_or_create( device_registry.async_get_or_create(
config_entry_id=config_entry.entry_id, config_entry_id=config_entry.entry_id,
connections=set(), connections=set(),
identifiers={("mqtt", "helloworld")}, identifiers={("mqtt", "helloworld")},
@ -578,20 +562,20 @@ async def test_cleanup_tag(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entries are created # Verify device registry entries are created
device_entry1 = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry1 = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry1 is not None assert device_entry1 is not None
assert device_entry1.config_entries == {config_entry.entry_id, mqtt_entry.entry_id} assert device_entry1.config_entries == {config_entry.entry_id, mqtt_entry.entry_id}
device_entry2 = device_reg.async_get_device({("mqtt", "hejhopp")}) device_entry2 = device_registry.async_get_device({("mqtt", "hejhopp")})
assert device_entry2 is not None assert device_entry2 is not None
# Remove other config entry from the device # Remove other config entry from the device
device_reg.async_update_device( device_registry.async_update_device(
device_entry1.id, remove_config_entry_id=config_entry.entry_id device_entry1.id, remove_config_entry_id=config_entry.entry_id
) )
device_entry1 = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry1 = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry1 is not None assert device_entry1 is not None
assert device_entry1.config_entries == {mqtt_entry.entry_id} assert device_entry1.config_entries == {mqtt_entry.entry_id}
device_entry2 = device_reg.async_get_device({("mqtt", "hejhopp")}) device_entry2 = device_registry.async_get_device({("mqtt", "hejhopp")})
assert device_entry2 is not None assert device_entry2 is not None
mqtt_mock.async_publish.assert_not_called() mqtt_mock.async_publish.assert_not_called()
@ -611,9 +595,9 @@ async def test_cleanup_tag(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is cleared # Verify device registry entry is cleared
device_entry1 = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry1 = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry1 is None assert device_entry1 is None
device_entry2 = device_reg.async_get_device({("mqtt", "hejhopp")}) device_entry2 = device_registry.async_get_device({("mqtt", "hejhopp")})
assert device_entry2 is not None assert device_entry2 is not None
# Verify retained discovery topic has been cleared # Verify retained discovery topic has been cleared
@ -623,7 +607,7 @@ async def test_cleanup_tag(
async def test_cleanup_device( async def test_cleanup_device(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test removal from device registry when tag is removed.""" """Test removal from device registry when tag is removed."""
await mqtt_mock_entry_no_yaml_config() await mqtt_mock_entry_no_yaml_config()
@ -637,21 +621,20 @@ async def test_cleanup_device(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is created # Verify device registry entry is created
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
async_fire_mqtt_message(hass, "homeassistant/tag/bla/config", "") async_fire_mqtt_message(hass, "homeassistant/tag/bla/config", "")
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is cleared # Verify device registry entry is cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is None assert device_entry is None
async def test_cleanup_device_several_tags( async def test_cleanup_device_several_tags(
hass: HomeAssistant, hass: HomeAssistant,
device_reg, device_registry,
entity_reg,
mqtt_mock_entry_no_yaml_config, mqtt_mock_entry_no_yaml_config,
tag_mock, tag_mock,
) -> None: ) -> None:
@ -673,14 +656,14 @@ async def test_cleanup_device_several_tags(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is created # Verify device registry entry is created
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", "") async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", "")
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is not cleared # Verify device registry entry is not cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
# Fake tag scan. # Fake tag scan.
@ -693,12 +676,12 @@ async def test_cleanup_device_several_tags(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is cleared # Verify device registry entry is cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is None assert device_entry is None
async def test_cleanup_device_with_entity_and_trigger_1( async def test_cleanup_device_with_entity_and_trigger_1(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test removal from device registry for device with tag, entity and trigger. """Test removal from device registry for device with tag, entity and trigger.
@ -736,7 +719,7 @@ async def test_cleanup_device_with_entity_and_trigger_1(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is created # Verify device registry entry is created
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
@ -748,7 +731,7 @@ async def test_cleanup_device_with_entity_and_trigger_1(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is not cleared # Verify device registry entry is not cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", "") async_fire_mqtt_message(hass, "homeassistant/device_automation/bla2/config", "")
@ -758,12 +741,12 @@ async def test_cleanup_device_with_entity_and_trigger_1(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is cleared # Verify device registry entry is cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is None assert device_entry is None
async def test_cleanup_device_with_entity2( async def test_cleanup_device_with_entity2(
hass: HomeAssistant, device_reg, entity_reg, mqtt_mock_entry_no_yaml_config hass: HomeAssistant, device_registry, mqtt_mock_entry_no_yaml_config
) -> None: ) -> None:
"""Test removal from device registry for device with tag, entity and trigger. """Test removal from device registry for device with tag, entity and trigger.
@ -801,7 +784,7 @@ async def test_cleanup_device_with_entity2(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is created # Verify device registry entry is created
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
triggers = await async_get_device_automations( triggers = await async_get_device_automations(
@ -816,14 +799,14 @@ async def test_cleanup_device_with_entity2(
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is not cleared # Verify device registry entry is not cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is not None assert device_entry is not None
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", "") async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", "")
await hass.async_block_till_done() await hass.async_block_till_done()
# Verify device registry entry is cleared # Verify device registry entry is cleared
device_entry = device_reg.async_get_device({("mqtt", "helloworld")}) device_entry = device_registry.async_get_device({("mqtt", "helloworld")})
assert device_entry is None assert device_entry is None
@ -872,7 +855,7 @@ async def test_update_with_bad_config_not_breaks_discovery(
async def test_unload_entry( async def test_unload_entry(
hass: HomeAssistant, device_reg, mqtt_mock, tag_mock, tmp_path hass: HomeAssistant, device_registry, mqtt_mock, tag_mock, tmp_path
) -> None: ) -> None:
"""Test unloading the MQTT entry.""" """Test unloading the MQTT entry."""
@ -880,7 +863,7 @@ async def test_unload_entry(
async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config)) async_fire_mqtt_message(hass, "homeassistant/tag/bla1/config", json.dumps(config))
await hass.async_block_till_done() await hass.async_block_till_done()
device_entry = device_reg.async_get_device({("mqtt", "0AFFD2")}) device_entry = device_registry.async_get_device({("mqtt", "0AFFD2")})
# Fake tag scan, should be processed # Fake tag scan, should be processed
async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN) async_fire_mqtt_message(hass, "foobar/tag_scanned", DEFAULT_TAG_SCAN)