diff --git a/terraform_shelly/.terraform.lock.hcl b/terraform_shelly/.terraform.lock.hcl index 2babc1f..53862e1 100644 --- a/terraform_shelly/.terraform.lock.hcl +++ b/terraform_shelly/.terraform.lock.hcl @@ -23,6 +23,27 @@ provider "registry.terraform.io/donrobo/shelly" { ] } +provider "registry.terraform.io/hashicorp/external" { + version = "2.4.0" + constraints = "~> 2.3" + hashes = [ + "h1:vvIsrZAD5eNoNtXvNcBUxkA9k2A4JcC8JTDFbM9d8gc=", + "zh:0772afb42b658468ac5e15df33bf2080456f8f0b8ab163bfe9c50d2b2ea02135", + "zh:0ac31a9aaa43dfcff5944b791596cdc94e153348e4bb4642282d034dff548134", + "zh:32d8492b1bdcc956ca3c6d00c6392d0a83942ff11d4820c7ee63ca6796e06950", + "zh:3c0482e894429f528ce6655a76ab0d8a9f7c0dacc6c828865e1515d4a7dbb852", + "zh:61e68100b4db2f930b31491f23c602126382fd5e51252be1b551f0e17f8ddbee", + "zh:6d60f615a0ad85eb962c9eb94f25e3eba7a72684ce276ba5dfb23f36b295a8f8", + "zh:78d5eefdd9e494defcb3c68d282b8f96630502cac21d1ea161f53cfe9bb483b3", + "zh:9ced2745eb5f1346203027d2dd7bf856ad1d279a25730ff7dbc6eec187aaca0c", + "zh:a8378558a177d43f55aa0d79d4fae91a704695122a1b109668c1daa8fb76f09d", + "zh:aadd98086133d3ebea67437d56512fdcc6dfb3bd34dfc23f276c0db9272e27b4", + "zh:beff701b653841e70441978137768f54e7dc6c27e7bf12a4589087f01f5bbcee", + "zh:c91c2223b29fdbc0044d20e1936ccc051d010727a13f2ff1e75e51f09bff33a3", + "zh:d491f9c2d32a39dc4031628469ae7c8aec0074312a7c1f0286b173cdcf854a54", + ] +} + provider "registry.terraform.io/hashicorp/local" { version = "2.9.0" constraints = "~> 2.4" diff --git a/terraform_shelly/main.tf b/terraform_shelly/main.tf index aa4fc79..c7d56d4 100644 --- a/terraform_shelly/main.tf +++ b/terraform_shelly/main.tf @@ -39,3 +39,17 @@ resource "null_resource" "upload_script" { depends_on = [shelly_script_config.taupi] } + +data "external" "script_status" { + program = ["python3", "${path.module}/scripts/get_status.py", var.shelly_ip, "1"] + + # Trigger re-run whenever upload finishes + query = { + trigger = null_resource.upload_script.id + } +} + +output "script_status" { + value = data.external.script_status.result + description = "The runtime status of the Shelly script" +} diff --git a/terraform_shelly/providers.tf b/terraform_shelly/providers.tf index ec7f6bc..6dcc25a 100644 --- a/terraform_shelly/providers.tf +++ b/terraform_shelly/providers.tf @@ -12,6 +12,10 @@ terraform { source = "hashicorp/local" version = "~> 2.4" } + external = { + source = "hashicorp/external" + version = "~> 2.3" + } } required_version = ">= 1.0" } diff --git a/terraform_shelly/scripts/get_status.py b/terraform_shelly/scripts/get_status.py new file mode 100644 index 0000000..8f6c328 --- /dev/null +++ b/terraform_shelly/scripts/get_status.py @@ -0,0 +1,48 @@ +import sys +import json +import urllib.request + +def get_status(ip, script_id): + url = f"http://{ip}/rpc" + payload = { + "id": 1, + "method": "Script.GetStatus", + "params": {"id": int(script_id)} + } + req = urllib.request.Request( + url, + data=json.dumps(payload).encode('utf-8'), + headers={'Content-Type': 'application/json'} + ) + try: + with urllib.request.urlopen(req) as resp: + res = json.loads(resp.read().decode('utf-8')) + if "result" in res: + status = res["result"] + # Convert all values to strings as HashiCorp External Provider only accepts string properties + return {k: str(v) for k, v in status.items() if v is not None} + elif "error" in res: + return {"error": str(res["error"])} + except Exception as e: + return {"error": str(e)} + return {"error": "unknown"} + +if __name__ == "__main__": + if len(sys.argv) < 3: + # Check stdin if arguments are missing (sometimes Terraform passes parameters to stdin) + try: + input_data = json.loads(sys.stdin.read()) + ip = input_data.get("ip") + script_id = input_data.get("script_id") + except: + ip, script_id = None, None + + if not ip or not script_id: + print(json.dumps({"error": "Usage: get_status.py or pass JSON via stdin"})) + sys.exit(1) + else: + ip = sys.argv[1] + script_id = sys.argv[2] + + status = get_status(ip, script_id) + print(json.dumps(status)) diff --git a/terraform_shelly/variables.tf b/terraform_shelly/variables.tf index d0fdaad..04a44a4 100644 --- a/terraform_shelly/variables.tf +++ b/terraform_shelly/variables.tf @@ -17,7 +17,7 @@ variable "sensor_innen_mac" { variable "taupunktschwelle" { type = number description = "Dew point difference threshold [°C] to trigger the fan (indoor_dp > outdoor_dp + threshold)" - default = 2 + default = 4 } variable "mindesttemperatur" {