From bbe00ef79eb363ea2db0a45e41a72006e4449dd5 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 6 May 2026 14:29:47 +0200 Subject: [PATCH] De-duplicate code to build Tuya device info (#169899) --- homeassistant/components/tuya/coordinator.py | 10 ++-------- homeassistant/components/tuya/entity.py | 16 +++------------- homeassistant/components/tuya/util.py | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/homeassistant/components/tuya/coordinator.py b/homeassistant/components/tuya/coordinator.py index 31cc158a3ea..6b8cf501d4f 100644 --- a/homeassistant/components/tuya/coordinator.py +++ b/homeassistant/components/tuya/coordinator.py @@ -28,6 +28,7 @@ from .const import ( TUYA_DISCOVERY_NEW, TUYA_HA_SIGNAL_UPDATE_ENTITY, ) +from .util import get_device_info type TuyaConfigEntry = ConfigEntry[DeviceListener] @@ -145,14 +146,7 @@ class DeviceListener(SharingDeviceListener): device_registry.async_get_or_create( config_entry_id=self._entry.entry_id, - identifiers={(DOMAIN, device.id)}, - manufacturer="Tuya", - name=device.name, - # Note: the model is overridden via entity.device_info property - # when the entity is created. If no entities are generated, it will - # stay as unsupported - model=f"{device.product_name} (unsupported)", - model_id=device.product_id, + **get_device_info(device, initial=True), ) def remove_device(self, device_id: str) -> None: diff --git a/homeassistant/components/tuya/entity.py b/homeassistant/components/tuya/entity.py index 81e94870f93..b00bbbc0d4d 100644 --- a/homeassistant/components/tuya/entity.py +++ b/homeassistant/components/tuya/entity.py @@ -5,11 +5,11 @@ from typing import Any from tuya_device_handlers.device_wrapper import DeviceWrapper from tuya_sharing import CustomerDevice, Manager -from homeassistant.helpers.device_registry import DeviceInfo from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity import Entity, EntityDescription -from .const import DOMAIN, LOGGER, TUYA_HA_SIGNAL_UPDATE_ENTITY +from .const import LOGGER, TUYA_HA_SIGNAL_UPDATE_ENTITY +from .util import get_device_info class TuyaEntity(Entity): @@ -25,6 +25,7 @@ class TuyaEntity(Entity): description: EntityDescription, ) -> None: """Init TuyaEntity.""" + self._attr_device_info = get_device_info(device) self._attr_unique_id = f"tuya.{device.id}{description.key}" self.entity_description = description # TuyaEntity initialize mq can subscribe @@ -32,17 +33,6 @@ class TuyaEntity(Entity): self.device = device self.device_manager = device_manager - @property - def device_info(self) -> DeviceInfo: - """Return a device description for device registry.""" - return DeviceInfo( - identifiers={(DOMAIN, self.device.id)}, - manufacturer="Tuya", - name=self.device.name, - model=self.device.product_name, - model_id=self.device.product_id, - ) - @property def available(self) -> bool: """Return if the device is available.""" diff --git a/homeassistant/components/tuya/util.py b/homeassistant/components/tuya/util.py index 824ff6bc91d..9743b8a1e64 100644 --- a/homeassistant/components/tuya/util.py +++ b/homeassistant/components/tuya/util.py @@ -3,6 +3,7 @@ from tuya_sharing import CustomerDevice from homeassistant.exceptions import ServiceValidationError +from homeassistant.helpers.device_registry import DeviceInfo from .const import DOMAIN, DPCode @@ -31,3 +32,22 @@ class ActionDPCodeNotFoundError(ServiceValidationError): "available": str(sorted(device.function.keys())), }, ) + + +def get_device_info(device: CustomerDevice, *, initial: bool = False) -> DeviceInfo: + """Get device info.""" + model = device.product_name + + if initial: + # Note: the model is overridden via entity.device_info property + # when the entity is created. If no entities are generated, it will + # stay as unsupported + model = f"{device.product_name} (unsupported)" + + return DeviceInfo( + identifiers={(DOMAIN, device.id)}, + manufacturer="Tuya", + name=device.name, + model=model, + model_id=device.product_id, + )