Ecowater softener -> how to integrate this python script?

So after going further and further with my HA installation, I managed to convert the python script from Kyle Johnson to an AppDaemon app :slight_smile:

Here is the code of the ecowater.py file (to be placed in the ‘appdaemon\apps’ folder) :

import mqttapi as mqtt
import datetime
from datetime import timedelta
import requests
import json
import re

#
# Retrieve data from EcoWater interface and publish to MQTT
#
# Args: ecowater_minutes, ecowater_dsn, ecowater_email, ecowater_password
#
# Credits : Thanks to Kyle Johnson for the original code (https://www.gnulnx.net/2020/02/18/ecowater-api-scraping/)
#
# Converted to AppDaemon by kalhimeo
#

class EcoWater(mqtt.Mqtt):

  def initialize(self):

    # Run every X minutes
    self.run_every(self.run_parsing, datetime.datetime.now() + timedelta(seconds=3), self.args["ecowater_minutes"] * 60)
	
  def run_parsing(self, kwargs):
	
    # Regex to match the hidden input on the initial log in page
    request_validation_re = re.compile(r'<input name="__RequestVerificationToken" type="hidden" value="(.*?)" />')

    # The serial number of your ecowater device
    dsn = { "dsn": self.args["ecowater_dsn"], }

    # The initial form data
    payload = {
        "Email" : self.args["ecowater_email"],
        "Password" : self.args["ecowater_password"],
        "Remember" : 'false'
    }
	
    # The headers needed for the JSON request
    headers = {
        'Accept': '*/*',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language' : 'en-US,en;q=0.5',
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0'
    }

    with requests.Session() as s:
        # Initial GET request
        g = s.get('https://www.wifi.ecowater.com/Site/Login')

        # Grab the token from the hidden input
        tokens = request_validation_re.findall(g.text)

        # Add the token to the form data payload
        payload['__RequestVerificationToken'] = tokens[0]

        # Log in to the site
        login = s.post('https://www.wifi.ecowater.com/Site/Login', data=payload)

        # Add the correct Referer header
        headers['Referer'] = login.url + '/' + dsn['dsn']

        # Query the JSON endpoint for the data that we actually want
        data = s.post('https://www.wifi.ecowater.com/Dashboard/UpdateFrequentData', data=dsn, headers=headers)

        # Load the data in to json
        jsonv = json.loads(data.text)

        # Delete the elements that we don't want
        del jsonv['water_units']
        del jsonv['time']

        # Publish each piece of json data in to mqtt
        for d in jsonv:
            self.mqtt_publish('ecowater/' + d, jsonv[d])

You need to add the app in the apps.yaml and provide some parameters :

EcoWater:
  module: ecowater
  class: EcoWater
  ecowater_minutes: 60
  ecowater_dsn: 'your_ecowater_serial_number'
  ecowater_email: 'your_email_account_for_ecowater_portal'
  ecowater_password: 'your_password_for_ecowater_portal'

You can then use the MQTT data as sensors and binary sensors in HA, example :

binary_sensor:
- platform: mqtt
 name: Ecowater Online
 state_topic: "ecowater/online"
 payload_on: True
 payload_off: False

sensor:
- platform: mqtt
 name: Ecowater Salt Level
 state_topic: "ecowater/salt_level"

Available binary sensors : online, recharge, out_of_salt
Available sensors : salt_level, salt_level_percent, water_today, water_avg, water_avail, water_flow, out_of_salt_days

That’s it , have fun !

1 Like