Skip to content

Commit 4b55319

Browse files
authored
Merge pull request #196 from DJDevon3/main
Update Rachio example with Rate Limit endpoints, fixed 1700 daily requests
2 parents 197c653 + 0244f02 commit 4b55319

File tree

1 file changed

+144
-118
lines changed

1 file changed

+144
-118
lines changed

examples/wifi/expanded/requests_wifi_rachio_irrigation.py

+144-118
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
# https://support.rachio.com/en_us/public-api-documentation-S1UydL1Fv
1717
# https://rachio.readme.io/reference/getting-started
1818
RACHIO_KEY = os.getenv("RACHIO_APIKEY")
19+
RACHIO_PERSONID = os.getenv("RACHIO_PERSONID")
1920

2021
# Get WiFi details, ensure these are setup in settings.toml
2122
ssid = os.getenv("CIRCUITPY_WIFI_SSID")
@@ -100,125 +101,150 @@ def _format_datetime(datetime):
100101
print("Retrying in 10 seconds")
101102
print("✅ Wifi!")
102103

103-
try:
104-
print(" | Attempting to GET Rachio Authorization")
104+
# RETREIVE PERSONID AND PASTE IT TO SETTINGS.TOML
105+
if RACHIO_PERSONID is None or RACHIO_PERSONID == "":
105106
try:
106-
with requests.get(
107-
url=RACHIO_SOURCE, headers=RACHIO_HEADER
108-
) as rachio_response:
109-
rachio_json = rachio_response.json()
110-
except ConnectionError as e:
111-
print("Connection Error:", e)
112-
print("Retrying in 10 seconds")
113-
print(" | ✅ Authorized")
114-
115-
rachio_id = rachio_json["id"]
116-
if DEBUG:
117-
print(" | | Person ID: ", rachio_id)
118-
print(" | | This ID will be used for subsequent calls")
119-
print("\nFull API GET URL: ", RACHIO_SOURCE)
120-
print(rachio_json)
121-
122-
except (ValueError, RuntimeError) as e:
123-
print(f"Failed to get data, retrying\n {e}")
124-
time.sleep(60)
125-
break
126-
127-
try:
128-
print(" | Attempting to GET Rachio JSON")
129-
try:
130-
with requests.get(
131-
url=RACHIO_PERSON_SOURCE + rachio_id, headers=RACHIO_HEADER
132-
) as rachio_response:
133-
rachio_json = rachio_response.json()
134-
except ConnectionError as e:
135-
print("Connection Error:", e)
136-
print("Retrying in 10 seconds")
137-
print(" | ✅ Rachio JSON")
138-
139-
rachio_id = rachio_json["id"]
140-
rachio_id_ast = obfuscating_asterix(rachio_id, "append", 3)
141-
print(" | | UserID: ", rachio_id_ast)
142-
143-
rachio_username = rachio_json["username"]
144-
rachio_username_ast = obfuscating_asterix(rachio_username, "append", 3)
145-
print(" | | Username: ", rachio_username_ast)
146-
147-
rachio_name = rachio_json["fullName"]
148-
rachio_name_ast = obfuscating_asterix(rachio_name, "append", 3)
149-
print(" | | Full Name: ", rachio_name_ast)
150-
151-
rachio_deleted = rachio_json["deleted"]
152-
if not rachio_deleted:
153-
print(" | | Account Status: Active")
154-
else:
155-
print(" | | Account Status?: Deleted!")
156-
157-
rachio_createdate = rachio_json["createDate"]
158-
rachio_timezone_offset = rachio_json["devices"][0]["utcOffset"]
159-
# Rachio Unix time is in milliseconds, convert to seconds
160-
rachio_createdate_seconds = rachio_createdate // 1000
161-
rachio_timezone_offset_seconds = rachio_timezone_offset // 1000
162-
# Apply timezone offset in seconds
163-
local_unix_time = rachio_createdate_seconds + rachio_timezone_offset_seconds
164-
if DEBUG:
165-
print(f" | | Unix Registration Date: {rachio_createdate}")
166-
print(f" | | Unix Timezone Offset: {rachio_timezone_offset}")
167-
current_struct_time = time.localtime(local_unix_time)
168-
final_timestamp = "{}".format(_format_datetime(current_struct_time))
169-
print(f" | | Registration Date: {final_timestamp}")
170-
171-
rachio_devices = rachio_json["devices"][0]["name"]
172-
print(" | | Device: ", rachio_devices)
173-
174-
rachio_model = rachio_json["devices"][0]["model"]
175-
print(" | | | Model: ", rachio_model)
176-
177-
rachio_serial = rachio_json["devices"][0]["serialNumber"]
178-
rachio_serial_ast = obfuscating_asterix(rachio_serial, "append")
179-
print(" | | | Serial Number: ", rachio_serial_ast)
180-
181-
rachio_mac = rachio_json["devices"][0]["macAddress"]
182-
rachio_mac_ast = obfuscating_asterix(rachio_mac, "append")
183-
print(" | | | MAC Address: ", rachio_mac_ast)
184-
185-
rachio_status = rachio_json["devices"][0]["status"]
186-
print(" | | | Device Status: ", rachio_status)
187-
188-
rachio_timezone = rachio_json["devices"][0]["timeZone"]
189-
print(" | | | Time Zone: ", rachio_timezone)
190-
191-
# Latitude & Longtitude are used for smart watering & rain delays
192-
rachio_latitude = str(rachio_json["devices"][0]["latitude"])
193-
rachio_lat_ast = obfuscating_asterix(rachio_latitude, "all")
194-
print(" | | | Latitude: ", rachio_lat_ast)
195-
196-
rachio_longitude = str(rachio_json["devices"][0]["longitude"])
197-
rachio_long_ast = obfuscating_asterix(rachio_longitude, "all")
198-
print(" | | | Longitude: ", rachio_long_ast)
199-
200-
rachio_rainsensor = rachio_json["devices"][0]["rainSensorTripped"]
201-
print(" | | | Rain Sensor: ", rachio_rainsensor)
202-
203-
rachio_zone0 = rachio_json["devices"][0]["zones"][0]["name"]
204-
rachio_zone1 = rachio_json["devices"][0]["zones"][1]["name"]
205-
rachio_zone2 = rachio_json["devices"][0]["zones"][2]["name"]
206-
rachio_zone3 = rachio_json["devices"][0]["zones"][3]["name"]
207-
zones = f"{rachio_zone0}, {rachio_zone1}, {rachio_zone2}, {rachio_zone3}"
208-
print(f" | | | Zones: {zones}")
209-
210-
if DEBUG:
211-
print(f"\nFull API GET URL: {RACHIO_PERSON_SOURCE+rachio_id}")
212-
print(rachio_json)
213-
107+
print(" | Attempting to GET Rachio Authorization")
108+
try:
109+
with requests.get(
110+
url=RACHIO_SOURCE, headers=RACHIO_HEADER
111+
) as rachio_response:
112+
rachio_json = rachio_response.json()
113+
except ConnectionError as e:
114+
print("Connection Error:", e)
115+
print("Retrying in 10 seconds")
116+
print(" | ✅ Authorized")
117+
118+
rachio_id = rachio_json["id"]
119+
print("\nADD THIS 🔑 TO YOUR SETTINGS.TOML FILE!")
120+
print(f'RACHIO_PERSONID = "{rachio_id}"')
121+
122+
if DEBUG:
123+
print("\nFull API GET URL: ", RACHIO_SOURCE)
124+
print(rachio_json)
125+
126+
except (ValueError, RuntimeError) as e:
127+
print(f"Failed to GET data: {e}")
128+
time.sleep(60)
129+
break
130+
print(
131+
"\nThis script can only continue when a proper APIKey & PersonID is used."
132+
)
214133
print("\nFinished!")
215-
print(f"Board Uptime: {time_calc(time.monotonic())}")
216-
print(f"Next Update: {time_calc(SLEEP_TIME)}")
217134
print("===============================")
135+
time.sleep(SLEEP_TIME)
218136

219-
except (ValueError, RuntimeError) as e:
220-
print(f"Failed to get data, retrying\n {e}")
221-
time.sleep(60)
222-
break
223-
224-
time.sleep(SLEEP_TIME)
137+
# Main Script
138+
if RACHIO_PERSONID is not None and RACHIO_PERSONID != "":
139+
try:
140+
print(" | Attempting to GET Rachio JSON")
141+
try:
142+
with requests.get(
143+
url=RACHIO_PERSON_SOURCE + RACHIO_PERSONID, headers=RACHIO_HEADER
144+
) as rachio_response:
145+
rachio_json = rachio_response.json()
146+
except ConnectionError as e:
147+
print("Connection Error:", e)
148+
print("Retrying in 10 seconds")
149+
print(" | ✅ Rachio JSON")
150+
response_headers = rachio_response.headers
151+
if DEBUG:
152+
print(f"Response Headers: {response_headers}")
153+
call_limit = int(response_headers["x-ratelimit-limit"])
154+
calls_remaining = int(response_headers["x-ratelimit-remaining"])
155+
calls_made_today = call_limit - calls_remaining
156+
157+
print(" | | Headers:")
158+
print(f" | | | Date: {response_headers['date']}")
159+
print(f" | | | Maximum Daily Requests: {call_limit}")
160+
print(f" | | | Today's Requests: {calls_made_today}")
161+
print(f" | | | Remaining Requests: {calls_remaining}")
162+
print(f" | | | Limit Reset: {response_headers['x-ratelimit-reset']}")
163+
print(f" | | | Content Type: {response_headers['content-type']}")
164+
165+
rachio_id = rachio_json["id"]
166+
rachio_id_ast = obfuscating_asterix(rachio_id, "append", 3)
167+
print(" | | PersonID: ", rachio_id_ast)
168+
169+
rachio_username = rachio_json["username"]
170+
rachio_username_ast = obfuscating_asterix(rachio_username, "append", 3)
171+
print(" | | Username: ", rachio_username_ast)
172+
173+
rachio_name = rachio_json["fullName"]
174+
rachio_name_ast = obfuscating_asterix(rachio_name, "append", 3)
175+
print(" | | Full Name: ", rachio_name_ast)
176+
177+
rachio_deleted = rachio_json["deleted"]
178+
if not rachio_deleted:
179+
print(" | | Account Status: Active")
180+
else:
181+
print(" | | Account Status?: Deleted!")
182+
183+
rachio_createdate = rachio_json["createDate"]
184+
rachio_timezone_offset = rachio_json["devices"][0]["utcOffset"]
185+
# Rachio Unix time is in milliseconds, convert to seconds
186+
rachio_createdate_seconds = rachio_createdate // 1000
187+
rachio_timezone_offset_seconds = rachio_timezone_offset // 1000
188+
# Apply timezone offset in seconds
189+
local_unix_time = rachio_createdate_seconds + rachio_timezone_offset_seconds
190+
if DEBUG:
191+
print(f" | | Unix Registration Date: {rachio_createdate}")
192+
print(f" | | Unix Timezone Offset: {rachio_timezone_offset}")
193+
current_struct_time = time.localtime(local_unix_time)
194+
final_timestamp = "{}".format(_format_datetime(current_struct_time))
195+
print(f" | | Registration Date: {final_timestamp}")
196+
197+
rachio_devices = rachio_json["devices"][0]["name"]
198+
print(" | | Device: ", rachio_devices)
199+
200+
rachio_model = rachio_json["devices"][0]["model"]
201+
print(" | | | Model: ", rachio_model)
202+
203+
rachio_serial = rachio_json["devices"][0]["serialNumber"]
204+
rachio_serial_ast = obfuscating_asterix(rachio_serial, "append")
205+
print(" | | | Serial Number: ", rachio_serial_ast)
206+
207+
rachio_mac = rachio_json["devices"][0]["macAddress"]
208+
rachio_mac_ast = obfuscating_asterix(rachio_mac, "append")
209+
print(" | | | MAC Address: ", rachio_mac_ast)
210+
211+
rachio_status = rachio_json["devices"][0]["status"]
212+
print(" | | | Device Status: ", rachio_status)
213+
214+
rachio_timezone = rachio_json["devices"][0]["timeZone"]
215+
print(" | | | Time Zone: ", rachio_timezone)
216+
217+
# Latitude & Longtitude are used for smart watering & rain delays
218+
rachio_latitude = str(rachio_json["devices"][0]["latitude"])
219+
rachio_lat_ast = obfuscating_asterix(rachio_latitude, "all")
220+
print(" | | | Latitude: ", rachio_lat_ast)
221+
222+
rachio_longitude = str(rachio_json["devices"][0]["longitude"])
223+
rachio_long_ast = obfuscating_asterix(rachio_longitude, "all")
224+
print(" | | | Longitude: ", rachio_long_ast)
225+
226+
rachio_rainsensor = rachio_json["devices"][0]["rainSensorTripped"]
227+
print(" | | | Rain Sensor: ", rachio_rainsensor)
228+
229+
rachio_zone0 = rachio_json["devices"][0]["zones"][0]["name"]
230+
rachio_zone1 = rachio_json["devices"][0]["zones"][1]["name"]
231+
rachio_zone2 = rachio_json["devices"][0]["zones"][2]["name"]
232+
rachio_zone3 = rachio_json["devices"][0]["zones"][3]["name"]
233+
zones = f"{rachio_zone0}, {rachio_zone1}, {rachio_zone2}, {rachio_zone3}"
234+
print(f" | | | Zones: {zones}")
235+
236+
if DEBUG:
237+
print(f"\nFull API GET URL: {RACHIO_PERSON_SOURCE+rachio_id}")
238+
print(rachio_json)
239+
240+
print("\nFinished!")
241+
print(f"Board Uptime: {time_calc(time.monotonic())}")
242+
print(f"Next Update: {time_calc(SLEEP_TIME)}")
243+
print("===============================")
244+
245+
except (ValueError, RuntimeError) as e:
246+
print(f"Failed to get data, retrying\n {e}")
247+
time.sleep(60)
248+
break
249+
250+
time.sleep(SLEEP_TIME)

0 commit comments

Comments
 (0)