hass-core/tests/components/dlna_dmr/test_init.py
Michael Chisholm 667e730946
Improve dlna_dmr code quality ()
* Listen for config updates from DlnaDmrEntity.async_added_to_hass

Use `Entity.async_on_remove` for dealing with callback cancellation,
instead of re-inventing the wheel with `_remove_ssdp_callbacks`.

* Use async_write_ha_state within async methods

* Import YAML config from async_setup_platform

* Import flow prompts user when device is uncontactable during migration

When config flow is able to contact a device, or when it has information
from SSDP, it will create config entries without error. If the device is
uncontactable at this point then it will appear as unavailable in HA
until it is turned on again.

When import flow cannot migrate an entry because it needs to contact the
device and can't, it will notify the user with a config flow form.

* Don't del unused parameters, HA pylint doesn't care

* Remove unused imports from tests

* Abort config flow at earliest opportunity

* Return async_abort instead of raising AbortFlow

* Consolidate config entry test cleanup into a single function

* fixup! Consolidate config entry test cleanup into a single function

Revert "Consolidate config entry test cleanup into a single function"

This reverts commit 8220da7263.

* Check resource acquisition/release in specific tests

* fixup! Check resource acquisition/release in specific tests

* Remove unused network dependency from manifest

* _on_event runs in async context

* Call async_write_ha_state directly (not via shedule_update)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-10-07 22:14:00 +02:00

60 lines
2.5 KiB
Python

"""Test the DLNA DMR component setup and cleanup."""
from unittest.mock import Mock
from homeassistant.components import media_player
from homeassistant.components.dlna_dmr.const import DOMAIN as DLNA_DOMAIN
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
async def test_resource_lifecycle(
hass: HomeAssistant,
domain_data_mock: Mock,
config_entry_mock: MockConfigEntry,
ssdp_scanner_mock: Mock,
dmr_device_mock: Mock,
) -> None:
"""Test that resources are acquired/released as the entity is setup/unloaded."""
# Set up the config entry
config_entry_mock.add_to_hass(hass)
assert await async_setup_component(hass, DLNA_DOMAIN, {}) is True
await hass.async_block_till_done()
# Check the entity is created and working
entries = entity_registry.async_entries_for_config_entry(
entity_registry.async_get(hass), config_entry_mock.entry_id
)
assert len(entries) == 1
entity_id = entries[0].entity_id
mock_state = hass.states.get(entity_id)
assert mock_state is not None
assert mock_state.state == media_player.STATE_IDLE
# Check update listeners and event notifiers are subscribed
assert len(config_entry_mock.update_listeners) == 1
assert domain_data_mock.async_get_event_notifier.await_count == 1
assert domain_data_mock.async_release_event_notifier.await_count == 0
assert ssdp_scanner_mock.async_register_callback.await_count == 2
assert ssdp_scanner_mock.async_register_callback.return_value.call_count == 0
assert dmr_device_mock.async_subscribe_services.await_count == 1
assert dmr_device_mock.async_unsubscribe_services.await_count == 0
assert dmr_device_mock.on_event is not None
# Unload the config entry
assert await hass.config_entries.async_remove(config_entry_mock.entry_id) == {
"require_restart": False
}
# Check update listeners and event notifiers are released
assert not config_entry_mock.update_listeners
assert domain_data_mock.async_get_event_notifier.await_count == 1
assert domain_data_mock.async_release_event_notifier.await_count == 1
assert ssdp_scanner_mock.async_register_callback.await_count == 2
assert ssdp_scanner_mock.async_register_callback.return_value.call_count == 2
assert dmr_device_mock.async_subscribe_services.await_count == 1
assert dmr_device_mock.async_unsubscribe_services.await_count == 1
assert dmr_device_mock.on_event is None