Skip to content

Commit

Permalink
Clean up unnecessary registry mocks from mqtt tests (home-assistant#8…
Browse files Browse the repository at this point in the history
…7728)

* Clean up unnecessary registry mocks from mqtt tests

* Fix helper methods
  • Loading branch information
frenck authored Feb 9, 2023
1 parent 80bf632 commit 76bf6f1
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 238 deletions.
26 changes: 15 additions & 11 deletions tests/components/mqtt/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
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

DEFAULT_CONFIG_DEVICE_INFO_ID = {
Expand Down Expand Up @@ -1144,7 +1144,7 @@ async def help_test_entity_id_update_subscriptions(
config[mqtt.DOMAIN][domain]["state_topic"] = "test-topic"
topics = ["avty-topic", "test-topic"]
assert len(topics) > 0
registry = mock_registry(hass, {})
entity_registry = er.async_get(hass)

assert await async_setup_component(
hass,
Expand All @@ -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.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()

state = hass.states.get(f"{domain}.test")
Expand Down Expand Up @@ -1191,8 +1193,7 @@ async def help_test_entity_id_update_discovery_update(
config[mqtt.DOMAIN][domain]["availability_topic"] = "avty-topic"
topic = "avty-topic"

ent_registry = mock_registry(hass, {})

entity_registry = er.async_get(hass)
data = json.dumps(config[mqtt.DOMAIN][domain])
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data)
await hass.async_block_till_done()
Expand All @@ -1205,7 +1206,9 @@ async def help_test_entity_id_update_discovery_update(
state = hass.states.get(f"{domain}.test")
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()

config[mqtt.DOMAIN][domain]["availability_topic"] = f"{topic}_2"
Expand Down Expand Up @@ -1497,14 +1500,13 @@ async def help_test_entity_debug_info_update_entity_id(
config["unique_id"] = "veryunique"
config["platform"] = "mqtt"

dev_registry = dr.async_get(hass)
ent_registry = mock_registry(hass, {})

device_registry = dr.async_get(hass)
entity_registry = er.async_get(hass)
data = json.dumps(config)
async_fire_mqtt_message(hass, f"homeassistant/{domain}/bla/config", data)
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

debug_info_data = debug_info.info_for_device(hass, device.id)
Expand All @@ -1521,7 +1523,9 @@ async def help_test_entity_debug_info_update_entity_id(
]
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()

Expand Down
26 changes: 7 additions & 19 deletions tests/components/mqtt/test_device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
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 = {
mqtt.DOMAIN: {
Expand All @@ -34,18 +34,6 @@ def device_tracker_platform_only():
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(
hass: HomeAssistant, mqtt_mock_entry_no_yaml_config, caplog
) -> None:
Expand Down Expand Up @@ -222,8 +210,8 @@ async def test_device_tracker_discovery_update(
async def test_cleanup_device_tracker(
hass: HomeAssistant,
hass_ws_client,
device_reg,
entity_reg,
device_registry,
entity_registry,
mqtt_mock_entry_no_yaml_config,
) -> None:
"""Test discovered device is cleaned up when removed from registry."""
Expand All @@ -242,9 +230,9 @@ async def test_cleanup_device_tracker(
await hass.async_block_till_done()

# 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
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

state = hass.states.get("device_tracker.mqtt_unique")
Expand All @@ -266,9 +254,9 @@ async def test_cleanup_device_tracker(
await hass.async_block_till_done()

# 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
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

# Verify state is removed
Expand Down
Loading

0 comments on commit 76bf6f1

Please sign in to comment.