From 54310ba2f5b30092c272a3e8abc7beef157737a0 Mon Sep 17 00:00:00 2001 From: ax Date: Thu, 2 Apr 2020 15:06:35 +0200 Subject: [PATCH 01/11] Exclude settings.json from git repo --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index dfcfd56..0e6fa6f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +resources/settings.json + # User-specific files *.rsuser *.suo From f391a9c46654c7fd2ae8f8660a8e19f5c162b8d1 Mon Sep 17 00:00:00 2001 From: ax Date: Thu, 2 Apr 2020 16:50:43 +0200 Subject: [PATCH 02/11] Added Dockerfile --- Dockerfile | 15 +++++++++++++++ docker-compose.yml | 11 +++++++++++ 2 files changed, 26 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0543546 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3 + +ENV APP /app + +RUN mkdir $APP +WORKDIR $APP + +EXPOSE 8321 + +COPY requirements.txt . + +RUN pip install -r requirements.txt + +COPY . . +CMD ["python3", "mystrombutton2mqtt.py", "./resources/settings.json"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5d61744 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' +services: + app: + image: mystrombutton2mqtt + build: + context: . + dockerfile: Dockerfile + ports: + - 8321:8321 + volumes: + - "./:/app" \ No newline at end of file From 3a11038961499938de13ae6ac2fa0267655b5be6 Mon Sep 17 00:00:00 2001 From: ax Date: Thu, 2 Apr 2020 19:47:56 +0200 Subject: [PATCH 03/11] Update README.md for Docker --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b024688..aa424c4 100644 --- a/README.md +++ b/README.md @@ -327,6 +327,10 @@ But if you want a real time action for adjusting the brightness of a light, you service: light.turn_on ``` +## Docker + +The included `Dockerfile` will build a basic image off `python:3`. Running `docker-compose up` will build and start the image, and will mount the `resources/settings.json` file. + ## Postscritum You can find the the API doc here: [https://api.mystrom.ch/?version=latest](https://api.mystrom.ch/?version=latest) From e2b1ac34b32c9044f3f2c78728afdb27a77c3745 Mon Sep 17 00:00:00 2001 From: ax Date: Thu, 2 Apr 2020 20:07:23 +0200 Subject: [PATCH 04/11] Add parameter for home-assistant autodiscovery --- mystrombutton2mqtt.py | 17 ++++++++++------- resources/settings.json | 42 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 resources/settings.json diff --git a/mystrombutton2mqtt.py b/mystrombutton2mqtt.py index d64f0b5..5963173 100644 --- a/mystrombutton2mqtt.py +++ b/mystrombutton2mqtt.py @@ -27,6 +27,8 @@ SUBSCRIBED_TOPICS = dict() +PREFIX = "" + def mqtt_message_callback(msg_topic, msg_payload): if msg_topic in SUBSCRIBED_TOPICS: @@ -120,8 +122,8 @@ def gen(): logging.debug("################### END ################### ") return "Ok" -def publish_discovery_sensor(mac,item,action_name,default_action_value,model,unit_of_measurement,device_class,icon,retain=False): - """ publish a Home Assistant Sensor Discory topic """ +def publish_discovery_sensor(mac,item,action_name,default_action_value,model,unit_of_measurement,device_class,icon,prefix, retain=False): + """ publish a Home Assistant Sensor Discovery topic """ if device_class =="None": device_class_template ='' else: @@ -143,7 +145,7 @@ def publish_discovery_sensor(mac,item,action_name,default_action_value,model,uni "state_topic": "~'+action_name+'",\ "value_template":"{{ value | upper }}"}' #Configuration topic: - conn.publish( topic="homeassistant/sensor/myStrom/"+mac+"_"+action_name+"/config",payload=msg_json,retain=True) + conn.publish( topic=prefix+"/sensor/myStrom/"+mac+"_"+action_name+"/config",payload=msg_json,retain=True) #State topic: conn.publish("myStrom/wifi_buttons/"+item+"_"+mac+"/"+action_name , default_action_value,retain=retain ) @@ -155,8 +157,8 @@ def nice_macaddress(mac): return out -def publish_discovery_binary_sensor( mac,item,action_name,default_action_value,model,icon): - """ publish a Home Assistant Binary_Sensor Discory topic """ +def publish_discovery_binary_sensor( mac,item,action_name,default_action_value,model,icon,prefix): + """ publish a Home Assistant Binary_Sensor Discovery topic """ icon_template= '' #'"ic":"'+icon+'",' @@ -176,7 +178,7 @@ def publish_discovery_binary_sensor( mac,item,action_name,default_action_value,m "payload_off":"OFF",\ "off_delay": 1 }' #Configuration topic: - conn.publish(topic="homeassistant/binary_sensor/myStrom/"+mac+"_"+action_name+"/config",payload=msg_json,retain=True) + conn.publish(topic=prefix+"/binary_sensor/myStrom/"+mac+"_"+action_name+"/config",payload=msg_json,retain=True) #State topic: conn.publish("myStrom/wifi_buttons/"+item+"_"+mac+"/"+action_name, default_action_value) @@ -227,7 +229,8 @@ def publish_discovery(): if settings["version"] != 2: print('Please update the settings file to the version 2') exit(1) - + + PREFIX = settings["homeassistant"]["discoveryprefix"] # users # for user in settings["http"]["valid_users"]: # VALID_USERS[user] = settings["http"]["valid_users"][user] diff --git a/resources/settings.json b/resources/settings.json new file mode 100644 index 0000000..3825b07 --- /dev/null +++ b/resources/settings.json @@ -0,0 +1,42 @@ +{ + "comment": "Config file for myStrom Wifi Button 2 MQTT", + "version": 2, + "mqtt": { + "brokeraddress": "192.168.1.9", + "brokerport": 1883, + "brokerusername": "mqtt", + "brokerpassword": "mqtt-password", + + "brokerssl": false, + "cafilepath": "./resources/mqtt_client/ca.crt", + "certfilepath": "./resources/mqtt_client/client.crt", + "keyfilepath": "./resources/mqtt_client/client.key", + + "valid_topics" : [ + "api/mystrom" + ,"api/mystrom/gen" + ], + "subscribed_topics" : [ + ] + }, + "mystrom": { + "button":{ + "60019427E1C7" : {"name":"axButton01"} + }, + "button+":{ + } + + }, + "homeassistant": { + "discoveryprefix": "hass" + }, + "http" : { + "port" : 8321, + "ssl" : false, + "certfilepath" : "./resources/http_server/server.crt", + "keyfilepath" : "./resources/http_server/server.key", + "valid_users" : { + "user1" : "pwd1" + } + } +} From abab7e34d024ab1c31bb61c182f0c34be5efe8bd Mon Sep 17 00:00:00 2001 From: ax Date: Thu, 2 Apr 2020 15:06:35 +0200 Subject: [PATCH 05/11] Exclude settings.json from git repo --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index dfcfd56..0e6fa6f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +resources/settings.json + # User-specific files *.rsuser *.suo From dcb68615807a2246aeecfabcc41a5efa45da9b05 Mon Sep 17 00:00:00 2001 From: ax Date: Thu, 2 Apr 2020 16:50:43 +0200 Subject: [PATCH 06/11] Added Dockerfile --- Dockerfile | 15 +++++++++++++++ docker-compose.yml | 11 +++++++++++ 2 files changed, 26 insertions(+) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0543546 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM python:3 + +ENV APP /app + +RUN mkdir $APP +WORKDIR $APP + +EXPOSE 8321 + +COPY requirements.txt . + +RUN pip install -r requirements.txt + +COPY . . +CMD ["python3", "mystrombutton2mqtt.py", "./resources/settings.json"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..5d61744 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,11 @@ +version: '3' +services: + app: + image: mystrombutton2mqtt + build: + context: . + dockerfile: Dockerfile + ports: + - 8321:8321 + volumes: + - "./:/app" \ No newline at end of file From 22fd221df71f63033c510ef30d83032b918732a6 Mon Sep 17 00:00:00 2001 From: ax Date: Thu, 2 Apr 2020 19:47:56 +0200 Subject: [PATCH 07/11] Update README.md for Docker --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index b024688..aa424c4 100644 --- a/README.md +++ b/README.md @@ -327,6 +327,10 @@ But if you want a real time action for adjusting the brightness of a light, you service: light.turn_on ``` +## Docker + +The included `Dockerfile` will build a basic image off `python:3`. Running `docker-compose up` will build and start the image, and will mount the `resources/settings.json` file. + ## Postscritum You can find the the API doc here: [https://api.mystrom.ch/?version=latest](https://api.mystrom.ch/?version=latest) From 21ad9e15ef35b9a71bfc56f4bf4c845d3699a95a Mon Sep 17 00:00:00 2001 From: ax Date: Thu, 2 Apr 2020 20:17:52 +0200 Subject: [PATCH 08/11] Added discoveryprefix to settings.json.sample --- mystrombutton2mqtt.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/mystrombutton2mqtt.py b/mystrombutton2mqtt.py index 5963173..729db13 100644 --- a/mystrombutton2mqtt.py +++ b/mystrombutton2mqtt.py @@ -185,21 +185,21 @@ def publish_discovery_binary_sensor( mac,item,action_name,default_action_value,m def publish_discovery_button_plus( mac,item): """ publish Home Assistant Discory topics for a button plus """ publish_discovery_button( mac,item,"Button Plus") - publish_discovery_binary_sensor(mac,item,"touch","OFF","Button Plus","mdi:gesture-tap") - publish_discovery_binary_sensor(mac,item,"wheel_final","OFF","Button Plus","mdi:sync") + publish_discovery_binary_sensor(mac,item,"touch","OFF","Button Plus","mdi:gesture-tap", PREFIX) + publish_discovery_binary_sensor(mac,item,"wheel_final","OFF","Button Plus","mdi:sync", PREFIX) publish_discovery_sensor(mac=mac,item=item,action_name="wheel",default_action_value="0",\ - model="Button Plus",unit_of_measurement="",device_class="None",icon="mdi:sync") + model="Button Plus",unit_of_measurement="",device_class="None",icon="mdi:sync",prefix=PREFIX) publish_discovery_sensor(mac=mac,item=item,action_name="level",default_action_value=LEVEL[mac],\ - model="Button Plus",unit_of_measurement="",device_class="None",icon="mdi:label-percent",retain=True) + model="Button Plus",unit_of_measurement="",device_class="None",icon="mdi:label-percent",prefix=PREFIX,retain=True) def publish_discovery_button( mac,item,model): """ publish Home Assistant Discory topics for a button """ - publish_discovery_binary_sensor(mac,item,"single","OFF",model,"mdi:radiobox-blank") - publish_discovery_binary_sensor(mac,item,"double","OFF",model,"mdi:circle-double") - publish_discovery_binary_sensor(mac,item,"long","OFF",model, "mdi:radiobox-marked") + publish_discovery_binary_sensor(mac,item,"single","OFF",model,"mdi:radiobox-blank",PREFIX) + publish_discovery_binary_sensor(mac,item,"double","OFF",model,"mdi:circle-double",PREFIX) + publish_discovery_binary_sensor(mac,item,"long","OFF",model, "mdi:radiobox-marked",PREFIX) publish_discovery_sensor(mac=mac,item=item,action_name="battery", default_action_value="-1",\ - model=model,unit_of_measurement=" %",device_class="battery",icon="mdi:battery-60",retain=True) + model=model,unit_of_measurement=" %",device_class="battery",icon="mdi:battery-60",prefix=PREFIX,retain=True) def publish_discovery(): """ publish Home Assistant Discory topics for every button plus and every button """ From 9f5d97e1b8361e6590d1bf9cbd5792944985f843 Mon Sep 17 00:00:00 2001 From: Alexis Iglauer Date: Thu, 2 Apr 2020 20:32:09 +0200 Subject: [PATCH 09/11] Delete settings.json --- resources/settings.json | 42 ----------------------------------------- 1 file changed, 42 deletions(-) delete mode 100644 resources/settings.json diff --git a/resources/settings.json b/resources/settings.json deleted file mode 100644 index 3825b07..0000000 --- a/resources/settings.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "comment": "Config file for myStrom Wifi Button 2 MQTT", - "version": 2, - "mqtt": { - "brokeraddress": "192.168.1.9", - "brokerport": 1883, - "brokerusername": "mqtt", - "brokerpassword": "mqtt-password", - - "brokerssl": false, - "cafilepath": "./resources/mqtt_client/ca.crt", - "certfilepath": "./resources/mqtt_client/client.crt", - "keyfilepath": "./resources/mqtt_client/client.key", - - "valid_topics" : [ - "api/mystrom" - ,"api/mystrom/gen" - ], - "subscribed_topics" : [ - ] - }, - "mystrom": { - "button":{ - "60019427E1C7" : {"name":"axButton01"} - }, - "button+":{ - } - - }, - "homeassistant": { - "discoveryprefix": "hass" - }, - "http" : { - "port" : 8321, - "ssl" : false, - "certfilepath" : "./resources/http_server/server.crt", - "keyfilepath" : "./resources/http_server/server.key", - "valid_users" : { - "user1" : "pwd1" - } - } -} From 9858797fbcbd98ccdc7b8a7031a480debc37b561 Mon Sep 17 00:00:00 2001 From: djax666 <35227325+djax666@users.noreply.github.com> Date: Sat, 4 Apr 2020 02:38:52 +0200 Subject: [PATCH 10/11] Move Docker section & update the discovery topics Moving the Docker section with the running options and renaming it "Running mystrombutton2mqtt with Docker" . Updating the discovery topics with the "prefix" concept --- README.md | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index aa424c4..8c189a8 100644 --- a/README.md +++ b/README.md @@ -131,18 +131,18 @@ The following topics for each button will be published: ```python ## The Discovery Topics ## -------------------- -homeassistant/sensor/myStrom/[BUTTON_MAC]_battery/config +[PREFIX]/sensor/myStrom/[BUTTON_MAC]_battery/config # only if it's a "Button Plus" -homeassistant/sensor/myStrom/[BUTTON_MAC]_wheel/config -homeassistant/sensor/myStrom/[BUTTON_MAC]_level/config +[PREFIX]/sensor/myStrom/[BUTTON_MAC]_wheel/config +[PREFIX]/sensor/myStrom/[BUTTON_MAC]_level/config -homeassistant/binary_sensor/myStrom/[BUTTON_MAC]_single/config -homeassistant/binary_sensor//myStrom/[BUTTON_MAC]_double/config -homeassistant/binary_sensor/myStrom/[BUTTON_MAC]_long/config +[PREFIX]/binary_sensor/myStrom/[BUTTON_MAC]_single/config +[PREFIX]/binary_sensor//myStrom/[BUTTON_MAC]_double/config +[PREFIX]/binary_sensor/myStrom/[BUTTON_MAC]_long/config # only if it's a "Button Plus" -homeassistant/binary_sensor/myStrom/[BUTTON_MAC]_touch/config -homeassistant/binary_sensor/myStrom/[BUTTON_MAC]_final_wheel/config -homeassistant/binary_sensor/myStrom/[BUTTON_MAC]_level/config +[PREFIX]/binary_sensor/myStrom/[BUTTON_MAC]_touch/config +[PREFIX]/binary_sensor/myStrom/[BUTTON_MAC]_final_wheel/config +[PREFIX]/binary_sensor/myStrom/[BUTTON_MAC]_level/config ## The state topics ## ---------------- myStrom/wifi_buttons/[CHOOSEN_NAME]_[BUTTON_MAC]/single @@ -155,6 +155,9 @@ myStrom/wifi_buttons/[CHOOSEN_NAME]_[BUTTON_MAC]/wheel myStrom/wifi_buttons/[CHOOSEN_NAME]_[BUTTON_MAC]/wheel_final myStrom/wifi_buttons/[CHOOSEN_NAME]_[BUTTON_MAC]/level ``` +__\[PREFIX\]__ will be the prefix you specify in the ./resources/settings.json + +For HomeAssistant, according to the [documentation](https://www.home-assistant.io/docs/mqtt/discovery/#discovery_prefix), the default discovery prefix is __homeassistant__ ### On Home Assistant On Home Assistant, you should have the same __\[MQTT_BROKER_IP\]__ in your "configuration.yaml": @@ -206,6 +209,10 @@ Check if it's active: sudo systemctl status mystrombutton2mqtt.serviceca ``` +## Running mystrombutton2mqtt with Docker + +The included `Dockerfile` will build a basic image off `python:3`. Running `docker-compose up` will build and start the image, and will mount the `resources/settings.json` file. + ## Home Assistant Automations ![alt text](https://raw.githubusercontent.com/djax666/mystrombutton2mqtt/master/static/button_automation.png "Wifi Button Automations") @@ -327,10 +334,6 @@ But if you want a real time action for adjusting the brightness of a light, you service: light.turn_on ``` -## Docker - -The included `Dockerfile` will build a basic image off `python:3`. Running `docker-compose up` will build and start the image, and will mount the `resources/settings.json` file. - ## Postscritum You can find the the API doc here: [https://api.mystrom.ch/?version=latest](https://api.mystrom.ch/?version=latest) From 721766471921d5bf50c0c9df5bf0e2cc6a353794 Mon Sep 17 00:00:00 2001 From: djax666 <35227325+djax666@users.noreply.github.com> Date: Sat, 4 Apr 2020 02:59:07 +0200 Subject: [PATCH 11/11] version no 3 and place of the PREFIX The structure of the settings.json has changed by adding a new mandatory setting "discoveryprefix", so the version has to increment. The discoveryprefix will impact the MQTT topic, so I prefer to put in the mqtt subsection. --- mystrombutton2mqtt.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mystrombutton2mqtt.py b/mystrombutton2mqtt.py index 729db13..44b2043 100644 --- a/mystrombutton2mqtt.py +++ b/mystrombutton2mqtt.py @@ -226,11 +226,11 @@ def publish_discovery(): print("The version of the settings must be specified.") exit(1) - if settings["version"] != 2: - print('Please update the settings file to the version 2') + if settings["version"] != 3: + print('Please update the settings file to the version 3') exit(1) - PREFIX = settings["homeassistant"]["discoveryprefix"] + PREFIX = settings["mqtt"]["discoveryprefix"] # users # for user in settings["http"]["valid_users"]: # VALID_USERS[user] = settings["http"]["valid_users"][user]