How to get the (parent) device of an entity?

Hey there,

I’m super duper new to HA development, plus: I’m not the most experienced Python dev: I program it in my freetime since couple of years, but unfortunately it’s not part my main job; I am a Software Engineer though, just not mainly a Python dev :wink: and HA really makes use of (feels like it) 100% of Pythons language features, so I sometimes struggle a bit to find the right things myself, especially if you have never done an actual project using Python asyncio.

My main question: I’m trying to submit my first, little improvement / maybe bugfix for a HA component: WLED.
I’m using WLED for a lot of LED strips I have in and outside of (X-Mas lighting) my apartment. One issue I have with the WLED component is that it chooses (imho) the wrong name when adding new entities.
If you’re not familiar with WLED: WLED is an awesome piece of software to run on a variety of microcontrollers to control (e.g.) LED strips. Two of its many abilities are:
a) Segmenting the LED strip into multiple segments, e.g. 5 segments of 20 LEDs each. And every segment is one entity in HA, so you can control it seperately.
b) Save and load presets: So e.g. have a preset where the strip is split into two segments, one where it’s split into five segments.

That said: If you have a preset active with two segments and then load one with five, the HA component needs to add entities for these other three (new) segments.
Imho, the current component chooses the wrong name for them, as for the base of the name (“BlaBla Segment X”, “BlaBla Segment Y”, etc…) it doesn’t use the HA device name, but always the actual name you have configured in WLED, so in the software / on the controller itself.
So I want to change it to use the HA device name as a base. But I struggle to get access to it in the current WLED component’s code. Strictly speaking about this line: https://github.com/home-assistant/core/blob/4ec81d4b6749932d3a998440972da0c554d264bb/homeassistant/components/wled/light.py#L164

I tried looking up other components, like the Yeelight component I also spent some time in already in the past, but I struggle to find it.

Could someone point me in the right direction, how to properly get an instance of the device these entities will be added for?

Thanks and sorry if I totally didn’t get the concept of this maybe :slight_smile:

Greetings,

Andy!

So I found a way to use the name of the ConfigEntry, which is not the exact same thing and not what I actually wanted, but one step in the right direction for me, as for me, I have both ConfigEntry and Device named equally :wink: https://github.com/gman-php/core/commit/5d889c4c6f16cea1f00498197236b7929f54e3d0

Definitely nothing I would ask to merge, as it’s logically wrong. But maybe somebody can still point me in the right direction how I can properly find the corresponding device for these entities :slight_smile: Or maybe I will find it myself

I finally found a way how to do it. It’s still weird that there is no method on an entity like get_device(), but that’s how I did It now:

device_registry = await hass.helpers.device_registry.async_get_registry()
device = device_registry.async_get_device({(DOMAIN, entry.data.get("mac"))}, set())

There’s a helper called the device_registry. It provides some methods to get a device by various things, like a device ID (async_get(), if you have it :wink: ), a config entry ID (async_entries_for_config_entry()) or search for it by various identifiers. These are usually the “domain” of the component and a unique key of that device, usually the mac address. That’s also how the WLED component does it: It creates the device using the “WLED” domain and it’s mac address. Hence I use that to also search for a device and I find it :slight_smile:

Hope I can help people in the future having the same weirdness.

I also used it in a fix I’m about to open a PR for: https://github.com/gman-php/core/commit/c3336131f58bda0281c4c40128d116478af1ce42