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 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/README.md b/README.md index b024688..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") 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 diff --git a/mystrombutton2mqtt.py b/mystrombutton2mqtt.py index d64f0b5..44b2043 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,28 +178,28 @@ 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) 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 """ @@ -224,10 +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["mqtt"]["discoveryprefix"] # users # for user in settings["http"]["valid_users"]: # VALID_USERS[user] = settings["http"]["valid_users"][user]