diff --git a/function_app.py b/function_app.py index 4126c8d..e8a739a 100644 --- a/function_app.py +++ b/function_app.py @@ -1,4 +1,5 @@ import logging +from typing import Optional import yaml import os import azure.functions as func @@ -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 @@ -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( @@ -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)