tomasmcguinness / homeassistant-mixergy

Add support for Mixergy's smart water tank into Home Assistant
MIT License
37 stars 8 forks source link

Switch to push rather than poll for entity state #48

Open mattjgalloway opened 8 months ago

mattjgalloway commented 8 months ago

I noticed that the Mixergy API supports push by connecting through a Websocket to Mixergy's STOMP API. I've taken a Quick Look and I think we can switch over to using it fairly easily. This would mean the integration would be a push based one rather than polling. It's better practice, plus we'd get updates immediately if you make changes to settings or states in the Mixergy app or website, and you'd get measurement information straight away after it's ready rather than only when the integration updates.

@tomasmcguinness what do you think here? I'm happy to start looking at the changes, but before I embark I wanted to get your thoughts on if this sounds sensible!

tomasmcguinness commented 8 months ago

How did you even find that out?? :)

Moving to push would be great. I'm not a fan of long polling, especially with a mostly static thing like a hot water tank.

My one concern with switching to web sockets is an increase in the number of constant connections the Mixergy servers have to handle, which they won't have accounted for.

Sure, being polled every 30 seconds by a few hundred integrations isn't something they've accounted for either, but I'm conscious of it all the same.

Let me drop a note to their technical team and see what they say.

DirkFace commented 8 months ago

You two are cooking on gas (or maybe electricity, or solar, lol). Anyway - thank you. Appreciate the work & ideas you two are doing with this project.

mattjgalloway commented 8 months ago

@tomasmcguinness i contacted support and they gave me a document that documents the API. And has some sample Python too. It’s quite neat. You should ask for it too.

Good idea on asking if the load will be ok. But to be honest I doubt it’ll be massive. And maintaining websocket connections is probably a little more predictable than polling every 30 seconds anyway.

tomasmcguinness commented 8 months ago

You must have a more up-to-date document than me :)

I got one a few years back and after two years of asking for an updated one, I just stopped.

If you want to get cracking on moving to web sockets, please do! Forgiveness > Permission in the case I think :)

mattjgalloway commented 8 months ago

You must have a more up-to-date document than me :)

Let me know if you can't get it from them and I can send to you by email if you'd like!

If you want to get cracking on moving to web sockets, please do! Forgiveness > Permission in the case I think :)

Consider me getting cracking :). I've got a working prototype now. It's going to change a tonne of the code, so want to make sure I get it all right!

tomasmcguinness commented 7 months ago

Hi Matt.

Are we ready to merge this in?

mattjgalloway commented 7 months ago

@tomasmcguinness - sorry, not quite yet actually. I found it sometimes gets stuck if the internet drops. It's quite odd to be honest, and I'm learning all about how to run async tasks in HA in the process of figuring it out. Where I'm a bit stuck at the moment is the process of trying to reconnect just stalls as though the task I spawn is just stuck. I've even been wrapping in a timeout async block, but that doesn't help and it never times out. I'll keep going and see if I can figure it all out.

tomasmcguinness commented 7 months ago

Thanks Matt.

I'll switch my own installation over to this and see what I can find out.

mattjgalloway commented 7 months ago

Thanks Tom! I haven't had chance the last few weeks to keep debugging as work and home life were both crazy busy. But should be able to get some time next week.

michaelmarconi commented 1 month ago

Hey @mattjgalloway, did you ever get anywhere with this?

mattjgalloway commented 1 month ago

@michaelmarconi i haven’t had chance to look back at it yet.

It all works, but I found when the network dropped, things got stuck and wouldn’t recover.

tomasmcguinness commented 1 month ago

I haven't paid any attention to this as I haven't made the time.

I'll try and experiment this over the next few weeks. I can hopeful use docker to drop connections etc. and see what happens.