Hi, I’m upgrading an already implementing component (LCN) to an integration using the config_entry workflow. Unfortunately the data entry framework is not sufficient to get the whole configuration done. So I basically can only use it for setting up a hub. Attached to this hub are the system specific hardware devices which offer switches, sensors, and much more. The system does not offer any functionality to automatically detect the configuration of the connected hardware devices so this had to be done by editing the configuration.yaml
in the past.
All this led me to the development of a new frontend configuration panel explicitly for the LCN component (just like Zigbee or Z-Wave). What I can do at the moment is to scan the hub for connected hardware devices.
In the next step, I’d like to create the entities representing the sensors and switches connected to the hardware devices. At the moment it is only possible to input all the relevant parameters using the configuration panel and pass the information to Hass using the WebSocket API.
But how do I create the new entity (e.g. a switch) based on the parameters and add it to Hass while the system is running?
What I do at the moment to create the entity at runtime is basically the following (which is almost taken from the way it is done during platform setup):
entity = LcnSwitchEntity(hass, entity_config)
entity_component = hass.data['switch'] # EntityComponent for switch
entity_platform = component._platforms[config_entry.entry_id] # correspondig EntityPlatform for LCN
hass.async_add_job(entity_platform.async_add_entities([entity]))
Here LcnSwitchEntity
is the platform specific entity for a switch and entity_config
is a dictionary with the entitiy specific configuration.
Although this works like a charm I wonder if this is the intended way. For example there are two things I don’t like:
- obtaining the EntityComponent using the
hass.data
object - getting the EntityPlatform using the protected
_platforms
attribute
I would have expected that there are some helper methods to obtain the right EntityComponent / EntityPlatform or at least to add a new entity to a platform/component after setup. But I didn’t find anything in the code.
Did I miss something? Is there a better or intended way of adding entities at runtime?