misc: codebase path cleanup

This commit is contained in:
2026-02-08 00:14:10 +01:00
parent cff05f46df
commit d0c489800d
6 changed files with 61 additions and 131 deletions
+49 -7
View File
@@ -40,6 +40,23 @@ def parse_actor_value(value):
return 0
def parse_actor_float(value):
if isinstance(value, bool):
return 1.0 if value else 0.0
if isinstance(value, (int, float)):
return float(value)
if isinstance(value, str):
if value.strip().lower() in {"true", "on"}:
return 1.0
if value.strip().lower() in {"false", "off"}:
return 0.0
try:
return float(value)
except ValueError:
return 0.0
return 0.0
def load_env_file(path: str) -> dict:
env = {}
try:
@@ -60,10 +77,14 @@ class DeviceEmulator:
self.mac_address = generate_mac()
self.device_id = mac_to_device_uuid(self.mac_address)
self.sensor_id = uuid.uuid5(self.device_id, "sensor-0")
self.sensor_bool_id = uuid.uuid5(self.device_id, "sensor-1")
self.actor_id = uuid.uuid5(self.device_id, "actor-0")
self.actor_float_id = uuid.uuid5(self.device_id, "actor-1")
self.actor_value = 0
self.actor_float_value = 0.0
self.temperature = 22.5
self.switch_state = False
self.publish_interval = 5
env_path = os.path.join(os.path.dirname(__file__), ".env")
@@ -93,6 +114,12 @@ class DeviceEmulator:
"name": "Temperature",
"type": "DHT22",
"data_type_id": 2,
},
{
"id": str(self.sensor_bool_id),
"name": "Presence",
"type": "PIR",
"data_type_id": 1,
}
],
"actors": [
@@ -101,6 +128,12 @@ class DeviceEmulator:
"name": "Binary Switch",
"type": "Relay",
"data_type_id": 1,
},
{
"id": str(self.actor_float_id),
"name": "Dimmer",
"type": "PWM",
"data_type_id": 2,
}
],
}
@@ -121,15 +154,16 @@ class DeviceEmulator:
self.client.publish(TOPIC, json.dumps(payload), qos=0)
print(f"sensor_reading published: {self.temperature}C")
def publish_actor_state(self):
def publish_bool_sensor_reading(self):
self.switch_state = not self.switch_state
payload = {
"type": "actor_state",
"actor_id": str(self.actor_id),
"value": self.actor_value,
"updated_at": now_rfc3339(),
"type": "sensor_reading",
"sensor_id": str(self.sensor_bool_id),
"value": self.switch_state,
"value_at": now_rfc3339(),
}
self.client.publish(TOPIC, json.dumps(payload), qos=0)
print(f"actor_state published: {self.actor_value}")
print(f"sensor_reading published: presence={self.switch_state}")
def publish_device_state(self):
payload = {
@@ -164,11 +198,18 @@ class DeviceEmulator:
if payload.get("sensor_id") == str(self.sensor_id):
self.publish_sensor_reading()
return
if payload.get("sensor_id") == str(self.sensor_bool_id):
self.publish_bool_sensor_reading()
return
if msg_type == "actor_command":
if payload.get("actor_id") == str(self.actor_id):
self.actor_value = parse_actor_value(payload.get("value"))
self.publish_actor_state()
print(f"actor_command received; state={self.actor_value}")
return
if payload.get("actor_id") == str(self.actor_float_id):
self.actor_float_value = parse_actor_float(payload.get("value"))
print(f"actor_command received; dimmer={self.actor_float_value}")
return
def run(self):
@@ -178,6 +219,7 @@ class DeviceEmulator:
while True:
time.sleep(self.publish_interval)
self.publish_sensor_reading()
self.publish_bool_sensor_reading()
finally:
self.client.loop_stop()