Hi community,
I’m very new to home assistant development and also very new to python itself. I’m used to program in php for web applications. Now I’m building my first custom integration and need some advice on best practices in handling dependencies between multiple entities. For example I have one “main” entity which is observing mqtt messages. It has a register_callback-function and can receive functions to call whenever it receives a mqtt message. Now I have multiple sensor entites which shall receive updates by mqtt so they need to know the mqtt entity in order to register their callbacks.
I currently have a basic init.py where I use async_forward_entry_setups to register all the entities. So mqtt and sensors are in different files and the entity are created via the async_add_entities function.
What is the usual, best practice way to access my mqtt entity from my sensors? I’d like to register a callable function from my sensors to the mqtt entity which should be called each time it’s receiving a message.
Easy way would be, create the sensor entities inside my mqtt file in the same async_setup_entry function where all objects are initiated and known to each other. But I think that’s a bad practice and initiation should be in each corresponding sensor file itself. But now how do I handle dependencies from one entity to another one?
Thanks for your advice.