arachnetech / homebridge-mqttthing

A plugin for Homebridge allowing the integration of many different accessory types using MQTT.
Apache License 2.0
462 stars 104 forks source link

Accessory-scoped state in apply functions #678

Closed jacoscaz closed 6 days ago

jacoscaz commented 6 days ago

Hello @arachnetech! First of all, thank you for this wonderful plugin. Brilliant work and it's making my life so much easier in pursuing my idea of a locally-managed smart home. That being said, would you be happy with a PR that extends the state parameter provided to apply functions with a state.accessory property referencing a state object scoped to the accessory itself?

apply functions are provided with a state parameter whose state.global property refers to a state object that seems to be scoped to the MQTT client / connection. However, I often find myself needing to share state across apply functions of the same accessory; this is something that can already be done by using the state.global object with per-accessory property names but that's a bit clunky. It would be better to just do state.accessory.foo = 'bar'. I think I have an idea of how to do it but I wanted to check in with you first before committing to it.

arachnetech commented 6 days ago

I may be misunderstanding, as it's been a while since I've done much to mqttthing (as I migrated my own home to HomeAssistant), but I think each accessory gets its own instance of mqttthing anyway and so global is per-accessory?

If not, what you're suggesting makes a lot of sense and I'm happy to accept a PR.

arachnetech commented 6 days ago

I may be misunderstanding, as it's been a while since I've done much to mqttthing (as I migrated my own home to HomeAssistant), but I think each accessory gets its own instance of mqttthing anyway and so global is per-accessory?

If not, what you're suggesting makes a lot of sense and I'm happy to accept a PR.

jacoscaz commented 6 days ago

@arachnetech I stand corrected! I I very likely misinterpreted a couple of lines of code - apologies for bothering you. I'll close this one for now and reopen if needed. Thank you for the super-quick feedback!