Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docker support - closes #14 #1

Merged
merged 12 commits into from
Apr 4, 2020
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

resources/settings.json

# User-specific files
*.rsuser
*.suo
Expand Down
15 changes: 15 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
25 changes: 16 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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":
Expand Down Expand Up @@ -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")
Expand Down
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3'
services:
app:
image: mystrombutton2mqtt
build:
context: .
dockerfile: Dockerfile
ports:
- 8321:8321
volumes:
- "./:/app"
37 changes: 20 additions & 17 deletions mystrombutton2mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@

SUBSCRIBED_TOPICS = dict()

PREFIX = ""


def mqtt_message_callback(msg_topic, msg_payload):
if msg_topic in SUBSCRIBED_TOPICS:
Expand Down Expand Up @@ -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:
Expand All @@ -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 )

Expand All @@ -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+'",'

Expand All @@ -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 """
Expand All @@ -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]
Expand Down