Skip to content

Commit

Permalink
Update tuya_exporter.py
Browse files Browse the repository at this point in the history
  • Loading branch information
luizbizzio authored Aug 9, 2024
1 parent 238badc commit ee01771
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions tuya_exporter.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import time
from typing import List

import click
import tinytuya
from prometheus_client import start_http_server
from prometheus_client.core import GaugeMetricFamily, REGISTRY

# Port to run the Prometheus exporter on
EXPORTER_PORT = 9999

# Replace the placeholder values with your device's local IP address, device ID, and local key
DEVICE_CONFIGS = [
DeviceConfig(ip="DEVICE_LOCAL_IP", device_id="DEVICE_ID", local_key="DEVICE_LOCAL_KEY")
]

class DeviceConfig:
def __init__(self, ip: str, device_id: str, local_key: str):
self.ip = ip
Expand All @@ -24,9 +31,9 @@ def collect(self):
}
for config in self.configs:
device = tinytuya.OutletDevice(config.device_id, config.ip, config.local_key)
device.set_version(3.4)
device.set_version(3.4) # If you encounter connection issues, try changing the version to 3.0, 3.1, 3.2, or 3.3
device.set_socketTimeout(2)
device.updatedps([18, 19, 20])
device.updatedps([18, 19, 20])
data = device.status()
if "Error" not in data:
gauges["tuya_consumption_current"].add_metric([], float(data.get("dps", {}).get("18", 0)) / 1000.0)
Expand All @@ -35,21 +42,14 @@ def collect(self):

yield from gauges.values()

def load_config() -> List[DeviceConfig]:
return [
DeviceConfig(ip="192.168.0.21", device_id="eb015220cd596d08afefhm", local_key="5@eUCXJS8R)QtD8K")
]

@click.command()
@click.option("--port", help="Port to run the Prometheus exporter on.", default=9999)
@click.option("--port", help="Port to run the Prometheus exporter on.", default=EXPORTER_PORT)
def main(port):
configs = load_config()
REGISTRY.register(Collector(configs))
REGISTRY.register(Collector(DEVICE_CONFIGS))
start_http_server(port)
print(f"Serving metrics on port {port}")
while True:
time.sleep(0.1)


if __name__ == "__main__":
main()

0 comments on commit ee01771

Please sign in to comment.