diff --git a/provisioning.yaml b/provisioning.yaml index 2c22634..457badb 100644 --- a/provisioning.yaml +++ b/provisioning.yaml @@ -10,6 +10,15 @@ e6614864d35f9934: HA_PLANT_TEMPERATURE_SENSOR: sensor.hedera_helix_temperature HA_PLANT_CONDUCTIVITY_SENSOR: sensor.hedera_helix_conductivity HA_PLANT_ILLUMINANCE_SENSOR: sensor.hedera_helix_illuminance + + ha_plant_max_moisture: number.hedera_helix_max_soil_moisture + ha_plant_min_moisture: number.hedera_helix_min_soil_moisture + ha_plant_max_temperature: number.hedera_helix_max_temperature + ha_plant_min_temperature: number.hedera_helix_min_temperature + ha_plant_max_illuminance: number.hedera_helix_max_illuminance + ha_plant_min_illuminance: number.hedera_helix_min_illuminance + ha_plant_max_conductivity: number.hedera_helix_max_conductivity + ha_plant_min_conductivity: number.hedera_helix_min_conductivity e6614864d3417f36: HA_PLANT_ID: plant.calathea_makoyana HA_PLANT_MOISTURE_SENSOR: sensor.calathea_makoyana_soil_moisture diff --git a/tasks.py b/tasks.py index 066245a..23cf854 100644 --- a/tasks.py +++ b/tasks.py @@ -52,18 +52,10 @@ def provision(c: Context, board_id: str, *, initial: bool = True) -> None: @task def download_image(c: Context, board_id: str) -> None: """Download and prepare the proper plant picture for the board.""" - import requests - import sys - - sys.path.insert(0, str(SRC_DIR)) - from secrets import HA_ACCESS_TOKEN, HA_BASE_URL - provisioning = get_provisioning(board_id) + plant_id = provisioning["HA_PLANT_ID"] - url = HA_BASE_URL + "/states/" + provisioning["HA_PLANT_ID"] - headers = {"Authorization": "Bearer " + HA_ACCESS_TOKEN} - res = requests.get(url, headers=headers) - data = res.json() + data = query_ha_state(plant_id) image_url = data["attributes"]["entity_picture"] image_path = SRC_DIR / "images" / "plant.jpg" c.run(f"curl -o {image_path} {image_url}", pty=True, echo=True) @@ -84,6 +76,20 @@ def download_image(c: Context, board_id: str) -> None: image.save(image_path) +def query_ha_state(entity_id): + import requests + import sys + + sys.path.insert(0, str(SRC_DIR)) + from secrets import HA_ACCESS_TOKEN, HA_BASE_URL + + url = HA_BASE_URL + "/states/" + entity_id + headers = {"Authorization": "Bearer " + HA_ACCESS_TOKEN} + res = requests.get(url, headers=headers) + data = res.json() + return data + + @task def wipe(c: Context, board_id: str) -> None: """Wipe the board with mpremote.""" @@ -130,6 +136,10 @@ def prepare(board_id: str) -> None: var_name = target.id if var_name in provisioning: node.value = ast.Constant(provisioning[var_name]) + elif var_name.lower() in provisioning: + state = query_ha_state(provisioning[var_name.lower()]) + value = int(state.get("state", -1)) + node.value = ast.Constant(value) with (SRC_DIR / "secrets.py").open("w") as f: f.write(ast.unparse(secrets))