This commit is contained in:
J. Nick Koston
2024-02-06 15:23:42 -06:00
parent 1723e338cb
commit 07ace6f169
3 changed files with 35 additions and 16 deletions

View File

@@ -163,11 +163,6 @@ class DlnaDmrEntity(MediaPlayerEntity):
async def async_added_to_hass(self) -> None:
"""Handle addition."""
self._background_setup_task = self.hass.async_create_background_task(
self._async_setup(), f"dlna_dmr {self.name} setup"
)
async def _async_setup(self) -> None:
# Update this entity when the associated config entry is modified
if self.registry_entry and self.registry_entry.config_entry_id:
config_entry = self.hass.config_entries.async_get_entry(
@@ -178,13 +173,6 @@ class DlnaDmrEntity(MediaPlayerEntity):
config_entry.add_update_listener(self.async_config_update_listener)
)
# Try to connect to the last known location, but don't worry if not available
if not self._device:
try:
await self._device_connect(self.location)
except UpnpError as err:
_LOGGER.debug("Couldn't connect immediately: %r", err)
# Get SSDP notifications for only this device
self.async_on_remove(
await ssdp.async_register_callback(
@@ -204,6 +192,18 @@ class DlnaDmrEntity(MediaPlayerEntity):
)
)
if not self._device:
self._background_setup_task = self.hass.async_create_background_task(
self._async_setup(), f"dlna_dmr {self.name} setup"
)
async def _async_setup(self) -> None:
# Try to connect to the last known location, but don't worry if not available
try:
await self._device_connect(self.location)
except UpnpError as err:
_LOGGER.debug("Couldn't connect immediately: %r", err)
async def async_will_remove_from_hass(self) -> None:
"""Handle removal."""
if self._background_setup_task:

View File

@@ -6,6 +6,7 @@ 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 as er
from homeassistant.helpers.entity_component import async_update_entity
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@@ -31,6 +32,10 @@ async def test_resource_lifecycle(
)
assert len(entries) == 1
entity_id = entries[0].entity_id
await async_update_entity(hass, entity_id)
await hass.async_block_till_done()
mock_state = hass.states.get(entity_id)
assert mock_state is not None
assert mock_state.state == media_player.STATE_IDLE

View File

@@ -216,6 +216,9 @@ async def test_setup_entry_no_options(
"""
config_entry_mock.options = MappingProxyType({})
mock_entity_id = await setup_mock_component(hass, config_entry_mock)
await async_update_entity(hass, mock_entity_id)
await hass.async_block_till_done()
mock_state = hass.states.get(mock_entity_id)
assert mock_state is not None
@@ -285,6 +288,8 @@ async def test_setup_entry_with_options(
}
)
mock_entity_id = await setup_mock_component(hass, config_entry_mock)
await async_update_entity(hass, mock_entity_id)
await hass.async_block_till_done()
mock_state = hass.states.get(mock_entity_id)
assert mock_state is not None
@@ -343,8 +348,9 @@ async def test_setup_entry_mac_address(
dmr_device_mock: Mock,
) -> None:
"""Entry with a MAC address will set up and set the device registry connection."""
await setup_mock_component(hass, config_entry_mock)
mock_entity_id = await setup_mock_component(hass, config_entry_mock)
await async_update_entity(hass, mock_entity_id)
await hass.async_block_till_done()
# Check the device registry connections for MAC address
dev_reg = async_get_dr(hass)
device = dev_reg.async_get_device(
@@ -363,8 +369,9 @@ async def test_setup_entry_no_mac_address(
dmr_device_mock: Mock,
) -> None:
"""Test setting up an entry without a MAC address will succeed."""
await setup_mock_component(hass, config_entry_mock_no_mac)
mock_entity_id = await setup_mock_component(hass, config_entry_mock_no_mac)
await async_update_entity(hass, mock_entity_id)
await hass.async_block_till_done()
# Check the device registry connections does not include the MAC address
dev_reg = async_get_dr(hass)
device = dev_reg.async_get_device(
@@ -382,6 +389,8 @@ async def test_event_subscribe_failure(
dmr_device_mock.async_subscribe_services.side_effect = UpnpError
mock_entity_id = await setup_mock_component(hass, config_entry_mock)
await async_update_entity(hass, mock_entity_id)
await hass.async_block_till_done()
mock_state = hass.states.get(mock_entity_id)
assert mock_state is not None
@@ -412,6 +421,8 @@ async def test_event_subscribe_rejected(
dmr_device_mock.async_subscribe_services.side_effect = UpnpResponseError(status=501)
mock_entity_id = await setup_mock_component(hass, config_entry_mock)
await async_update_entity(hass, mock_entity_id)
await hass.async_block_till_done()
mock_state = hass.states.get(mock_entity_id)
assert mock_state is not None
@@ -432,6 +443,8 @@ async def test_available_device(
) -> None:
"""Test a DlnaDmrEntity with a connected DmrDevice."""
# Check hass device information is filled in
await async_update_entity(hass, mock_entity_id)
await hass.async_block_till_done()
dev_reg = async_get_dr(hass)
device = dev_reg.async_get_device(
connections={(CONNECTION_UPNP, MOCK_DEVICE_UDN)},
@@ -1366,6 +1379,7 @@ async def test_become_available(
# Cause connection attempts to fail before adding entity
domain_data_mock.upnp_factory.async_create_device.side_effect = UpnpConnectionError
mock_entity_id = await setup_mock_component(hass, config_entry_mock)
await async_update_entity(hass, mock_entity_id)
mock_state = hass.states.get(mock_entity_id)
assert mock_state is not None
assert mock_state.state == ha_const.STATE_UNAVAILABLE