mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Don't allow creating device if config entry does not exist (#98157)
* Don't allow creating device if config entry does not exist * Fix test * Update test
This commit is contained in:
@@ -158,7 +158,7 @@ class DeviceInfoError(HomeAssistantError):
|
||||
|
||||
|
||||
def _validate_device_info(
|
||||
config_entry: ConfigEntry | None,
|
||||
config_entry: ConfigEntry,
|
||||
device_info: DeviceInfo,
|
||||
) -> str:
|
||||
"""Process a device info."""
|
||||
@@ -167,7 +167,7 @@ def _validate_device_info(
|
||||
# If no keys or not enough info to match up, abort
|
||||
if not device_info.get("connections") and not device_info.get("identifiers"):
|
||||
raise DeviceInfoError(
|
||||
config_entry.domain if config_entry else "unknown",
|
||||
config_entry.domain,
|
||||
device_info,
|
||||
"device info must include at least one of identifiers or connections",
|
||||
)
|
||||
@@ -182,7 +182,7 @@ def _validate_device_info(
|
||||
|
||||
if device_info_type is None:
|
||||
raise DeviceInfoError(
|
||||
config_entry.domain if config_entry else "unknown",
|
||||
config_entry.domain,
|
||||
device_info,
|
||||
(
|
||||
"device info needs to either describe a device, "
|
||||
@@ -527,6 +527,10 @@ class DeviceRegistry:
|
||||
device_info[key] = val # type: ignore[literal-required]
|
||||
|
||||
config_entry = self.hass.config_entries.async_get_entry(config_entry_id)
|
||||
if config_entry is None:
|
||||
raise HomeAssistantError(
|
||||
f"Can't link device to unknown config entry {config_entry_id}"
|
||||
)
|
||||
device_info_type = _validate_device_info(config_entry, device_info)
|
||||
|
||||
if identifiers is None or identifiers is UNDEFINED:
|
||||
@@ -550,11 +554,7 @@ class DeviceRegistry:
|
||||
)
|
||||
self.devices[device.id] = device
|
||||
# If creating a new device, default to the config entry name
|
||||
if (
|
||||
device_info_type == "primary"
|
||||
and (not name or name is UNDEFINED)
|
||||
and config_entry
|
||||
):
|
||||
if device_info_type == "primary" and (not name or name is UNDEFINED):
|
||||
name = config_entry.title
|
||||
|
||||
if default_manufacturer is not UNDEFINED and device.manufacturer is None:
|
||||
|
||||
Reference in New Issue
Block a user