How to add devices to Device Registry?

I’m trying to get all of my devices to show up in the ‘Device Registry’ (Configuration > Devices). Some do, some don’t. My first target is getting the Tuya devices added.

Reading the developer documentation on the device registry it looks like all I need to do is add a ‘device info’ property to the component and/or platform inside the class for the new entity. I’ve done that for the Tuya component (in both the integration class ‘init.py’ and in the platform definition ‘light.py’). The component loads and I can see/control the entities. I even verified that the ‘device info’ property was called by reporting to the logger. But the devices don’t show up in the Device Registry page.

Here’s my code. I was going for a proof of concept, so I hard-coded most of the values.

class TuyaDevice(Entity):
    """Tuya base device."""

    @property
    def device_info(self):
        _LOGGER.warning("device_info called for tuya.")
        return {
            'identifiers': {
                ("tuya", self.unique_id)
            },
            'name': self.name,
            'manufacturer': "tuya manufacturer",
            'model': "tuya light model",
            'sw_version': "3.14",
        }

    def __init__(self, tuya):

What is required to set up a device in the registry? Why don’t all components add their devices already?

I’m just getting started here so, please, feel free to over-explain things. :slight_smile:

2 Likes

After reading the documentation in more detail, I found the answer to my question.

To make add devices to the device registry, they have to be set up via a “config entry” (not the same as an entry in configuration.yaml) config entries are sent to the async_setup_entry function in the component’s init.py file and are forwarded to each platform implemented by the component.

Furthermore, in order to create a “config entry” for an integration, it must be created through the UI. Which means that they component needs to have a “config flow” so that the user can set up the integration from the integrations page.

I’ve roughly implemented a solution for the Tuya component, but it’s missing some features right now. If you have any follow-up questions, just reply here and I’ll share what I’ve learned.

2 Likes

Thank you for your really good explanation.

Can you please elaborate more on your sentence “Furthermore, in order to create a “config entry” for an integration, it must be created through the UI. Which means that they component needs to have a “config flow” so that the user can set up the integration from the integrations page.”?

I am planning to add some entry in device registery for my device…

Thanks in Advance.

Sure. Basically, the developer of the component needs to add a “config flow” file to the component source code so that users can set it up via the home assistant integrations page UI. Some of the component developers have done this, but many haven’t. If there is not a “config flow” file in the component, then the only way to set up the component is through the config.yaml file and entities added via config.yaml are not registered as devices. At this point, it’s not possible to manually register an entity from config.yaml (or group of entities) as a device.

Did that answer your question?

I created a custom branch of the tuya component which includes a config flow file (https://github.com/ztrglider/tuya_withConfigFlow). If you want to adapt a component yourself, I found the Hue component was a great reference. Based on the fact that a intermediate like me was able to figure out how to do it, I don’t know why so many components are missing them.

Ok, I understood it now. But I don’t have a custom_component directory in config of hass.io. Do we have to create one ourself?

And I am facing one issue which is somewhat related to this topic. I have a Esp12E device and developed a code using Arduino. I am using Mosquitto mqtt broker addon. Discovery is enabled. Also json data is sent for discovery on config topic and I can see the switch in “Configuration>Entities” and can get to toggle button to switch on and off. But gets error “This entity do have unique_id, therefore it’s settings cannot be managed from the UI”. That is why I cannot see it in Mqtt broker integration. Can you help me with this?
Do I need to create config flow file for this?
I am bit confused. The switch is working fine but need to set unique_id for this!

Thanks in advance.

For your mqtt device it is easy, but first 2 questions :
1/ do you use discovery or did you setup your device in configuration.yaml
2/ from your question, I guess you use discovery with a self-crafted discovery message. Can you post it ?

  1. I am using discovery and no manual configuration.
  2. The json data sent over the Config_topic: “homeassistant/switch/water_tank_switch/config” is:

{
“name”:“Water_Tank_Switch”,
“command_topic”:“homeassistant/switch/Water_tank_switch/set”,
“state_topic”:“homeassistant/switch/Water_tank_switch/state”,
“availability_topic”:“homeassistant/switch/Water_tank_switch/available”,
“optimistic”:“false”,
“retain”:“true”,
“qos”:“0”
}

try :

{
“name”:“Water_Tank_Switch”,
“command_topic”:“homeassistant/switch/Water_tank_switch/set”,
“state_topic”:“homeassistant/switch/Water_tank_switch/state”,
“availability_topic”:“homeassistant/switch/Water_tank_switch/available”,
“optimistic”:“false”,
“retain”:“true”,
“qos”:“0”,
"unique_id": "Water_tank_switch",
"device": {"identifiers": ["Water_tank_switch"],"name": "Water_tank_switch", "model": "DIY", manufacturer": "DIY"}
}
1 Like

Ok Now I can see the switch in Mqtt broker integration, but it disappeared from “Configuration>Entities” and so I am not getting the toggle switch now.
Do I need to send entity_id in the json payload of discovery?
So the improved json format should be like this:

{
“name”:“Water_Tank_Switch”,
“command_topic”:“homeassistant/switch/Water_tank_switch/set”,
“state_topic”:“homeassistant/switch/Water_tank_switch/state”,
“availability_topic”:“homeassistant/switch/Water_tank_switch/available”,
“optimistic”:“false”,
“retain”:“true”,
“qos”:“0”,
“unique_id”: “Water_tank_switch”,
“device”: {“identifiers”: [“Water_tank_switch”],“name”: “Water_tank_switch”, “model”: “DIY”, manufacturer": “DIY”}
"entities: {“device_class”:“switch”,“original_name”:“Water_tank_switch”,“entity_id”:“switch.water_tank_switch”,“original_icon”:“mdi:water-pump”,“platform”:“mqtt”}
}

Thanks in advance.

Also just need to inform you that I can see the newly added switch entry in “core.device_registry” file but cannot see the switch entry in “core.entity_registry”.
So thought of adding entity data in json payload.

Thanks.

Ok, I get the toggle switch now along with the switch in mqtt integration. Simply changed the “unique_id” name and “device identifier” to “Water_tank_switch1”. So here is the actual json payload to be sent on discovery:

{
“name”:“Water_Tank_Switch”,
“command_topic”:“homeassistant/switch/Water_tank_switch/set”,
“state_topic”:“homeassistant/switch/Water_tank_switch/state”,
“availability_topic”:“homeassistant/switch/Water_tank_switch/available”,
“optimistic”:“false”,
“retain”:“true”,
“qos”:“0”,
“unique_id”: “Water_tank_switch1”,
“device”: {“identifiers”: [“Water_tank_switch1”],“name”: “Water_tank_switch”, “model”: “DIY”, manufacturer": “DIY”}
}

Thanks.

I have a similar problem. My setup consists of Ikea (mostly lights) and Xiaomi (sensors and buttons and plugs).
I have a few Xiaomi wifi/zgbee plugs which control lights, and without having those as “Devices” i cannot use Scenes. Scenes would make it much easier and nicer to create automations for lighting.
those switches are created as yaml constructs
-switch

  • platform: xiaomi_miio
    name: ‘misocket1’

As xiaomi hubs also do not have “intergrations” (only yaml based config), the Xiaomi buttons also have to be used with json code which makes creating automation really painful. There is no viable alternative to Xiaomi for me, not to mention investments already made.
Ikea has an “integration” and all lights show up as “Devices”.

Am i right that without waiting for developers to create integrations for Xiaomi hubs (and for wifi plugs it will probably never happen as they are not that common) there is basically no way to create those “devices” manually in any reasonable way?

Where did you get that idea from ? Scenes don’t need devices, they need entities.

I cannot see an option to use entities in scenes, only devices as show on screenshot. I’m experimenting with the latest version.

Strange.

Go to your user profile page (initials in sidebar) and turn on the Advanced Mode toggle. Then you should be able to see that.

Gee, that thing was so well hidden! :slight_smile: I wouldn’t have though to look there ever. Hass is definitely getting more user-oriented.
Thanks for the hint, now I have entities in scenes which should make it a bit easier. It also brought back “check config” button which i was badly missing (should’ve googled that one first)

some tips how to enable the ‘advanced mode’

Dear all
I have the tuya integration which is working well.

I add a new device through the application smart but this new device does not appear in hassio ?

Could you help me

What device did you add in smart life? Not all devices are supported by the Tuya integration.

Hi all,

I am using mqtt discovery, sending config data from ESP12e to hassio.
Home Assistant OS 5.9 running on Raspi 4 model B.
config_topic: homeassistant/sensor/wts_dist/config
state_topic: homeassistant/sensor/wts_dist/state

When subscribed with # topic in Configuration>>Integration>>Mqtt_broker>>Configure>>Subscribe, I am getting json data on config_topic as follows:

{
“name”: “Water_Tank_Sensor_D”,
“dev_cla”: “None”,
“stat_t”: “homeassistant/sensor/wts_dist/state”,
“avty_t”: “homeassistant/sensor/wts_dist/available”,
“pl_avail”: “online”,
“pl_not_avail”: “offline”,
“unit_of_meas”: “%”,
“val_tpl”: “{{ value_json.filledcap}}”,
“ic”: “mdi:water-pump”,
“uniq_id”: “Water_Tank_Sensor_D-2C_3A_11_11_11_11”,
“dev”: {“ids”: [“Water_Tank_Sensor_D-2C_3A_11_11_11_11”],“name”: “Water_Tank_Sensor_D”,“mdl”: “NodeMCU 1.0 (ESP12E module)”,“mf”: “Amica”,“sw”: “v0.1”}
}

But, the device is not getting added to core.device_registry & core.entity_registry.
mqtt_discovery is set to true, confirmed in core.config_entries. Earlier it was working perfectly, has anything changed with new hass os version 5.9?

Can any once help me on this?
@francisp do you have a solution for this?

Thanks in advance.