summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2023-05-31 22:27:46 +0300
committerEvgeny Zinoviev <me@ch1p.io>2023-05-31 22:27:46 +0300
commit52db06e0c81bf7f5ec75da40250ddb700d333db2 (patch)
tree730c0db16580a42487cf46b60cdb346571beee3b /src
parentc976495222858c4921454c9294ff73794ae56277 (diff)
save
Diffstat (limited to 'src')
-rw-r--r--src/home/mqtt/relay.py59
-rwxr-xr-xsrc/pio_ini.py10
2 files changed, 68 insertions, 1 deletions
diff --git a/src/home/mqtt/relay.py b/src/home/mqtt/relay.py
new file mode 100644
index 0000000..cf657f7
--- /dev/null
+++ b/src/home/mqtt/relay.py
@@ -0,0 +1,59 @@
+#!/usr/bin/env python3
+import paho.mqtt.client as mqtt
+import re
+import logging
+
+from .mqtt import MQTTBase
+
+
+class MQTTRelayClient(MQTTBase):
+ _home_id: str
+
+ def __init__(self, home_id: str):
+ super().__init__(clean_session=True)
+ self._home_id = home_id
+
+ def on_connect(self, client: mqtt.Client, userdata, flags, rc):
+ super().on_connect(client, userdata, flags, rc)
+
+ topic = f'home/{self._home_id}/#'
+ self._logger.info(f"subscribing to {topic}")
+
+ client.subscribe(topic, qos=1)
+
+ def on_message(self, client: mqtt.Client, userdata, msg):
+ try:
+ match = re.match(r'^home/(.*?)/relay/(stat|power)(?:/(.+))?$', msg.topic)
+ self._logger.info(f'topic: {msg.topic}')
+ if not match:
+ return
+
+ name = match.group(1)
+ subtopic = match.group(2)
+
+ if name != self._home_id:
+ return
+
+ if subtopic == 'stat':
+ stat_name, stat_value = match.group(3).split('/')
+ self._logger.info(f'stat: {stat_name} = {stat_value}')
+
+ except Exception as e:
+ self._logger.exception(str(e))
+
+
+class MQTTRelayController(MQTTBase):
+ _home_id: str
+
+ def __init__(self, home_id: str):
+ super().__init__(clean_session=True)
+ self._home_id = home_id
+
+ def set_power(self, enable: bool):
+ self._client.publish(f'home/{self._home_id}/relay/power',
+ payload=int(enable),
+ qos=1)
+ self._client.loop_write()
+
+ def send_stat(self, stat: dict):
+ pass
diff --git a/src/pio_ini.py b/src/pio_ini.py
index 19dd707..920c3e5 100755
--- a/src/pio_ini.py
+++ b/src/pio_ini.py
@@ -54,12 +54,17 @@ def bsd_parser(product_config: dict,
arg_kwargs['type'] = int
elif kwargs['type'] == 'int':
arg_kwargs['type'] = int
+ elif kwargs['type'] == 'bool':
+ arg_kwargs['action'] = 'store_true'
+ arg_kwargs['required'] = False
else:
raise TypeError(f'unsupported type {kwargs["type"]} for define {define_name}')
else:
arg_kwargs['action'] = 'store_true'
- parser.add_argument(f'--{define_name}', required=True, **arg_kwargs)
+ if 'required' not in arg_kwargs:
+ arg_kwargs['required'] = True
+ parser.add_argument(f'--{define_name}', **arg_kwargs)
bsd_walk(product_config, f)
@@ -76,6 +81,9 @@ def bsd_get(product_config: dict,
enums.append(f'CONFIG_{define_name}')
defines[f'CONFIG_{define_name}'] = f'HOMEKIT_{attr_value.upper()}'
return
+ if kwargs['type'] == 'bool':
+ defines[f'CONFIG_{define_name}'] = True
+ return
defines[f'CONFIG_{define_name}'] = str(attr_value)
bsd_walk(product_config, f)
return defines, enums