ChaseDurand / Pool-Pi

Hardware+software solution for controlling a Goldline/Hayward Aqualogic pool control board over WiFi via a Raspberry Pi.
Other
9 stars 7 forks source link

Integration into Home Assistant? #11

Open blulite opened 4 months ago

blulite commented 4 months ago

This is GREAT stuff, thanks! Is there any development still going on with this project? I am connecting this solution to Home Assistant as it is the most reliable solution (putting the PI/serial connection by the pool instead of remote Serial-IP stuff).

I've coded some MQTT into the Python code to get the status to HA, and that is working. I am developing communication so that HA can switch on/off pool resources). Where is the best place to turn things on and off? I can subscribe to the MQTT topic and turn it on/off in Python, but I thought it might be interesting to make an HTTP call directly from HA (HTTP://poolpi:5000/lights_on). I only follow some Java stuff, but I thought I would ask before I try to figure it out.

Jeff

ChaseDurand commented 4 months ago

Hi Jeff! Thank you for the kind words and your interest. While there is no active development, my system has been running without issue on the side of my home. Further improvements I'd like to make but don't have any schedule for are easier customization of auxiliary names, support for variable speed pumps, and support for older P-4 remotes.

I'm away from my computer so any code I write below is untested, but I think the easiest way to get HA talking back to the controller is to have HA push commands to MQTT and read them in src/pool-pi.py getCommand. Right now this is done with a Redis server which takes messages from the web interface but it should work the same with MQTT. The commands are json with the resource id and modelVersion to ensure the user-facing state and actual state of the system are in sync. I've never used MQTT but I think it would be straightforward to send a message with those two values from HA and subscribe+parse them here in python.

For the HTTP endpoint approach, I think you would modify src/web.py to add in the api routes and then publish the command to the Redis server to be parsed in getCommand. If the JSON in your POST from HA matches what getCommand expects, you could pass it through like this:

@app.route('/api', methods=['POST'])
def HACommand():
    r.publish("inbox", request.get_json())
    return "", 204

or individually by command:

@app.route('/api/lights', methods=['POST'])
def HACommandLights():
    modelVersion = request.get_json()
    #form message with id and modelVersion
    message = "{ 'id': lights, 'modelVersion': "+ modelVersion + " }"
    r.publish("inbox", json.dumps(message))
    return "", 204

and then repeated for all commands. I'm not sure the advantage the second option gives you with this situation considering all of my command messages follow the same form.

Another note is that all of the commands (except menu buttons) sent over RS485 to the OEM board act as toggles. If you want to explicitly turn the lights off, you can't send a "lights off" command, so you have to check the state of the lights to figure out if you need to send a command or not, and similarly check the state after sending a command to ensure it went through (and didn't have a collision on the RS485 bus) before sending an additional attempt (I already handle this). If you want to have a lights off command then the easiest way would be to implement the logic in HA to check if the current state matches, and send a toggle command if not.

I found a subset of commands from an older controller that do support separate on/off packets, so you can send without needing to check the state of the system or worry about accidentally sending two messages, but I don't use them in my code: Legacy On/Off Commands.

I should add that I don't know anything about API design, am away from a development machine, and am shooting from the hip with all of this.

Let me know if there is any other info I can provide! Chase