Skip to content

Commit

Permalink
Merge pull request #11 from PilotChalkanov/feature/ths_sensor_status
Browse files Browse the repository at this point in the history
Add temp and humidity sensor status
  • Loading branch information
PilotChalkanov authored Jan 27, 2024
2 parents 318e1a3 + 2658d83 commit 1b89132
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions function_app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
from typing import Optional
import yaml
import os
import azure.functions as func
Expand All @@ -15,7 +16,7 @@
IR_REMOTE_ID = os.getenv("IR_REMOTE_ID")
AIRCON_ID = os.getenv("AIRCON_ID")
SMART_PLUG_ID = os.getenv("SMART_PLUG_ID")

THS_SENSOR_ID = os.getenv("THS_SENSOR_ID")
DEVICES_ENDPOINT = "/v1.0/iot-03/devices"

# Init settings - client, config, etc
Expand All @@ -25,9 +26,9 @@
config = {}
with open('func-config.yml', 'r') as f:
config = yaml.safe_load(f)

schedule = config['base-configs']['schedule']

plug_status_endpoint = f"{DEVICES_ENDPOINT}/{SMART_PLUG_ID}/status"
ths_status_endpoint = f"{DEVICES_ENDPOINT}/{THS_SENSOR_ID}/status"

#TODO : Add real sensor values
@app.schedule(
Expand All @@ -41,11 +42,17 @@ def control_humidifier(myTimer: func.TimerRequest) -> None:
"""
logging.info("Python timer trigger function executed.")
openapi.connect()
plug_status_endpoint = f"{DEVICES_ENDPOINT}/{SMART_PLUG_ID}/status"
status = openapi.get(path=plug_status_endpoint).get("result")[0]

plug_status = openapi.get(path=plug_status_endpoint).get("result")[0]
ths_result_list = openapi.get(path=ths_status_endpoint).get("result")

humidity = [r for r in ths_result_list if r["code"]=="va_humidity"][0]
plug_on = plug_status["value"]
switch_on = False
if not status["value"]:

if not plug_on and humidity["value"] < 45:
switch_on = True

payload = {"commands": [{"code": "switch_1", "value": switch_on}]}
plug_command_endpoint = f"{DEVICES_ENDPOINT}/{SMART_PLUG_ID}/commands"
response = openapi.post(path=plug_command_endpoint, body=payload)
Expand Down

0 comments on commit 1b89132

Please sign in to comment.