1
0
mirror of https://github.com/home-assistant/core.git synced 2026-05-26 18:26:25 +01:00

De-duplicate code to build Tuya device info (#169899)

This commit is contained in:
epenet
2026-05-06 14:29:47 +02:00
committed by GitHub
parent 7f447abc3a
commit bbe00ef79e
3 changed files with 25 additions and 21 deletions
+2 -8
View File
@@ -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:
+3 -13
View File
@@ -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."""
+20
View File
@@ -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,
)