Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
cekk committed Nov 30, 2019
0 parents commit 41ed321
Show file tree
Hide file tree
Showing 6 changed files with 248 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
instance
.vscode
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: install
install:
pipenv install

.PHONY: dev
dev:
pipenv run python app.py
14 changes: 14 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
flask = "*"
flask-mqtt = "*"
flask-restful = "*"

[requires]
python_version = "3.7"
140 changes: 140 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Roller Shutter

TODO
82 changes: 82 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from flask import Flask
from flask_restful import Resource, Api, reqparse
from flask_mqtt import Mqtt

app = Flask(__name__, instance_relative_config=True)
app.config.from_pyfile("config.py")
api = Api(app)
mqtt = Mqtt(app)

parser = reqparse.RequestParser()
parser.add_argument("command", type=str, help="")


@mqtt.on_connect()
def handle_connect(client, userdata, flags, rc):
mqtt.subscribe("shellies/command")
for blind in app.config.get("BLINDS", []):
# mqtt.subscribe("shellies/{id}".format(id=blind.get("id", "")))
mqtt.subscribe("shellies/{id}/online".format(id=blind.get("id", "")))
mqtt.subscribe("shellies/{id}/roller/0/pos".format(id=blind.get("id", "")))
mqtt.subscribe("shellies/{id}/roller/0".format(id=blind.get("id", "")))


@mqtt.on_message()
def handle_mqtt_message(client, userdata, message):
print("{} - {}".format(message.topic, message.payload))


class HomePage(Resource):
def get(self):
return app.config.get("BLINDS", [])


class Announce(Resource):
def get(self):
# force all shellies to announce their status
mqtt.publish("shellies/command", "announce")
return "", 204


class Update(Resource):
def get(self):
# force all shellies to announce their status
mqtt.publish("shellies/command", "update")
return "", 204


class Open(Resource):
def get(self, id):
mqtt.publish("shellies/{id}/roller/0/command".format(id=id), "open")
return "", 204


class Close(Resource):
def get(self, id):
mqtt.publish("shellies/{id}/roller/0/command".format(id=id), "close")
return "", 204


class Stop(Resource):
def get(self, id):
mqtt.publish("shellies/{id}/roller/0/command".format(id=id), "stop")
return "", 204


class Command(Resource):
def post(self):
args = parser.parse_args()
mqtt.publish("shellies/shellyswitch25-68E5F8/roller/0/command", "close")
return {todo_id: todos[todo_id]}


api.add_resource(HomePage, "/")
api.add_resource(Announce, "/announce")
api.add_resource(Update, "/update")
api.add_resource(Command, "/command")
api.add_resource(Close, "/<string:id>/close")
api.add_resource(Open, "/<string:id>/open")
api.add_resource(Stop, "/<string:id>/stop")

if __name__ == "__main__":
app.run(debug=False)

0 comments on commit 41ed321

Please sign in to comment.