core/tests/components/dlna_dmr/test_init.py

61 lines
2.5 KiB
Python
Raw Normal View History

Improve dlna_dmr code quality (#56886) * 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 8220da7263e346a37c3e1a219dc374c4a43c7df5. * 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 20:14:00 +00:00
"""Test the DLNA DMR component setup and cleanup."""
Config-flow for DLNA-DMR integration (#55267) * Modernize dlna_dmr component: configflow, test, types * Support config-flow with ssdp discovery * Add unit tests * Enforce strict typing * Gracefully handle network devices (dis)appearing * Fix Aiohttp mock response headers type to match actual response class * Fixes from code review * Fixes from code review * Import device config in flow if unavailable at hass start * Support SSDP advertisements * Ignore bad BOOTID, fix ssdp:byebye handling * Only listen for events on interface connected to device * Release all listeners when entities are removed * Warn about deprecated dlna_dmr configuration * Use sublogger for dlna_dmr.config_flow for easier filtering * Tests for dlna_dmr.data module * Rewrite DMR tests for HA style * Fix DMR strings: "Digital Media *Renderer*" * Update DMR entity state and device info when changed * Replace deprecated async_upnp_client State with TransportState * supported_features are dynamic, based on current device state * Cleanup fully when subscription fails * Log warnings when device connection fails unexpectedly * Set PARALLEL_UPDATES to unlimited * Fix spelling * Fixes from code review * Simplify has & can checks to just can, which includes has * Treat transitioning state as playing (not idle) to reduce UI jerking * Test if device is usable * Handle ssdp:update messages properly * Fix _remove_ssdp_callbacks being shared by all DlnaDmrEntity instances * Fix tests for transitioning state * Mock DmrDevice.is_profile_device (added to support embedded devices) * Use ST & NT SSDP headers to find DMR devices, not deviceType The deviceType is extracted from the device's description XML, and will not be what we want when dealing with embedded devices. * Use UDN from SSDP headers, not device description, as unique_id The SSDP headers have the UDN of the embedded device that we're interested in, whereas the device description (`ATTR_UPNP_UDN`) field will always be for the root device. * Fix DMR string English localization * Test config flow with UDN from SSDP headers * Bump async-upnp-client==0.22.1, fix flake8 error * fix test for remapping * DMR HA Device connections based on root and embedded UDN * DmrDevice's UpnpDevice is now named profile_device * Use device type from SSDP headers, not device description * Mark dlna_dmr constants as Final * Use embedded device UDN and type for unique ID when connected via URL * More informative connection error messages * Also match SSDP messages on NT headers The NT header is to ssdp:alive messages what ST is to M-SEARCH responses. * Bump async-upnp-client==0.22.2 * fix merge * Bump async-upnp-client==0.22.3 Co-authored-by: Steven Looman <steven.looman@gmail.com> Co-authored-by: J. Nick Koston <nick@koston.org>
2021-09-27 20:47:01 +00:00
from unittest.mock import Mock
Improve dlna_dmr code quality (#56886) * 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 8220da7263e346a37c3e1a219dc374c4a43c7df5. * 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 20:14:00 +00:00
from homeassistant.components import media_player
from homeassistant.components.dlna_dmr.const import DOMAIN as DLNA_DOMAIN
Config-flow for DLNA-DMR integration (#55267) * Modernize dlna_dmr component: configflow, test, types * Support config-flow with ssdp discovery * Add unit tests * Enforce strict typing * Gracefully handle network devices (dis)appearing * Fix Aiohttp mock response headers type to match actual response class * Fixes from code review * Fixes from code review * Import device config in flow if unavailable at hass start * Support SSDP advertisements * Ignore bad BOOTID, fix ssdp:byebye handling * Only listen for events on interface connected to device * Release all listeners when entities are removed * Warn about deprecated dlna_dmr configuration * Use sublogger for dlna_dmr.config_flow for easier filtering * Tests for dlna_dmr.data module * Rewrite DMR tests for HA style * Fix DMR strings: "Digital Media *Renderer*" * Update DMR entity state and device info when changed * Replace deprecated async_upnp_client State with TransportState * supported_features are dynamic, based on current device state * Cleanup fully when subscription fails * Log warnings when device connection fails unexpectedly * Set PARALLEL_UPDATES to unlimited * Fix spelling * Fixes from code review * Simplify has & can checks to just can, which includes has * Treat transitioning state as playing (not idle) to reduce UI jerking * Test if device is usable * Handle ssdp:update messages properly * Fix _remove_ssdp_callbacks being shared by all DlnaDmrEntity instances * Fix tests for transitioning state * Mock DmrDevice.is_profile_device (added to support embedded devices) * Use ST & NT SSDP headers to find DMR devices, not deviceType The deviceType is extracted from the device's description XML, and will not be what we want when dealing with embedded devices. * Use UDN from SSDP headers, not device description, as unique_id The SSDP headers have the UDN of the embedded device that we're interested in, whereas the device description (`ATTR_UPNP_UDN`) field will always be for the root device. * Fix DMR string English localization * Test config flow with UDN from SSDP headers * Bump async-upnp-client==0.22.1, fix flake8 error * fix test for remapping * DMR HA Device connections based on root and embedded UDN * DmrDevice's UpnpDevice is now named profile_device * Use device type from SSDP headers, not device description * Mark dlna_dmr constants as Final * Use embedded device UDN and type for unique ID when connected via URL * More informative connection error messages * Also match SSDP messages on NT headers The NT header is to ssdp:alive messages what ST is to M-SEARCH responses. * Bump async-upnp-client==0.22.2 * fix merge * Bump async-upnp-client==0.22.3 Co-authored-by: Steven Looman <steven.looman@gmail.com> Co-authored-by: J. Nick Koston <nick@koston.org>
2021-09-27 20:47:01 +00:00
from homeassistant.core import HomeAssistant
Improve dlna_dmr code quality (#56886) * 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 8220da7263e346a37c3e1a219dc374c4a43c7df5. * 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 20:14:00 +00:00
from homeassistant.helpers import entity_registry
Config-flow for DLNA-DMR integration (#55267) * Modernize dlna_dmr component: configflow, test, types * Support config-flow with ssdp discovery * Add unit tests * Enforce strict typing * Gracefully handle network devices (dis)appearing * Fix Aiohttp mock response headers type to match actual response class * Fixes from code review * Fixes from code review * Import device config in flow if unavailable at hass start * Support SSDP advertisements * Ignore bad BOOTID, fix ssdp:byebye handling * Only listen for events on interface connected to device * Release all listeners when entities are removed * Warn about deprecated dlna_dmr configuration * Use sublogger for dlna_dmr.config_flow for easier filtering * Tests for dlna_dmr.data module * Rewrite DMR tests for HA style * Fix DMR strings: "Digital Media *Renderer*" * Update DMR entity state and device info when changed * Replace deprecated async_upnp_client State with TransportState * supported_features are dynamic, based on current device state * Cleanup fully when subscription fails * Log warnings when device connection fails unexpectedly * Set PARALLEL_UPDATES to unlimited * Fix spelling * Fixes from code review * Simplify has & can checks to just can, which includes has * Treat transitioning state as playing (not idle) to reduce UI jerking * Test if device is usable * Handle ssdp:update messages properly * Fix _remove_ssdp_callbacks being shared by all DlnaDmrEntity instances * Fix tests for transitioning state * Mock DmrDevice.is_profile_device (added to support embedded devices) * Use ST & NT SSDP headers to find DMR devices, not deviceType The deviceType is extracted from the device's description XML, and will not be what we want when dealing with embedded devices. * Use UDN from SSDP headers, not device description, as unique_id The SSDP headers have the UDN of the embedded device that we're interested in, whereas the device description (`ATTR_UPNP_UDN`) field will always be for the root device. * Fix DMR string English localization * Test config flow with UDN from SSDP headers * Bump async-upnp-client==0.22.1, fix flake8 error * fix test for remapping * DMR HA Device connections based on root and embedded UDN * DmrDevice's UpnpDevice is now named profile_device * Use device type from SSDP headers, not device description * Mark dlna_dmr constants as Final * Use embedded device UDN and type for unique ID when connected via URL * More informative connection error messages * Also match SSDP messages on NT headers The NT header is to ssdp:alive messages what ST is to M-SEARCH responses. * Bump async-upnp-client==0.22.2 * fix merge * Bump async-upnp-client==0.22.3 Co-authored-by: Steven Looman <steven.looman@gmail.com> Co-authored-by: J. Nick Koston <nick@koston.org>
2021-09-27 20:47:01 +00:00
from homeassistant.setup import async_setup_component
Improve dlna_dmr code quality (#56886) * 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 8220da7263e346a37c3e1a219dc374c4a43c7df5. * 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 20:14:00 +00:00
from tests.common import MockConfigEntry
Config-flow for DLNA-DMR integration (#55267) * Modernize dlna_dmr component: configflow, test, types * Support config-flow with ssdp discovery * Add unit tests * Enforce strict typing * Gracefully handle network devices (dis)appearing * Fix Aiohttp mock response headers type to match actual response class * Fixes from code review * Fixes from code review * Import device config in flow if unavailable at hass start * Support SSDP advertisements * Ignore bad BOOTID, fix ssdp:byebye handling * Only listen for events on interface connected to device * Release all listeners when entities are removed * Warn about deprecated dlna_dmr configuration * Use sublogger for dlna_dmr.config_flow for easier filtering * Tests for dlna_dmr.data module * Rewrite DMR tests for HA style * Fix DMR strings: "Digital Media *Renderer*" * Update DMR entity state and device info when changed * Replace deprecated async_upnp_client State with TransportState * supported_features are dynamic, based on current device state * Cleanup fully when subscription fails * Log warnings when device connection fails unexpectedly * Set PARALLEL_UPDATES to unlimited * Fix spelling * Fixes from code review * Simplify has & can checks to just can, which includes has * Treat transitioning state as playing (not idle) to reduce UI jerking * Test if device is usable * Handle ssdp:update messages properly * Fix _remove_ssdp_callbacks being shared by all DlnaDmrEntity instances * Fix tests for transitioning state * Mock DmrDevice.is_profile_device (added to support embedded devices) * Use ST & NT SSDP headers to find DMR devices, not deviceType The deviceType is extracted from the device's description XML, and will not be what we want when dealing with embedded devices. * Use UDN from SSDP headers, not device description, as unique_id The SSDP headers have the UDN of the embedded device that we're interested in, whereas the device description (`ATTR_UPNP_UDN`) field will always be for the root device. * Fix DMR string English localization * Test config flow with UDN from SSDP headers * Bump async-upnp-client==0.22.1, fix flake8 error * fix test for remapping * DMR HA Device connections based on root and embedded UDN * DmrDevice's UpnpDevice is now named profile_device * Use device type from SSDP headers, not device description * Mark dlna_dmr constants as Final * Use embedded device UDN and type for unique ID when connected via URL * More informative connection error messages * Also match SSDP messages on NT headers The NT header is to ssdp:alive messages what ST is to M-SEARCH responses. * Bump async-upnp-client==0.22.2 * fix merge * Bump async-upnp-client==0.22.3 Co-authored-by: Steven Looman <steven.looman@gmail.com> Co-authored-by: J. Nick Koston <nick@koston.org>
2021-09-27 20:47:01 +00:00
Improve dlna_dmr code quality (#56886) * 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 8220da7263e346a37c3e1a219dc374c4a43c7df5. * 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 20:14:00 +00:00
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
Config-flow for DLNA-DMR integration (#55267) * Modernize dlna_dmr component: configflow, test, types * Support config-flow with ssdp discovery * Add unit tests * Enforce strict typing * Gracefully handle network devices (dis)appearing * Fix Aiohttp mock response headers type to match actual response class * Fixes from code review * Fixes from code review * Import device config in flow if unavailable at hass start * Support SSDP advertisements * Ignore bad BOOTID, fix ssdp:byebye handling * Only listen for events on interface connected to device * Release all listeners when entities are removed * Warn about deprecated dlna_dmr configuration * Use sublogger for dlna_dmr.config_flow for easier filtering * Tests for dlna_dmr.data module * Rewrite DMR tests for HA style * Fix DMR strings: "Digital Media *Renderer*" * Update DMR entity state and device info when changed * Replace deprecated async_upnp_client State with TransportState * supported_features are dynamic, based on current device state * Cleanup fully when subscription fails * Log warnings when device connection fails unexpectedly * Set PARALLEL_UPDATES to unlimited * Fix spelling * Fixes from code review * Simplify has & can checks to just can, which includes has * Treat transitioning state as playing (not idle) to reduce UI jerking * Test if device is usable * Handle ssdp:update messages properly * Fix _remove_ssdp_callbacks being shared by all DlnaDmrEntity instances * Fix tests for transitioning state * Mock DmrDevice.is_profile_device (added to support embedded devices) * Use ST & NT SSDP headers to find DMR devices, not deviceType The deviceType is extracted from the device's description XML, and will not be what we want when dealing with embedded devices. * Use UDN from SSDP headers, not device description, as unique_id The SSDP headers have the UDN of the embedded device that we're interested in, whereas the device description (`ATTR_UPNP_UDN`) field will always be for the root device. * Fix DMR string English localization * Test config flow with UDN from SSDP headers * Bump async-upnp-client==0.22.1, fix flake8 error * fix test for remapping * DMR HA Device connections based on root and embedded UDN * DmrDevice's UpnpDevice is now named profile_device * Use device type from SSDP headers, not device description * Mark dlna_dmr constants as Final * Use embedded device UDN and type for unique ID when connected via URL * More informative connection error messages * Also match SSDP messages on NT headers The NT header is to ssdp:alive messages what ST is to M-SEARCH responses. * Bump async-upnp-client==0.22.2 * fix merge * Bump async-upnp-client==0.22.3 Co-authored-by: Steven Looman <steven.looman@gmail.com> Co-authored-by: J. Nick Koston <nick@koston.org>
2021-09-27 20:47:01 +00:00
await hass.async_block_till_done()
Improve dlna_dmr code quality (#56886) * 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 8220da7263e346a37c3e1a219dc374c4a43c7df5. * 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 20:14:00 +00:00
# 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
Config-flow for DLNA-DMR integration (#55267) * Modernize dlna_dmr component: configflow, test, types * Support config-flow with ssdp discovery * Add unit tests * Enforce strict typing * Gracefully handle network devices (dis)appearing * Fix Aiohttp mock response headers type to match actual response class * Fixes from code review * Fixes from code review * Import device config in flow if unavailable at hass start * Support SSDP advertisements * Ignore bad BOOTID, fix ssdp:byebye handling * Only listen for events on interface connected to device * Release all listeners when entities are removed * Warn about deprecated dlna_dmr configuration * Use sublogger for dlna_dmr.config_flow for easier filtering * Tests for dlna_dmr.data module * Rewrite DMR tests for HA style * Fix DMR strings: "Digital Media *Renderer*" * Update DMR entity state and device info when changed * Replace deprecated async_upnp_client State with TransportState * supported_features are dynamic, based on current device state * Cleanup fully when subscription fails * Log warnings when device connection fails unexpectedly * Set PARALLEL_UPDATES to unlimited * Fix spelling * Fixes from code review * Simplify has & can checks to just can, which includes has * Treat transitioning state as playing (not idle) to reduce UI jerking * Test if device is usable * Handle ssdp:update messages properly * Fix _remove_ssdp_callbacks being shared by all DlnaDmrEntity instances * Fix tests for transitioning state * Mock DmrDevice.is_profile_device (added to support embedded devices) * Use ST & NT SSDP headers to find DMR devices, not deviceType The deviceType is extracted from the device's description XML, and will not be what we want when dealing with embedded devices. * Use UDN from SSDP headers, not device description, as unique_id The SSDP headers have the UDN of the embedded device that we're interested in, whereas the device description (`ATTR_UPNP_UDN`) field will always be for the root device. * Fix DMR string English localization * Test config flow with UDN from SSDP headers * Bump async-upnp-client==0.22.1, fix flake8 error * fix test for remapping * DMR HA Device connections based on root and embedded UDN * DmrDevice's UpnpDevice is now named profile_device * Use device type from SSDP headers, not device description * Mark dlna_dmr constants as Final * Use embedded device UDN and type for unique ID when connected via URL * More informative connection error messages * Also match SSDP messages on NT headers The NT header is to ssdp:alive messages what ST is to M-SEARCH responses. * Bump async-upnp-client==0.22.2 * fix merge * Bump async-upnp-client==0.22.3 Co-authored-by: Steven Looman <steven.looman@gmail.com> Co-authored-by: J. Nick Koston <nick@koston.org>
2021-09-27 20:47:01 +00:00
)
Improve dlna_dmr code quality (#56886) * 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 8220da7263e346a37c3e1a219dc374c4a43c7df5. * 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 20:14:00 +00:00
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
Config-flow for DLNA-DMR integration (#55267) * Modernize dlna_dmr component: configflow, test, types * Support config-flow with ssdp discovery * Add unit tests * Enforce strict typing * Gracefully handle network devices (dis)appearing * Fix Aiohttp mock response headers type to match actual response class * Fixes from code review * Fixes from code review * Import device config in flow if unavailable at hass start * Support SSDP advertisements * Ignore bad BOOTID, fix ssdp:byebye handling * Only listen for events on interface connected to device * Release all listeners when entities are removed * Warn about deprecated dlna_dmr configuration * Use sublogger for dlna_dmr.config_flow for easier filtering * Tests for dlna_dmr.data module * Rewrite DMR tests for HA style * Fix DMR strings: "Digital Media *Renderer*" * Update DMR entity state and device info when changed * Replace deprecated async_upnp_client State with TransportState * supported_features are dynamic, based on current device state * Cleanup fully when subscription fails * Log warnings when device connection fails unexpectedly * Set PARALLEL_UPDATES to unlimited * Fix spelling * Fixes from code review * Simplify has & can checks to just can, which includes has * Treat transitioning state as playing (not idle) to reduce UI jerking * Test if device is usable * Handle ssdp:update messages properly * Fix _remove_ssdp_callbacks being shared by all DlnaDmrEntity instances * Fix tests for transitioning state * Mock DmrDevice.is_profile_device (added to support embedded devices) * Use ST & NT SSDP headers to find DMR devices, not deviceType The deviceType is extracted from the device's description XML, and will not be what we want when dealing with embedded devices. * Use UDN from SSDP headers, not device description, as unique_id The SSDP headers have the UDN of the embedded device that we're interested in, whereas the device description (`ATTR_UPNP_UDN`) field will always be for the root device. * Fix DMR string English localization * Test config flow with UDN from SSDP headers * Bump async-upnp-client==0.22.1, fix flake8 error * fix test for remapping * DMR HA Device connections based on root and embedded UDN * DmrDevice's UpnpDevice is now named profile_device * Use device type from SSDP headers, not device description * Mark dlna_dmr constants as Final * Use embedded device UDN and type for unique ID when connected via URL * More informative connection error messages * Also match SSDP messages on NT headers The NT header is to ssdp:alive messages what ST is to M-SEARCH responses. * Bump async-upnp-client==0.22.2 * fix merge * Bump async-upnp-client==0.22.3 Co-authored-by: Steven Looman <steven.looman@gmail.com> Co-authored-by: J. Nick Koston <nick@koston.org>
2021-09-27 20:47:01 +00:00
}
Improve dlna_dmr code quality (#56886) * 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 8220da7263e346a37c3e1a219dc374c4a43c7df5. * 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 20:14:00 +00:00
# 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