summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/home/mqtt/temphum.py21
-rw-r--r--src/home/util.py12
-rwxr-xr-xsrc/temphum_nodes_util.py17
-rwxr-xr-xsrc/temphum_smbus_util.py (renamed from src/temphum.py)0
4 files changed, 44 insertions, 6 deletions
diff --git a/src/home/mqtt/temphum.py b/src/home/mqtt/temphum.py
index b9b2eb9..83886ac 100644
--- a/src/home/mqtt/temphum.py
+++ b/src/home/mqtt/temphum.py
@@ -1,10 +1,25 @@
import paho.mqtt.client as mqtt
import re
-from .payload.temphum import (
- TempHumDataPayload
-)
+from enum import auto
+from .payload.temphum import TempHumDataPayload
from .esp import MqttEspBase
+from ..util import HashableEnum
+
+
+class MqttTempHumNodes(HashableEnum):
+ KBN_SH_HALL = auto()
+ KBN_SH_BATHROOM = auto()
+ KBN_SH_LIVINGROOM = auto()
+ KBN_SH_BEDROOM = auto()
+
+ KBN_BH_2FL = auto()
+ KBN_BH_2FL_STREET = auto()
+ KBN_BH_1FL_LIVINGROOM = auto()
+ KBN_BH_1FL_BEDROOM = auto()
+ KBN_BH_1FL_BATHROOM = auto()
+
+ SPB_FLAT120_CABINET = auto()
class MqttTempHum(MqttEspBase):
diff --git a/src/home/util.py b/src/home/util.py
index 5050ebb..d5c4229 100644
--- a/src/home/util.py
+++ b/src/home/util.py
@@ -7,9 +7,10 @@ import logging
import string
import random
-from enum import Enum
+from enum import Enum, IntEnum
from datetime import datetime
-from typing import Tuple, Optional, List
+from typing import Tuple, Optional, List, Union
+from zlib import adler32
Addr = Tuple[str, int] # network address type (host, port)
@@ -187,4 +188,9 @@ def filesize_fmt(num, suffix="B") -> str:
if abs(num) < 1024.0:
return f"{num:3.1f} {unit}{suffix}"
num /= 1024.0
- return f"{num:.1f} Yi{suffix}" \ No newline at end of file
+ return f"{num:.1f} Yi{suffix}"
+
+
+class HashableEnum(Enum):
+ def hash(self) -> int:
+ return adler32(self.name.encode()) \ No newline at end of file
diff --git a/src/temphum_nodes_util.py b/src/temphum_nodes_util.py
new file mode 100755
index 0000000..c700ca8
--- /dev/null
+++ b/src/temphum_nodes_util.py
@@ -0,0 +1,17 @@
+#!/usr/bin/env python3
+from home.mqtt.temphum import MqttTempHumNodes
+
+if __name__ == '__main__':
+ max_name_len = 0
+ for node in MqttTempHumNodes:
+ if len(node.name) > max_name_len:
+ max_name_len = len(node.name)
+
+ values = []
+ for node in MqttTempHumNodes:
+ hash = node.hash()
+ if hash in values:
+ raise ValueError(f'collision detected: {hash}')
+ values.append(values)
+ print(' '*(max_name_len-len(node.name)), end='')
+ print(f'{node.name}: {hash}')
diff --git a/src/temphum.py b/src/temphum_smbus_util.py
index 0f90835..0f90835 100755
--- a/src/temphum.py
+++ b/src/temphum_smbus_util.py