How I can get DeviceEntry by entity_id in python?

I get hass: core.HomeAssistant in my custom component. I know, what device can have many entities. I know entity, for example, media_player.nexus_player, how I can get DeviceEntry? I need it for manufacturer, sw_version and another info.

I believe that you can get it by first retrieving the entity from the entity registry, extract device_id and use that to pull the device from the device registry. Some kind of pseudo code (not tested, but it gives the idea):

from homeassistant.helpers import entity_registry as er
from homeassistant.helpers import device_registry as dr

entity_id = "sensor.foobar"

entity_reg = er.async_get_registry(hass)
entry = entity_reg.async_get(entity_id)

dev_reg = dr.async_get_registry(hass)
device = dev_reg.async_get(entry.device_id)
print(device.model)

References:


2 Likes