Add min & max values during provisioning

This commit is contained in:
Gabriel Augendre 2023-03-21 15:16:29 +01:00
parent 6239ee0bc9
commit 87482464db
2 changed files with 29 additions and 10 deletions

View file

@ -10,6 +10,15 @@ e6614864d35f9934:
HA_PLANT_TEMPERATURE_SENSOR: sensor.hedera_helix_temperature HA_PLANT_TEMPERATURE_SENSOR: sensor.hedera_helix_temperature
HA_PLANT_CONDUCTIVITY_SENSOR: sensor.hedera_helix_conductivity HA_PLANT_CONDUCTIVITY_SENSOR: sensor.hedera_helix_conductivity
HA_PLANT_ILLUMINANCE_SENSOR: sensor.hedera_helix_illuminance 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: e6614864d3417f36:
HA_PLANT_ID: plant.calathea_makoyana HA_PLANT_ID: plant.calathea_makoyana
HA_PLANT_MOISTURE_SENSOR: sensor.calathea_makoyana_soil_moisture HA_PLANT_MOISTURE_SENSOR: sensor.calathea_makoyana_soil_moisture

View file

@ -52,18 +52,10 @@ def provision(c: Context, board_id: str, *, initial: bool = True) -> None:
@task @task
def download_image(c: Context, board_id: str) -> None: def download_image(c: Context, board_id: str) -> None:
"""Download and prepare the proper plant picture for the board.""" """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) provisioning = get_provisioning(board_id)
plant_id = provisioning["HA_PLANT_ID"]
url = HA_BASE_URL + "/states/" + provisioning["HA_PLANT_ID"] data = query_ha_state(plant_id)
headers = {"Authorization": "Bearer " + HA_ACCESS_TOKEN}
res = requests.get(url, headers=headers)
data = res.json()
image_url = data["attributes"]["entity_picture"] image_url = data["attributes"]["entity_picture"]
image_path = SRC_DIR / "images" / "plant.jpg" image_path = SRC_DIR / "images" / "plant.jpg"
c.run(f"curl -o {image_path} {image_url}", pty=True, echo=True) 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) 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 @task
def wipe(c: Context, board_id: str) -> None: def wipe(c: Context, board_id: str) -> None:
"""Wipe the board with mpremote.""" """Wipe the board with mpremote."""
@ -130,6 +136,10 @@ def prepare(board_id: str) -> None:
var_name = target.id var_name = target.id
if var_name in provisioning: if var_name in provisioning:
node.value = ast.Constant(provisioning[var_name]) 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: with (SRC_DIR / "secrets.py").open("w") as f:
f.write(ast.unparse(secrets)) f.write(ast.unparse(secrets))