Hello everyone,
After watching HA project for more than a year I decided that this is the home automation platform for me ( coming from ISY & Smartthings), And I would like to help and contribute as much as I can and learn from the other devs here what ever they can teach
So in the past week I’m working on integration for Aerogarden this is an hydroponic indoor garden that we use to grow harbs and vegetables, I have a working script in python3 that I wrote after taking a look how the IOS app works with fiddler in the middle, I started to play around with the basic sensor example and i ended up with the code below, I can see the sensors with data from the Aerogarden server but the state of the sensor is not being updated once the state is being updated on the IOS app or on the garden itself, I don’t have a lot of experience coding with classes so I’m sure I’m missing something very basic… anyway I would love to hear what you guys think or any comments in general!
Epotex
from homeassistant.helpers.entity import Entity
import logging
import urllib
import requests
import base64
##########
#some aerogarden static vars
DOMAIN = 'dev3'
agent = "BountyWiFi/1.1.13 (iPhone; iOS 10.3.2; Scale/2.00)"
port = "8080"
host = "http://ec2-54-86-39-88.compute-1.amazonaws.com:"
#API Calls
Login_call = "/api/Admin/Login"
SetDictPushCount = "/api/CustomData/SetDictPushCount?userID="
QueryUserDevice = "/api/CustomData/QueryUserDevice"
GetUserSetted = "/api/CustomData/GetUserSetted"
QueryDeviceOnline = "/api/CustomData/QueryDeviceOnline"
QueryDeviceStatus = "/api/CustomData/QueryDeviceStatus"
UpdateDeviceConfig ="/api/CustomData/UpdateDeviceConfig"
##########
def base64decode(b):
return base64.b64decode(b).decode('utf-8')
def setup_platform(hass, base_config, add_devices, discovery_info=None):
#getting config vars
encoded_email = urllib.parse.quote(base_config['mail'])
encoded_password = urllib.parse.quote(base_config['password'])
encoded_mac = urllib.parse.quote(base_config['aerogarden_mac_address'])
#building the auth data
auth_data = "mail=" + encoded_email + "&userPwd=" + encoded_password
apiurl = str(host) + str(port) + str(Login_call)
#creating IOS header
headers = {
'User-Agent': 'BountyWiFi/1.1.13 (iPhone; iOS 10.3.2; Scale/2.00)',
"Content-Type": "application/x-www-form-urlencoded",
"Connection": "keep-alive",
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate"
}
try:
#building the post request
r = requests.post(apiurl, data=auth_data, headers=headers)
responce = r.json()
#userID =responce["code"]
userID = responce["code"]
device_url = "airGuid=" + encoded_mac + "&userID=" + str(userID)
apiurl = str(host) + str(port) + str(QueryDeviceStatus)
#requesting data
r = requests.post(apiurl, data=str(device_url), headers=headers)
garden_data = r.json()
#extracted info
totalDays = garden_data['plantedDay']
nutriRemindDay = garden_data['nutriRemindDay']
""" Available stats from Aerogarden
lightStat = garden_data['lightStat']
pumpStat = garden_data['pumpStat']
clock = garden_data['clock']
airGuid = garden_data['airGuid']
lightCycle = garden_data['lightCycle']
pumpCycle = garden_data['pumpCycle']
lightTemp = garden_data['lightTemp']
config_id= garden_data['configID']
pumpHydro = garden_data['pumpHydro']
pumpRemind4Hour = garden_data['pumpRemind4Hour']
plantedType = garden_data['plantedType']
garden_name =base64decode(garden_data['plantedName'])
plantedDay = garden_data['totalDay']
alarmAllow = garden_data['alarmAllow']
plantedDate = garden_data['plantedDate']
nutrientDate = garden_data['nutrientDate']
updateDate = garden_data['updateDate']
createDate = garden_data['createDate']
swVersion = garden_data['swVersion']
hwVersion = garden_data['hwVersion']
bwVersion = garden_data['bwVersion']
oldPlantedDay = garden_data['oldPlantedDay']
deviceID = garden_data['deviceID']
deviceIP = garden_data['deviceIP']
"""
# Adding Nutrition reminder days sensor
add_devices([add_Sensor("Nutrition Reminder Day", nutriRemindDay, "Days")])
# Adding Total days planted sensor
add_devices([add_Sensor("Total Days Planted", totalDays, "Days")])
except RequestException:
_LOGGER.exception("Error communicating with AeroGarden")
return False
class add_Sensor(Entity):
"""Representation of a Sensor."""
def __init__(self, s_name, parm, status):
self.s_name = s_name
self.parm = parm
self.status = status
"""Initialize the sensor."""
self._state = None
@property
def name(self):
"""Return the name of the sensor."""
return self.s_name
@property
def state(self):
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return self.status
def update(self):
"""Fetch new state data for the sensor.
This is the only method that should fetch new data for Home Assistant.
"""
self._state = self.parm