mirror of
https://github.com/home-assistant/core.git
synced 2026-08-24 10:13:52 -05:00
Fix via_device race in ntfy (#177738)
This commit is contained in:
@@ -14,7 +14,7 @@ from aiontfy.update import UpdateChecker
|
||||
from homeassistant.const import CONF_TOKEN, CONF_URL, CONF_VERIFY_SSL, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers import config_validation as cv, device_registry as dr
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
from homeassistant.util.hass_dict import HassKey
|
||||
@@ -92,6 +92,10 @@ async def async_setup_entry(hass: HomeAssistant, entry: NtfyConfigEntry) -> bool
|
||||
|
||||
entry.runtime_data = NtfyRuntimeData(coordinator, version)
|
||||
|
||||
dr.async_get(hass).async_get_or_create(
|
||||
config_entry_id=entry.entry_id, **coordinator.device_info
|
||||
)
|
||||
|
||||
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
|
||||
|
||||
entry.async_on_unload(entry.add_update_listener(_async_update_listener))
|
||||
|
||||
@@ -15,10 +15,13 @@ from aiontfy.exceptions import (
|
||||
NtfyUnauthorizedAuthenticationError,
|
||||
)
|
||||
from aiontfy.update import LatestRelease, UpdateChecker, UpdateCheckerError
|
||||
from yarl import URL
|
||||
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import CONF_URL
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import ConfigEntryAuthFailed
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
|
||||
|
||||
from .const import DOMAIN
|
||||
@@ -56,6 +59,17 @@ class BaseDataUpdateCoordinator[_DataT](DataUpdateCoordinator[_DataT]):
|
||||
|
||||
self.ntfy = ntfy
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device info of the ntfy account service device."""
|
||||
return DeviceInfo(
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
manufacturer="ntfy LLC",
|
||||
model="ntfy",
|
||||
configuration_url=URL(self.config_entry.data[CONF_URL]) / "app",
|
||||
identifiers={(DOMAIN, self.config_entry.entry_id)},
|
||||
)
|
||||
|
||||
@abstractmethod
|
||||
async def async_update_data(self) -> _DataT:
|
||||
"""Fetch the latest data from the source."""
|
||||
|
||||
@@ -4,6 +4,7 @@ from yarl import URL
|
||||
|
||||
from homeassistant.config_entries import ConfigSubentry
|
||||
from homeassistant.const import CONF_URL
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
|
||||
from homeassistant.helpers.entity import Entity, EntityDescription
|
||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||
@@ -39,7 +40,11 @@ class NtfyBaseEntity(Entity):
|
||||
name=subentry.title,
|
||||
configuration_url=URL(config_entry.data[CONF_URL]) / self.topic,
|
||||
identifiers={(DOMAIN, f"{config_entry.entry_id}_{subentry.subentry_id}")},
|
||||
via_device=(DOMAIN, config_entry.entry_id),
|
||||
via_device_id=dr.async_get_device_id_by_identifier(
|
||||
config_entry.runtime_data.account.hass,
|
||||
(DOMAIN, config_entry.entry_id),
|
||||
config_entry_id=config_entry.entry_id,
|
||||
),
|
||||
)
|
||||
self.ntfy = config_entry.runtime_data.account.ntfy
|
||||
self.config_entry = config_entry
|
||||
@@ -60,10 +65,4 @@ class NtfyCommonBaseEntity(CoordinatorEntity[BaseDataUpdateCoordinator]):
|
||||
super().__init__(coordinator)
|
||||
self.entity_description = description
|
||||
self._attr_unique_id = f"{coordinator.config_entry.entry_id}_{description.key}"
|
||||
self._attr_device_info = DeviceInfo(
|
||||
entry_type=DeviceEntryType.SERVICE,
|
||||
manufacturer="ntfy LLC",
|
||||
model="ntfy",
|
||||
configuration_url=URL(coordinator.config_entry.data[CONF_URL]) / "app",
|
||||
identifiers={(DOMAIN, coordinator.config_entry.entry_id)},
|
||||
)
|
||||
self._attr_device_info = coordinator.device_info
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
"""Tests for the ntfy integration."""
|
||||
|
||||
from unittest.mock import AsyncMock
|
||||
from unittest.mock import AsyncMock, patch
|
||||
|
||||
from aiontfy.exceptions import (
|
||||
NtfyConnectionError,
|
||||
@@ -10,8 +10,11 @@ from aiontfy.exceptions import (
|
||||
)
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.ntfy.const import DOMAIN
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
@@ -33,6 +36,40 @@ async def test_entry_setup_unload(
|
||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("mock_aiontfy")
|
||||
async def test_topic_device_via_device(
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
) -> None:
|
||||
"""Test topic device is linked to the account service device.
|
||||
|
||||
The account service device is registered at setup so the topic device,
|
||||
which is created by the event/notify platforms, is always linked to it
|
||||
even when the sensor platform (which also creates the account device) is
|
||||
not loaded.
|
||||
"""
|
||||
|
||||
with patch("homeassistant.components.ntfy.PLATFORMS", [Platform.EVENT]):
|
||||
config_entry.add_to_hass(hass)
|
||||
assert await hass.config_entries.async_setup(config_entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
subentry_id = next(iter(config_entry.subentries))
|
||||
account_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, config_entry.entry_id), config_entry.entry_id
|
||||
)
|
||||
topic_device = device_registry.async_get_device_by_identifier(
|
||||
(DOMAIN, f"{config_entry.entry_id}_{subentry_id}"), config_entry.entry_id
|
||||
)
|
||||
|
||||
assert account_device is not None
|
||||
assert topic_device is not None
|
||||
assert topic_device.via_device_id == account_device.id
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("exception", "state"),
|
||||
[
|
||||
|
||||
Reference in New Issue
Block a user