1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-08 17:49:37 +01:00

Read Tuya device info from quirk (#169888)

This commit is contained in:
epenet
2026-05-07 11:28:11 +02:00
committed by GitHub
parent 427758ef15
commit ce5f2330eb
2 changed files with 123 additions and 3 deletions
+14 -3
View File
@@ -1,5 +1,6 @@
"""Utility methods for the Tuya integration."""
from tuya_device_handlers import TUYA_QUIRKS_REGISTRY
from tuya_sharing import CustomerDevice
from homeassistant.exceptions import ServiceValidationError
@@ -36,7 +37,9 @@ class ActionDPCodeNotFoundError(ServiceValidationError):
def get_device_info(device: CustomerDevice, *, initial: bool = False) -> DeviceInfo:
"""Get device info."""
model = device.product_name
manufacturer = "Tuya"
model: str | None = device.product_name
model_id: str | None = device.product_id
if initial:
# Note: the model is overridden via entity.device_info property
@@ -44,10 +47,18 @@ def get_device_info(device: CustomerDevice, *, initial: bool = False) -> DeviceI
# stay as unsupported
model = f"{device.product_name} (unsupported)"
if (
quirk := TUYA_QUIRKS_REGISTRY.get_quirk_for_device(device)
) and quirk.manufacturer:
# If the manufacturer is not set, we cannot trust the model/model_id
manufacturer = quirk.manufacturer
model = quirk.model
model_id = quirk.model_id
return DeviceInfo(
identifiers={(DOMAIN, device.id)},
manufacturer="Tuya",
manufacturer=manufacturer,
name=device.name,
model=model,
model_id=device.product_id,
model_id=model_id,
)
+109
View File
@@ -2,6 +2,7 @@
from unittest.mock import MagicMock, patch
import pytest
from syrupy.assertion import SnapshotAssertion
from tuya_device_handlers import TUYA_QUIRKS_REGISTRY
from tuya_sharing import CustomerDevice, Manager
@@ -14,6 +15,7 @@ from homeassistant.components.tuya.const import (
DOMAIN,
)
from homeassistant.components.tuya.diagnostics import _REDACTED_DPCODES
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
from homeassistant.helpers import device_registry as dr, entity_registry as er
@@ -141,6 +143,113 @@ async def test_device_registry(
)
@pytest.mark.parametrize(
("mock_device_code", "platforms", "manufacturer", "model", "model_id", "quirks"),
[
# Ensure model is suffixed with "(unsupported)" when no entities
# are generated
(
"mal_gyitctrjj1kefxp2",
[],
"Tuya",
"Multifunction alarm (unsupported)",
"gyitctrjj1kefxp2",
{},
),
# Ensure model is not suffixed with "(unsupported)" when entities
# are generated
(
"mal_gyitctrjj1kefxp2",
[Platform.ALARM_CONTROL_PANEL],
"Tuya",
"Multifunction alarm",
"gyitctrjj1kefxp2",
{},
),
# With a quirk that has manufacturer, model and model_id are
# taken from quirk (and not suffixed with "(unsupported)" even if
# no entities are generated)
(
"mal_gyitctrjj1kefxp2",
[],
"My manufacturer",
"Amazing model",
"AMA-ZING1",
{
"gyitctrjj1kefxp2": MagicMock(
manufacturer="My manufacturer",
model="Amazing model",
model_id="AMA-ZING1",
)
},
),
# With a quirk that has manufacturer, model and model_id are
# taken from quirk (even if None)
(
"mal_gyitctrjj1kefxp2",
[],
"My manufacturer",
None,
None,
{
"gyitctrjj1kefxp2": MagicMock(
manufacturer="My manufacturer",
model=None,
model_id=None,
)
},
),
# With a quirk that has null manufacturer, model and model_id
# are ignored
(
"mal_gyitctrjj1kefxp2",
[],
"Tuya",
"Multifunction alarm (unsupported)",
"gyitctrjj1kefxp2",
{
"gyitctrjj1kefxp2": MagicMock(
manufacturer=None,
model="Amazing model",
model_id="AMA-ZING1",
)
},
),
],
)
async def test_device_registry_with_quirk(
hass: HomeAssistant,
mock_manager: Manager,
mock_config_entry: MockConfigEntry,
mock_device: CustomerDevice,
device_registry: dr.DeviceRegistry,
platforms: list[Platform],
manufacturer: str,
model: str | None,
model_id: str | None,
quirks: dict[str, MagicMock],
) -> None:
"""Validate device information with and without quirks."""
with (
patch.dict(TUYA_QUIRKS_REGISTRY._quirks, quirks, clear=True),
patch("homeassistant.components.tuya.coordinator.register_tuya_quirks"),
patch("homeassistant.components.tuya.PLATFORMS", platforms),
):
await initialize_entry(hass, mock_manager, mock_config_entry, mock_device)
device_registry_entries = dr.async_entries_for_config_entry(
device_registry, mock_config_entry.entry_id
)
assert len(device_registry_entries) == 1
device_registry_entry = device_registry_entries[0]
assert device_registry_entry.manufacturer == manufacturer
assert device_registry_entry.model == model
assert device_registry_entry.model_id == model_id
assert device_registry_entry.name == "Multifunction alarm"
@patch.object(
TUYA_QUIRKS_REGISTRY,
"initialise_device_quirk",