diff --git a/.coveragerc b/.coveragerc new file mode 100644 index 0000000..e568bab --- /dev/null +++ b/.coveragerc @@ -0,0 +1,28 @@ +# .coveragerc to control coverage.py +[run] +branch = True +source = openwebnet +# omit = bad_file.py + +[paths] +source = + src/ + */site-packages/ + +[report] +# Regexes for lines to exclude from consideration +exclude_lines = + # Have to re-enable the standard pragma + pragma: no cover + + # Don't complain about missing debug-only code: + def __repr__ + if self\.debug + + # Don't complain if tests don't hit defensive assertion code: + raise AssertionError + raise NotImplementedError + + # Don't complain if non-runnable code isn't run: + if 0: + if __name__ == .__main__.: diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..53bfc8b --- /dev/null +++ b/.gitignore @@ -0,0 +1,53 @@ +# Avoid accidental commit of real credentials +reopenwebnet_config.yml + +# Temporary and binary files +*~ +*.py[cod] +*.so +*.cfg +!.isort.cfg +!setup.cfg +*.orig +*.log +*.pot +__pycache__/* +.cache/* +.*.swp +*/.ipynb_checkpoints/* + +# Project files +.ropeproject +.project +.pydevproject +.settings +.idea +tags + +# Package files +*.egg +*.eggs/ +.installed.cfg +*.egg-info + +# Unittest and coverage +htmlcov/* +.coverage +.tox +junit.xml +coverage.xml +.pytest_cache/ + +# Build and docs folder/files +build/* +dist/* +sdist/* +docs/api/* +docs/_rst/* +docs/_build/* +cover/* +MANIFEST + +# Per-project virtualenvs +.venv*/ +venv/ diff --git a/AUTHORS.rst b/AUTHORS.rst new file mode 100644 index 0000000..1fdbd17 --- /dev/null +++ b/AUTHORS.rst @@ -0,0 +1,4 @@ +# Contributors + +* Pippocla (GitHub https://github.com/pippocla) +* Karel Vervaeke diff --git a/CHANGELOG.rst b/CHANGELOG.rst new file mode 100644 index 0000000..226e6f5 --- /dev/null +++ b/CHANGELOG.rst @@ -0,0 +1,10 @@ +========= +Changelog +========= + +Version 0.1 +=========== + +- Feature A added +- FIX: nasty bug #1729 fixed +- add your changes here! diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..2d0e8da --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) 2019 karel1980 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/OpenWebNet.py b/OpenWebNet.py deleted file mode 100644 index fbde638..0000000 --- a/OpenWebNet.py +++ /dev/null @@ -1,358 +0,0 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- -import socket - -""" -Read Write class for OpenWebNet bus -""" - -class OpenWebNet(object): - - #OK message from bus - ACK = '*#*1##' - #Non OK message from bus - NACK = '*#*0##' - #OpenWeb string for open a command session - CMD_SESSION = '*99*0##' - #OpenWeb string for open an event session - EVENT_SESSION = '*99*1##' - - #Init metod - def __init__(self,host,port,password): - self._host = host - self._port = int(port) - self._psw = password - self._session = False - self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - - - #Connection with host - def connection(self): - self._socket.connect((self._host,self._port)) - print('connection') - - #Send data to host - def send_data(self,data): - self._socket.send(data.encode()) - - #Read data from host - def read_data(self): - return str(self._socket.recv(1024).decode()) - -#Calculate the password to start operation - def calculated_psw (self, nonce): - m_1 = 0xFFFFFFFF - m_8 = 0xFFFFFFF8 - m_16 = 0xFFFFFFF0 - m_128 = 0xFFFFFF80 - m_16777216 = 0XFF000000 - flag = True - num1 = 0 - num2 = 0 - self._psw = int(self._psw) - - for c in nonce: - num1 = num1 & m_1 - num2 = num2 & m_1 - if c == '1': - length = not flag - if not length: - num2 = self._psw - num1 = num2 & m_128 - num1 = num1 >> 7 - num2 = num2 << 25 - num1 = num1 + num2 - flag = False - elif c == '2': - length = not flag - if not length: - num2 = self._psw - num1 = num2 & m_16 - num1 = num1 >> 4 - num2 = num2 << 28 - num1 = num1 + num2 - flag = False - elif c == '3': - length = not flag - if not length: - num2 = self._psw - num1 = num2 & m_8 - num1 = num1 >> 3 - num2 = num2 << 29 - num1 = num1 + num2 - flag = False - elif c == '4': - length = not flag - - if not length: - num2 = self._psw - num1 = num2 << 1 - num2 = num2 >> 31 - num1 = num1 + num2 - flag = False - elif c == '5': - length = not flag - if not length: - num2 = self._psw - num1 = num2 << 5 - num2 = num2 >> 27 - num1 = num1 + num2 - flag = False - elif c == '6': - length = not flag - if not length: - num2 = self._psw - num1 = num2 << 12 - num2 = num2 >> 20 - num1 = num1 + num2 - flag = False - elif c == '7': - length = not flag - if not length: - num2 = self._psw - num1 = num2 & 0xFF00 - num1 = num1 + (( num2 & 0xFF ) << 24 ) - num1 = num1 + (( num2 & 0xFF0000 ) >> 16 ) - num2 = ( num2 & m_16777216 ) >> 8 - num1 = num1 + num2 - flag = False - elif c == '8': - length = not flag - if not length: - num2 = self._psw - num1 = num2 & 0xFFFF - num1 = num1 << 16 - num1 = num1 + ( num2 >> 24 ) - num2 = num2 & 0xFF0000 - num2 = num2 >> 8 - num1 = num1 + num2 - flag = False - elif c == '9': - length = not flag - if not length: - num2 = self._psw - num1 = ~num2 - flag = False - else: - num1 = num2 - num2 = num1 - return num1 & m_1 - - #Open command session - def cmd_session(self): - #create the connection - self.connection() - - #if the bus answer with a NACK report the error - if self.read_data() == OpenWebNet.NACK : - _LOGGER.exception("Non posso inizializzare la comunicazione con il gateway") - - #open commanc session - self.send_data(OpenWebNet.CMD_SESSION) - - answer = self.read_data() - #if the bus answer with a NACK report the error - if answer == OpenWebNet.NACK: - _LOGGER.exception("Il gateway rifiuta la sessione comandi") - return False - - #calculate the psw - psw_open = '*#' + str(self.calculated_psw(answer)) + '##' - - #send the password - self.send_data(psw_open) - - #if the bus answer with a NACK report the error - if self.read_data() == OpenWebNet.NACK: - _LOGGER.exception("Password errata") - - #othefwise set the variable to True - else: - self._session = True - print('cmd_session') - - - #Extractor for the answer from the bus - def extractor(self,answer): - value_list = [] - print('estrattore riceve',answer) - #scan on all the caracters on the answer - index = 0 - while index <= len(answer) - 1: - print('index',index) - if answer[index] != '*' and answer[index] != '#': - lenght = 0 - val = '' - while lenght <= len(answer) - 1 - index: - if answer[index + lenght] != '*' and answer[index + lenght] != '#': - lenght = lenght +1 - print('lenght',lenght) - else: - break - print('aggiungo a val',answer[index:index + lenght]) - val = val + answer[index:index + lenght] - print('val',val) - value_list.append(val) - print('value_list',value_list) - index = index + lenght - lenght = 0 - index = index + 1 - print(value_list) - return value_list - - - #Check that bus send al the data - def check_answer (self,message): - #if final part of the message is not and ACK or NACK - end_message = '' - print('message ricevuto da check answer', message) - print('OpenWebNet.ACK',OpenWebNet.ACK) - if message[len(message)- 6:] != OpenWebNet.ACK and message[len(message)- 6:] != OpenWebNet.NACK: - #the answer is not completed, read again from bus - print('message -len',message[len(message)-6:]) - end_message = self.read_data() - #add it - - print('message +end message',message + end_message) - return message + end_message - - #check if I get a NACK - if message[len(message)- 6:] == OpenWebNet.NACK: - _LOGGER.exception("Errore Comando non effettuato") - - - return message - - - #Normal request to BUS - def normal_request(self,who,where,what): - - #if the command session is not active - if not self._session: - self.cmd_session() - - #prepare the request - normal_request = '*' + who + '*' + what + '*' + where + '##' - - #and send - self.send_data(normal_request) - - #read the answer - message = self.read_data() - - #check if I get a NACK - if message == OpenWebNet.NACK: - _LOGGER.exception("Errore Comando non effettuato") - - - #Request of state of a components on the bus - def stato_request(self,who,where): - print('stato request)') - #if the command session is not active - if not self._session: - self.cmd_session() - - #preparo la richiesta - stato_request = '*#' + who + '*' + where + '##' - print('richiesta',stato_request) - #e la Invio - self.send_data(stato_request) - - #e leggo la risposta - message = self.read_data() - print('messagge',message) - #verifico se il bus ha trasmesso tutti i dati - check_message = self.check_answer(message) - - #verifico se ho ricevuto un NACK - if message[len(message)- 6:] == OpenWebNet.NACK: - _LOGGER.exception("Errore Comando non effettuato") - #o un ACK - else: - #nel qual caso estraggo i dati della risposta e li restituisco sotto forma di lista - return self.extractor(check_message[:len(check_message) - 6]) - - #Richiesta grandezza - def grandezza_request(self,who,where, grandezza): - #Se non è attiva apro sessione comandi - if not self._session: - self.cmd_session() - - #preparo la richiesta - grandezza_request = '*#' + who + '*' + where + '*' + grandezza + '##' - - #e la Invio - self.send_data(grandezza_request) - - #e leggo la risposta - message = self.read_data() - - #verifico se il bus ha trasmesso tutti i dati - check_message = self.check_answer(message) - - #verifico se ho ricevuto un NACK - if message[len(message)- 6:] == OpenWebNet.NACK: - _LOGGER.exception("Errore Comando non effettuato") - #o un ACK - else: - #nel qual caso estraggo i dati della risposta e li restituisco sotto forma di lista - return self.extractor(check_message[:len(check_message) - 6]) - - #Scrittura di una grandezza - def grandezza_write(self,who,where,grandezza,valori): - #Se non è attiva apro sessione comandi - if not self._session: - self.cmd_session() - - #preparo la richiesta - val ='' - for item in valori: - val = '*' + val[item] - - grandezza_write = '*#' + who + '*' + where + '*#' + grandezza + val + '##' - - #e la Invio - self.send_data(stato_request) - - #e leggo la risposta - return self.read_data() - - #metodo che invia il comando di accensione della luce where sul bus - def luce_on (self,where): - self.normal_request('1',where,'1') - - #metodo che invia il comandi di spegnimento della luce where sul bus - def luce_off(self,where): - self.normal_request('1',where,'0') - - #metodo per la richiesta dello stato della luce where sul bus - def stato_luce(self,where): - print('stato_luce') - stato = self.stato_request('1',where) - - if stato[1] == '1': - return True - else: - return False - - #Metodo per la lettura della temperatura - def read_temperature(self,where): - print('lettura temperatura') - temperatura = self.grandezza_request('4',where,'0') - return float(temperatura[3])/10.0 - - #Metodo per la lettura della temperatura settata nella sonda - def read_setTemperature(self,where): - print('lettura set temperature') - setTemperatura = self.grandezza_request('4',where,'14') - return float(setTemperatura[3])/10.0 - - #Metodo per la lettura dello stato della elettrovalvola - def read_sondaStatus(self,where): - print('lettura stato sonda temperature') - stato_sonda = self.grandezza_request('4',where,'19') - print('stato sonda',stato_sonda[4]) - if stato_sonda[4] == '0': - return 'OFF' - else: - return 'ON' diff --git a/README.md b/README.md index 770e0bd..52df365 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,36 @@ -# OpenWebNet -TEST Python Class for interaction with OpenWebNet bus +# ReOpenWebNet + +ReOpenWebNet is a library communicating with an OpenWebNet gateway. It supports event sessions and command sessions. +OpenWebNet is a communication protocol developed by Bticino, to enable communication between devices of its home automation product suite 'MyHome'. For more information about OpenWebNet, see https://www.myopen-legrandgroup.com/developers/ + +This project started as a fork from https://github.com/pippocla/openwebnet + +## Features + +- Asynchronous components for interacting with the gateway. +- A bridge between openwebnet and mqtt; At the moment only light switches/actuators ('who=1') are supported. If you want to see support for other things, please reach out via GitHub. + +## Example scripts + +Note: before running these examples, change the constants declared at the top of these script. + +- `examples/event_session.py`: When running this script you should see openwebnet events being logged to the command line as they happen. +- `examples/command_session.py`: Running the script should toggle a light on and off 5 times with 1 second intervals. + +## MQTT Bridge + +See `bin/openwebnet-mqtt-bridge`. + +This bridge communicates with an openwebnet service over http and and mqtt service. +This should make it easier to interact with openwebnet in various tools (OpenHAB, Homeassistant, Node-Red) + +### Configuration + +The MQTT bridge is configured via $HOME/.reopenwebnet/config.yaml +See reopenwebnet_config.yml.sample for an example + +## Releasing + + git tag x.y.z + python setup.py build + twine upload dist/reopenwebnet-x.y.z-py2.py3-none-any.whl diff --git a/bin/openwebnet-mqtt-bridge b/bin/openwebnet-mqtt-bridge new file mode 100755 index 0000000..b2eb10e --- /dev/null +++ b/bin/openwebnet-mqtt-bridge @@ -0,0 +1,3 @@ +#!/bin/sh + +python -mreopenwebnet.mqtt diff --git a/docs/Makefile b/docs/Makefile new file mode 100644 index 0000000..eef2496 --- /dev/null +++ b/docs/Makefile @@ -0,0 +1,193 @@ +# Makefile for Sphinx documentation +# + +# You can set these variables from the command line. +SPHINXOPTS = +SPHINXBUILD = sphinx-build +PAPER = +BUILDDIR = _build +AUTODOCDIR = api +AUTODOCBUILD = sphinx-apidoc +PROJECT = reopenwebnet +MODULEDIR = ../src/reopenwebnet + +# User-friendly check for sphinx-build +ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $?), 1) +$(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) +endif + +# Internal variables. +PAPEROPT_a4 = -D latex_paper_size=a4 +PAPEROPT_letter = -D latex_paper_size=letter +ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . +# the i18n builder cannot share the environment and doctrees with the others +I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . + +.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest gettext doc-requirements + +help: + @echo "Please use \`make ' where is one of" + @echo " html to make standalone HTML files" + @echo " dirhtml to make HTML files named index.html in directories" + @echo " singlehtml to make a single large HTML file" + @echo " pickle to make pickle files" + @echo " json to make JSON files" + @echo " htmlhelp to make HTML files and a HTML help project" + @echo " qthelp to make HTML files and a qthelp project" + @echo " devhelp to make HTML files and a Devhelp project" + @echo " epub to make an epub" + @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" + @echo " latexpdf to make LaTeX files and run them through pdflatex" + @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" + @echo " text to make text files" + @echo " man to make manual pages" + @echo " texinfo to make Texinfo files" + @echo " info to make Texinfo files and run them through makeinfo" + @echo " gettext to make PO message catalogs" + @echo " changes to make an overview of all changed/added/deprecated items" + @echo " xml to make Docutils-native XML files" + @echo " pseudoxml to make pseudoxml-XML files for display purposes" + @echo " linkcheck to check all external links for integrity" + @echo " doctest to run all doctests embedded in the documentation (if enabled)" + +clean: + rm -rf $(BUILDDIR)/* $(AUTODOCDIR) + +$(AUTODOCDIR): $(MODULEDIR) + mkdir -p $@ + $(AUTODOCBUILD) -f -o $@ $^ + +doc-requirements: $(AUTODOCDIR) + +html: doc-requirements + $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." + +dirhtml: doc-requirements + $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml + @echo + @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." + +singlehtml: doc-requirements + $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml + @echo + @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." + +pickle: doc-requirements + $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle + @echo + @echo "Build finished; now you can process the pickle files." + +json: doc-requirements + $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json + @echo + @echo "Build finished; now you can process the JSON files." + +htmlhelp: doc-requirements + $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp + @echo + @echo "Build finished; now you can run HTML Help Workshop with the" \ + ".hhp project file in $(BUILDDIR)/htmlhelp." + +qthelp: doc-requirements + $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp + @echo + @echo "Build finished; now you can run "qcollectiongenerator" with the" \ + ".qhcp project file in $(BUILDDIR)/qthelp, like this:" + @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/$(PROJECT).qhcp" + @echo "To view the help file:" + @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/$(PROJECT).qhc" + +devhelp: doc-requirements + $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp + @echo + @echo "Build finished." + @echo "To view the help file:" + @echo "# mkdir -p $HOME/.local/share/devhelp/$(PROJECT)" + @echo "# ln -s $(BUILDDIR)/devhelp $HOME/.local/share/devhelp/$(PROJEC)" + @echo "# devhelp" + +epub: doc-requirements + $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub + @echo + @echo "Build finished. The epub file is in $(BUILDDIR)/epub." + +patch-latex: + find _build/latex -iname "*.tex" | xargs -- \ + sed -i'' 's~includegraphics{~includegraphics\[keepaspectratio,max size={\\textwidth}{\\textheight}\]{~g' + +latex: doc-requirements + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + $(MAKE) patch-latex + @echo + @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." + @echo "Run \`make' in that directory to run these through (pdf)latex" \ + "(use \`make latexpdf' here to do that automatically)." + +latexpdf: doc-requirements + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + $(MAKE) patch-latex + @echo "Running LaTeX files through pdflatex..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +latexpdfja: doc-requirements + $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex + @echo "Running LaTeX files through platex and dvipdfmx..." + $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja + @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." + +text: doc-requirements + $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text + @echo + @echo "Build finished. The text files are in $(BUILDDIR)/text." + +man: doc-requirements + $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man + @echo + @echo "Build finished. The manual pages are in $(BUILDDIR)/man." + +texinfo: doc-requirements + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo + @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." + @echo "Run \`make' in that directory to run these through makeinfo" \ + "(use \`make info' here to do that automatically)." + +info: doc-requirements + $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo + @echo "Running Texinfo files through makeinfo..." + make -C $(BUILDDIR)/texinfo info + @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." + +gettext: doc-requirements + $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale + @echo + @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." + +changes: doc-requirements + $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes + @echo + @echo "The overview file is in $(BUILDDIR)/changes." + +linkcheck: doc-requirements + $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck + @echo + @echo "Link check complete; look for any errors in the above output " \ + "or in $(BUILDDIR)/linkcheck/output.txt." + +doctest: doc-requirements + $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest + @echo "Testing of doctests in the sources finished, look at the " \ + "results in $(BUILDDIR)/doctest/output.txt." + +xml: doc-requirements + $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml + @echo + @echo "Build finished. The XML files are in $(BUILDDIR)/xml." + +pseudoxml: doc-requirements + $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml + @echo + @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." diff --git a/docs/_static/.gitignore b/docs/_static/.gitignore new file mode 100644 index 0000000..3c96363 --- /dev/null +++ b/docs/_static/.gitignore @@ -0,0 +1 @@ +# Empty directory diff --git a/docs/authors.rst b/docs/authors.rst new file mode 100644 index 0000000..cd8e091 --- /dev/null +++ b/docs/authors.rst @@ -0,0 +1,2 @@ +.. _authors: +.. include:: ../AUTHORS.rst diff --git a/docs/changelog.rst b/docs/changelog.rst new file mode 100644 index 0000000..871950d --- /dev/null +++ b/docs/changelog.rst @@ -0,0 +1,2 @@ +.. _changes: +.. include:: ../CHANGELOG.rst diff --git a/docs/conf.py b/docs/conf.py new file mode 100644 index 0000000..7cdc4ec --- /dev/null +++ b/docs/conf.py @@ -0,0 +1,272 @@ +# -*- coding: utf-8 -*- +# +# This file is execfile()d with the current directory set to its containing dir. +# +# Note that not all possible configuration values are present in this +# autogenerated file. +# +# All configuration values have a default; values that are commented out +# serve to show the default. + +import os +import sys +import inspect +import shutil + +__location__ = os.path.join(os.getcwd(), os.path.dirname( + inspect.getfile(inspect.currentframe()))) + +# If extensions (or modules to document with autodoc) are in another directory, +# add these directories to sys.path here. If the directory is relative to the +# documentation root, use os.path.abspath to make it absolute, like shown here. +sys.path.insert(0, os.path.join(__location__, '../src')) + +# -- Run sphinx-apidoc ------------------------------------------------------ +# This hack is necessary since RTD does not issue `sphinx-apidoc` before running +# `sphinx-build -b html . _build/html`. See Issue: +# https://github.com/rtfd/readthedocs.org/issues/1139 +# DON'T FORGET: Check the box "Install your project inside a virtualenv using +# setup.py install" in the RTD Advanced Settings. +# Additionally it helps us to avoid running apidoc manually + +try: # for Sphinx >= 1.7 + from sphinx.ext import apidoc +except ImportError: + from sphinx import apidoc + +output_dir = os.path.join(__location__, "api") +module_dir = os.path.join(__location__, "../src/reopenwebnet") +try: + shutil.rmtree(output_dir) +except FileNotFoundError: + pass + +try: + import sphinx + from pkg_resources import parse_version + + cmd_line_template = "sphinx-apidoc -f -o {outputdir} {moduledir}" + cmd_line = cmd_line_template.format(outputdir=output_dir, moduledir=module_dir) + + args = cmd_line.split(" ") + if parse_version(sphinx.__version__) >= parse_version('1.7'): + args = args[1:] + + apidoc.main(args) +except Exception as e: + print("Running `sphinx-apidoc` failed!\n{}".format(e)) + +# -- General configuration ----------------------------------------------------- + +# If your documentation needs a minimal Sphinx version, state it here. +# needs_sphinx = '1.0' + +# Add any Sphinx extension module names here, as strings. They can be extensions +# coming with Sphinx (named 'sphinx.ext.*') or your custom ones. +extensions = ['sphinx.ext.autodoc', 'sphinx.ext.intersphinx', 'sphinx.ext.todo', + 'sphinx.ext.autosummary', 'sphinx.ext.viewcode', 'sphinx.ext.coverage', + 'sphinx.ext.doctest', 'sphinx.ext.ifconfig', 'sphinx.ext.mathjax', + 'sphinx.ext.napoleon'] + +# Add any paths that contain templates here, relative to this directory. +templates_path = ['_templates'] + +# The suffix of source filenames. +source_suffix = '.rst' + +# The encoding of source files. +# source_encoding = 'utf-8-sig' + +# The master toctree document. +master_doc = 'index' + +# General information about the project. +project = u'reopenwebnet' +copyright = u'2019, karel1980' + +# The version info for the project you're documenting, acts as replacement for +# |version| and |release|, also used in various other places throughout the +# built documents. +# +# The short X.Y version. +version = '' # Is set by calling `setup.py docs` +# The full version, including alpha/beta/rc tags. +release = '' # Is set by calling `setup.py docs` + +# The language for content autogenerated by Sphinx. Refer to documentation +# for a list of supported languages. +# language = None + +# There are two options for replacing |today|: either, you set today to some +# non-false value, then it is used: +# today = '' +# Else, today_fmt is used as the format for a strftime call. +# today_fmt = '%B %d, %Y' + +# List of patterns, relative to source directory, that match files and +# directories to ignore when looking for source files. +exclude_patterns = ['_build'] + +# The reST default role (used for this markup: `text`) to use for all documents. +# default_role = None + +# If true, '()' will be appended to :func: etc. cross-reference text. +# add_function_parentheses = True + +# If true, the current module name will be prepended to all description +# unit titles (such as .. function::). +# add_module_names = True + +# If true, sectionauthor and moduleauthor directives will be shown in the +# output. They are ignored by default. +# show_authors = False + +# The name of the Pygments (syntax highlighting) style to use. +pygments_style = 'sphinx' + +# A list of ignored prefixes for module index sorting. +# modindex_common_prefix = [] + +# If true, keep warnings as "system message" paragraphs in the built documents. +# keep_warnings = False + + +# -- Options for HTML output --------------------------------------------------- + +# The theme to use for HTML and HTML Help pages. See the documentation for +# a list of builtin themes. +html_theme = 'alabaster' + +# Theme options are theme-specific and customize the look and feel of a theme +# further. For a list of options available for each theme, see the +# documentation. +html_theme_options = { + 'sidebar_width': '300px', + 'page_width': '1200px' +} + +# Add any paths that contain custom themes here, relative to this directory. +# html_theme_path = [] + +# The name for this set of Sphinx documents. If None, it defaults to +# " v documentation". +try: + from reopenwebnet import __version__ as version +except ImportError: + pass +else: + release = version + +# A shorter title for the navigation bar. Default is the same as html_title. +# html_short_title = None + +# The name of an image file (relative to this directory) to place at the top +# of the sidebar. +# html_logo = "" + +# The name of an image file (within the static path) to use as favicon of the +# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 +# pixels large. +# html_favicon = None + +# Add any paths that contain custom static files (such as style sheets) here, +# relative to this directory. They are copied after the builtin static files, +# so a file named "default.css" will overwrite the builtin "default.css". +html_static_path = ['_static'] + +# If not '', a 'Last updated on:' timestamp is inserted at every page bottom, +# using the given strftime format. +# html_last_updated_fmt = '%b %d, %Y' + +# If true, SmartyPants will be used to convert quotes and dashes to +# typographically correct entities. +# html_use_smartypants = True + +# Custom sidebar templates, maps document names to template names. +# html_sidebars = {} + +# Additional templates that should be rendered to pages, maps page names to +# template names. +# html_additional_pages = {} + +# If false, no module index is generated. +# html_domain_indices = True + +# If false, no index is generated. +# html_use_index = True + +# If true, the index is split into individual pages for each letter. +# html_split_index = False + +# If true, links to the reST sources are added to the pages. +# html_show_sourcelink = True + +# If true, "Created using Sphinx" is shown in the HTML footer. Default is True. +# html_show_sphinx = True + +# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. +# html_show_copyright = True + +# If true, an OpenSearch description file will be output, and all pages will +# contain a tag referring to it. The value of this option must be the +# base URL from which the finished HTML is served. +# html_use_opensearch = '' + +# This is the file name suffix for HTML files (e.g. ".xhtml"). +# html_file_suffix = None + +# Output file base name for HTML help builder. +htmlhelp_basename = 'reopenwebnet-doc' + + +# -- Options for LaTeX output -------------------------------------------------- + +latex_elements = { +# The paper size ('letterpaper' or 'a4paper'). +# 'papersize': 'letterpaper', + +# The font size ('10pt', '11pt' or '12pt'). +# 'pointsize': '10pt', + +# Additional stuff for the LaTeX preamble. +# 'preamble': '', +} + +# Grouping the document tree into LaTeX files. List of tuples +# (source start file, target name, title, author, documentclass [howto/manual]). +latex_documents = [ + ('index', 'user_guide.tex', u'Reopenwebnet Documentation', + u'Karel Vervaeke', 'manual'), +] + +# The name of an image file (relative to this directory) to place at the top of +# the title page. +# latex_logo = "" + +# For "manual" documents, if this is true, then toplevel headings are parts, +# not chapters. +# latex_use_parts = False + +# If true, show page references after internal links. +# latex_show_pagerefs = False + +# If true, show URL addresses after external links. +# latex_show_urls = False + +# Documents to append as an appendix to all manuals. +# latex_appendices = [] + +# If false, no module index is generated. +# latex_domain_indices = True + +# -- External mapping ------------------------------------------------------------ +python_version = '.'.join(map(str, sys.version_info[0:2])) +intersphinx_mapping = { + 'sphinx': ('http://www.sphinx-doc.org/en/stable', None), + 'python': ('https://docs.python.org/' + python_version, None), + 'matplotlib': ('https://matplotlib.org', None), + 'numpy': ('https://docs.scipy.org/doc/numpy', None), + 'sklearn': ('http://scikit-learn.org/stable', None), + 'pandas': ('http://pandas.pydata.org/pandas-docs/stable', None), + 'scipy': ('https://docs.scipy.org/doc/scipy/reference', None), +} diff --git a/docs/index.rst b/docs/index.rst new file mode 100644 index 0000000..4ebba24 --- /dev/null +++ b/docs/index.rst @@ -0,0 +1,57 @@ +# reopenwebnet + +This is the documentation of **reopenwebnet**. + +.. note:: + + This is the main page of your project's `Sphinx`_ documentation. + It is formatted in `reStructuredText`_. Add additional pages + by creating rst-files in ``docs`` and adding them to the `toctree`_ below. + Use then `references`_ in order to link them from this page, e.g. + :ref:`authors` and :ref:`changes`. + + It is also possible to refer to the documentation of other Python packages + with the `Python domain syntax`_. By default you can reference the + documentation of `Sphinx`_, `Python`_, `NumPy`_, `SciPy`_, `matplotlib`_, + `Pandas`_, `Scikit-Learn`_. You can add more by extending the + ``intersphinx_mapping`` in your Sphinx's ``conf.py``. + + The pretty useful extension `autodoc`_ is activated by default and lets + you include documentation from docstrings. Docstrings can be written in + `Google style`_ (recommended!), `NumPy style`_ and `classical style`_. + + +Contents +======== + +.. toctree:: + :maxdepth: 2 + + License + Authors + Changelog + Module Reference + + +Indices and tables +================== + +* :ref:`genindex` +* :ref:`modindex` +* :ref:`search` + +.. _toctree: http://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html +.. _reStructuredText: http://www.sphinx-doc.org/en/master/usage/restructuredtext/basics.html +.. _references: http://www.sphinx-doc.org/en/stable/markup/inline.html +.. _Python domain syntax: http://sphinx-doc.org/domains.html#the-python-domain +.. _Sphinx: http://www.sphinx-doc.org/ +.. _Python: http://docs.python.org/ +.. _Numpy: http://docs.scipy.org/doc/numpy +.. _SciPy: http://docs.scipy.org/doc/scipy/reference/ +.. _matplotlib: https://matplotlib.org/contents.html# +.. _Pandas: http://pandas.pydata.org/pandas-docs/stable +.. _Scikit-Learn: http://scikit-learn.org/stable +.. _autodoc: http://www.sphinx-doc.org/en/stable/ext/autodoc.html +.. _Google style: https://github.com/google/styleguide/blob/gh-pages/pyguide.md#38-comments-and-docstrings +.. _NumPy style: https://numpydoc.readthedocs.io/en/latest/format.html +.. _classical style: http://www.sphinx-doc.org/en/stable/domains.html#info-field-lists diff --git a/docs/license.rst b/docs/license.rst new file mode 100644 index 0000000..3989c51 --- /dev/null +++ b/docs/license.rst @@ -0,0 +1,7 @@ +.. _license: + +======= +License +======= + +.. include:: ../LICENSE.txt diff --git a/examples/command_session.py b/examples/command_session.py new file mode 100644 index 0000000..a7be8e2 --- /dev/null +++ b/examples/command_session.py @@ -0,0 +1,44 @@ +import asyncio +import logging + +from reopenwebnet import messages +from reopenwebnet.client import OpenWebNetClient + +logging.basicConfig(level=logging.DEBUG) + +HOST = '192.168.0.10' +PORT = 20000 +PASSWORD = '951753' +LIGHT_WHERE = 13 + + +async def schedule_stop(delay): + return asyncio.ensure_future(asyncio.sleep(delay)) + + +async def main(): + def on_event(*args): + print("got event", args) + + client = OpenWebNetClient(HOST, PORT, PASSWORD, messages.CMD_SESSION) + await client.start() + + # Play with the lights + for i in range(5): + await light_on(client) + await asyncio.sleep(1) + await light_off(client) + await asyncio.sleep(1) + + +async def light_off(client): + print("Light off") + client.send_message(messages.NormalMessage(1, 0, LIGHT_WHERE)) + + +async def light_on(client): + print("Light on") + client.send_message(messages.NormalMessage(1, 1, LIGHT_WHERE)) + + +asyncio.run(main()) diff --git a/examples/event_session.py b/examples/event_session.py new file mode 100644 index 0000000..deb8b80 --- /dev/null +++ b/examples/event_session.py @@ -0,0 +1,35 @@ +import asyncio +from asyncio import FIRST_COMPLETED + +from reopenwebnet import messages +from reopenwebnet.client import OpenWebNetClient + +HOST = '192.168.0.10' +PORT = 20000 +PASSWORD = '951753' + + +async def main(): + loop = asyncio.get_running_loop() + + # Start openwebnet protocol + def on_event(*args): + print("got event", args) + + client = OpenWebNetClient(HOST, PORT, PASSWORD, session_type=messages.EVENT_SESSION) + await client.start(on_event) + + # Schedule stop + delay = 60 + print("Listening for openwebnet events for %d seconds. Try switching a light on and off" % delay) + on_stop = asyncio.ensure_future(asyncio.sleep(delay)) + + # Wait until scheduled stop or connection loss + done, pending = await asyncio.wait([client.on_con_lost, on_stop], return_when=FIRST_COMPLETED) + if client.on_con_lost in done: + print("Connection lost") + if on_stop in done: + print("Scheduled stop") + + +asyncio.run(main()) diff --git a/reopenwebnet_config.yml.sample b/reopenwebnet_config.yml.sample new file mode 100644 index 0000000..2260bc9 --- /dev/null +++ b/reopenwebnet_config.yml.sample @@ -0,0 +1,12 @@ + +openwebnet: + host: 192.168.1.10 + # port: 20000 + password: '951753' + +mqtt: + host: localhost + # port: 1883 + # client_id: reopenwebnet + # user: + # password: diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..7aaf82b --- /dev/null +++ b/requirements.txt @@ -0,0 +1,19 @@ +nose +# ============================================================================= +# DEPRECATION WARNING: +# +# The file `requirements.txt` does not influence the package dependencies and +# will not be automatically created in the next version of PyScaffold (v4.x). +# +# Please have look at the docs for better alternatives +# (`Dependency Management` section). +# ============================================================================= +# +# Add your pinned requirements so that they can be easily installed with: +# pip install -r requirements.txt +# Remember to also add them in setup.cfg but unpinned. +# Example: +# numpy==1.13.3 +# scipy==1.0 + +paho-mqtt==1.5.0 diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 0000000..514327b --- /dev/null +++ b/setup.cfg @@ -0,0 +1,112 @@ +# This file is used to configure your project. +# Read more about the various options under: +# http://setuptools.readthedocs.io/en/latest/setuptools.html#configuring-setup-using-setup-cfg-files + +[metadata] +name = reopenwebnet +description = An OpenWebNet client +author = karel1980 +author_email = karel@vervaeke.info +license = mit +url = https://github.com/karel1980/reopenwebnet +long_description = file: README.md +# Change if running only on Windows, Mac or Linux (comma-separated) +platforms = any +# Add here all kinds of additional classifiers as defined under +# https://pypi.python.org/pypi?%3Aaction=list_classifiers +classifiers = + Development Status :: 4 - Beta + Programming Language :: Python + +[options] +zip_safe = False +packages = find: +include_package_data = True +package_dir = + =src +# DON'T CHANGE THE FOLLOWING LINE! IT WILL BE UPDATED BY PYSCAFFOLD! +setup_requires = pyscaffold>=3.1a0,<3.2a0 +# Add here dependencies of your project (semicolon/line-separated), e.g. +install_requires = pyyaml>=5.3;paho-mqtt>=1.5 +# The usage of test_requires is discouraged, see `Dependency Management` docs +tests_require = nose; pytest-cov +# Require a specific Python version, e.g. Python 2.7 or >= 3.4 +# python_requires = >=2.7,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.* +python_requires = >=3.7 + +[options.packages.find] +where = src +exclude = + tests + +[options.extras_require] +# Add here additional requirements for extra features, to install with: +# `pip install reopenwebnet[PDF]` like: +# PDF = ReportLab; RXP +# Add here test requirements (semicolon/line-separated) +testing = + pytest + pytest-cov + +[options.entry_points] +# Add here console scripts like: +# console_scripts = +# script_name = reopenwebnet.module:function +# For example: +# console_scripts = +# fibonacci = reopenwebnet.skeleton:run +# And any other entry points, for example: +# pyscaffold.cli = +# awesome = pyscaffoldext.awesome.extension:AwesomeExtension + +[test] +# py.test options when running `python setup.py test` +# addopts = --verbose +extras = True + +[tool:pytest] +# Options for py.test: +# Specify command line options as you would do when invoking py.test directly. +# e.g. --cov-report html (or xml) for html/xml output or --junitxml junit.xml +# in order to write a coverage file that can be read by Jenkins. +addopts = + --cov reopenwebnet --cov-report term-missing + --verbose +norecursedirs = + dist + build + .tox +testpaths = tests + +[aliases] +build = bdist_wheel +release = build upload + +[bdist_wheel] +# Use this option if your package is pure-python +universal = 1 + +[build_sphinx] +source_dir = docs +build_dir = docs/_build + +[devpi:upload] +# Options for the devpi: PyPI server and packaging tool +# VCS export must be deactivated since we are using setuptools-scm +no-vcs = 1 +formats = bdist_wheel + +[flake8] +# Some sane defaults for the code style checker flake8 +exclude = + .tox + build + dist + .eggs + docs/conf.py + +[pyscaffold] +# PyScaffold's parameters when the project was created. +# This will be used when updating. Do not change! +version = 3.1 +package = reopenwebnet diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..aff97f6 --- /dev/null +++ b/setup.py @@ -0,0 +1,28 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" + Setup file for Reopenwebnet. + Use setup.cfg to configure your project. + + This file was generated with PyScaffold 3.1. + PyScaffold helps you to put up the scaffold of your new Python project. + Learn more under: https://pyscaffold.org/ +""" +import sys + +from pkg_resources import require, VersionConflict +from setuptools import setup +from os import path + +try: + require('setuptools>=38.3') +except VersionConflict: + print("Error: version of setuptools is too old (<38.3)!") + sys.exit(1) + +this_directory = path.abspath(path.dirname(__file__)) +with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: + long_description = f.read() + +if __name__ == "__main__": + setup(use_pyscaffold=True, long_description=long_description) diff --git a/src/reopenwebnet/__init__.py b/src/reopenwebnet/__init__.py new file mode 100644 index 0000000..fc8efbb --- /dev/null +++ b/src/reopenwebnet/__init__.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +from pkg_resources import get_distribution, DistributionNotFound + +from reopenwebnet import messages, protocol + +__all__ = [messages, protocol] + +try: + # Change here if project is renamed and does not equal the package name + dist_name = __name__ + __version__ = get_distribution(dist_name).version +except DistributionNotFound: + __version__ = 'unknown' +finally: + del get_distribution, DistributionNotFound diff --git a/src/reopenwebnet/client.py b/src/reopenwebnet/client.py new file mode 100644 index 0000000..a053f32 --- /dev/null +++ b/src/reopenwebnet/client.py @@ -0,0 +1,40 @@ +import asyncio +import logging +import socket + +from reopenwebnet.protocol import OpenWebNetProtocol + +_LOGGER = logging.getLogger(__name__) + +class OpenWebNetClient: + def __init__(self, host, port, password, session_type, name = "openwebnet"): + self.host = host + self.port = port + self.password = password + self.transport = None + self.protocol = None + self.on_con_lost = None + self.session_type = session_type + self.name = name + + async def start(self, event_callback=None): + loop = asyncio.get_running_loop() + mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + mysock.connect((self.host, self.port)) + on_con_lost = loop.create_future() + on_session_start = loop.create_future() + + transport, protocol = await loop.create_connection( + lambda: OpenWebNetProtocol(self.session_type, self.password, on_session_start, event_callback, on_con_lost, name=self.name), + sock=mysock) + + await on_session_start + self.transport = transport + self.protocol = protocol + self.on_con_lost = on_con_lost + + def send_message(self, msg): + if self.protocol is None: + _LOGGER.error("Could not send message; Did you call client.start()?") + return + self.protocol.send_message(msg) diff --git a/src/reopenwebnet/config.py b/src/reopenwebnet/config.py new file mode 100644 index 0000000..062d3af --- /dev/null +++ b/src/reopenwebnet/config.py @@ -0,0 +1,35 @@ +import os + +import yaml +from yaml import SafeLoader + + +class Config: + def __init__(self, config_dict): + self.openwebnet = OpenWebNetConfig(config_dict['openwebnet']) if 'openwebnet' in config_dict else None + + self.mqtt = MqttConfig(config_dict['mqtt']) if 'mqtt' in config_dict else None + + +class OpenWebNetConfig: + def __init__(self, config_dict): + self.host = config_dict['host'] + self.port = config_dict.get('port', 20000) + self.password = config_dict.get('password', None) + + +class MqttConfig: + def __init__(self, config_dict): + self.host = config_dict['host'] + self.port = config_dict.get('port', 1883) + self.user = config_dict.get('user', None) + self.password = config_dict.get('password', None) + self.client_id = config_dict.get('client_id') + + +def read_environment_config(): + default_config_path = os.path.expanduser('~/.reopenwebnet/config.yaml') + config_path = os.environ.get('REOPENWEBNET_CONFIG', default_config_path) + + yml_config = yaml.load(open(config_path), Loader=SafeLoader) + return Config(yml_config) diff --git a/src/reopenwebnet/messages.py b/src/reopenwebnet/messages.py new file mode 100644 index 0000000..fc38b79 --- /dev/null +++ b/src/reopenwebnet/messages.py @@ -0,0 +1,155 @@ +TYPE_OTHER = 'OTHER' +TYPE_ACK = 'ACK' +TYPE_NACK = 'NACK' +TYPE_NORMAL = 'NORMAL' +TYPE_STATUS_REQUEST = 'STATUS_REQUEST' +TYPE_DIMENSION_REQUEST = 'DIMENSION_REQUEST' +TYPE_DIMENSION_READING = 'DIMENSION_READING' +TYPE_DIMENSION_WRITING = 'DIMENSION_WRITING' + + +class FixedMessage: + def __init__(self, value, message_type): + self.value = value + self.type = message_type + + def __str__(self): + return self.value + + def __repr__(self): + return f"{self.type} : {self}" + + +class NormalMessage: + def __init__(self, who, what, where): + self.who = who + self.what = what + self.where = where + self.type = TYPE_NORMAL + + def __str__(self): + return f"*{self.who}*{self.what}*{self.where}##" + + def __repr__(self): + return f"{self.type} : {self}" + + +class StatusRequestMessage: + def __init__(self, who, where): + self.who = who + self.where = where + self.type = TYPE_STATUS_REQUEST + + def __str__(self): + return f"*#{self.who}*{self.where}##" + + def __repr__(self): + return f"{self.type} : {self}" + + +class DimensionRequestMessage: + def __init__(self, who, where, dimension): + self.who = who + self.where = where + self.dimension = dimension + self.type = TYPE_DIMENSION_REQUEST + + def __str__(self): + return f"*#{self.who}*{self.where}*{self.dimension}##" + + def __repr__(self): + return f"{self.type} : {self}" + + +class DimensionReadingMessage: + def __init__(self, who, where, dimension, values): + self.who = who + self.where = where + self.dimension = dimension + self.values = values + self.type = TYPE_DIMENSION_READING + + def __str__(self): + values = "*".join(self.values) + return f"*#{self.who}*{self.where}*{self.dimension}*{values}##" + + def __repr__(self): + return f"{self.type} : {self}" + + +class DimensionWritingMessage: + def __init__(self, who, where, dimension, values): + self.who = who + self.where = where + self.dimension = dimension + self.values = values + self.type = TYPE_DIMENSION_WRITING + + def __str__(self): + values = "*".join(self.values) + return f"*#{self.who}*{self.where}*#{self.dimension}*{values}##" + + def __repr__(self): + return f"{self.type} : {self}" + + +# OpenWeb string for opening a command session +CMD_SESSION = FixedMessage('*99*0##', TYPE_OTHER) +# OpenWeb string for opening an event session +EVENT_SESSION = FixedMessage('*99*1##', TYPE_OTHER) + +ACK = FixedMessage('*#*1##', TYPE_ACK) +NACK = FixedMessage('*#*0##', TYPE_NACK) + + +def bad_message(data): + raise Exception('Improperly formatted message:', data) + + +def parse_message(data): + if data == str(ACK): + return ACK + if data == str(NACK): + return NACK + + if not data.startswith("*"): + raise Exception(f"data does not start with *: {data}") + if not data.endswith("##"): + raise Exception(f"data does not end with ##: {data}") + + parts = data[1:-2].split("*") + if not parts[0].startswith("#"): + if len(parts) != 3: + bad_message(data) + return NormalMessage(parts[0], parts[1], parts[2]) + + if len(parts) < 1: + return bad_message(data) + + if len(parts) == 1: + return FixedMessage(data, TYPE_OTHER) + + if len(parts) == 2: + return StatusRequestMessage(parts[0][1:], parts[1]) + + if len(parts) == 3: + return DimensionRequestMessage(parts[0][1:], parts[1], parts[2]) + + if not parts[2].startswith("#"): + return DimensionReadingMessage(parts[0][1:], parts[1], parts[2], parts[3:]) + + return DimensionWritingMessage(parts[0][1:], parts[1], parts[2][1:], parts[3:]) + + +def parse_messages(data): + if "##" not in data: + return [], data + + parts = data.split("##") + + messages = list(map(lambda part: parse_message(part + '##'), parts[:-1])) + + if len(parts[-1]) == 0: + return messages, None + + return messages, parts[-1] diff --git a/src/reopenwebnet/mqtt/__init__.py b/src/reopenwebnet/mqtt/__init__.py new file mode 100644 index 0000000..9cc7a19 --- /dev/null +++ b/src/reopenwebnet/mqtt/__init__.py @@ -0,0 +1,75 @@ +import asyncio +import logging +import re + +import paho.mqtt.client as mqtt +from reopenwebnet import messages +from reopenwebnet.client import OpenWebNetClient + +MQTT_LIGHT_COMMAND_PATTERN = re.compile('openwebnet/1/(\\d+)/cmd') + +class MqttBridge: + def __init__(self, config): + if config.openwebnet is None: + raise Exception('openwebnet configuration is required') + + if config.mqtt is None: + raise Exception('mqtt configuration required') + + self.config = config + + self.event_client = OpenWebNetClient(config.openwebnet.host, config.openwebnet.port, config.openwebnet.password, messages.EVENT_SESSION, name="eventclient") + self.mqtt = _create_mqtt_client(config.mqtt) + + self.mqtt.on_message = self.send_mqtt_command_to_openwebnet + + async def start(self): + logging.debug('starting mqtt loop') + self.mqtt.loop_start() + + logging.debug('starting event client') + await self.event_client.start(self.send_openwebnet_event_to_mqtt) + + def send_mqtt_command_to_openwebnet(self, client, dummy, message): + logging.debug('received mqtt message: %s / %s', message.topic, message.payload) + match = MQTT_LIGHT_COMMAND_PATTERN.match(message.topic) + if match is not None: + what = message.payload.decode('ASCII') + where = match.group(1) + try: + openwebnet_message = messages.NormalMessage(1, what, where) + asyncio.run(self.send_openwebnet_message(openwebnet_message)) + except Exception as ex: + logging.error("Failed to send message", ex) + + async def send_openwebnet_message(self, message): + command_client = OpenWebNetClient(self.config.openwebnet.host, self.config.openwebnet.port, + self.config.openwebnet.password, messages.CMD_SESSION, + name="commandclient") + await command_client.start(self.send_openwebnet_event_to_mqtt) + command_client.send_message(message) + command_client.transport.close() + + def send_openwebnet_event_to_mqtt(self, msgs): + logging.debug('openwebnet messages received %s', msgs) + for msg in msgs: + # TODO: handle other 'who' types, allow registering transformations (to allow configuring different topic and payload) + if isinstance(msg, messages.NormalMessage): + if msg.who == '1': + topic = f"openwebnet/{msg.who}/{msg.where}/state" + logging.debug('publishing to %s: %s'%(topic, msg)) + self.mqtt.publish(topic, msg.what) + + +def _create_mqtt_client(mqtt_config): + client = mqtt.Client(mqtt_config.client_id) + if mqtt_config.user is not None and mqtt_config.user != '': + client.username_pw_set(mqtt_config.user, mqtt_config.password) + + def on_connect(client, b, c, d): + logging.debug('mqtt connected %s/%s/%s/%s', client, b, c, d) + client.subscribe('openwebnet/1/+/cmd') + + client.on_connect = on_connect + client.connect(mqtt_config.host, port=mqtt_config.port) + return client diff --git a/src/reopenwebnet/mqtt/__main__.py b/src/reopenwebnet/mqtt/__main__.py new file mode 100755 index 0000000..5ceaa45 --- /dev/null +++ b/src/reopenwebnet/mqtt/__main__.py @@ -0,0 +1,14 @@ +import asyncio + +from reopenwebnet.config import read_environment_config +from reopenwebnet.mqtt import MqttBridge + +async def main(): + bridge = MqttBridge(read_environment_config()) + print("starting bridge") + await bridge.start() + while True: + await asyncio.sleep(60) + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/src/reopenwebnet/password.py b/src/reopenwebnet/password.py new file mode 100644 index 0000000..94b9a30 --- /dev/null +++ b/src/reopenwebnet/password.py @@ -0,0 +1,97 @@ +def calculate_password(password, nonce): + m_1 = 0xFFFFFFFF + m_8 = 0xFFFFFFF8 + m_16 = 0xFFFFFFF0 + m_128 = 0xFFFFFF80 + m_16777216 = 0XFF000000 + flag = True + num1 = 0 + num2 = 0 + password = int(password) + + for c in nonce: + num1 = num1 & m_1 + num2 = num2 & m_1 + if c == '1': + length = not flag + if not length: + num2 = password + num1 = num2 & m_128 + num1 = num1 >> 7 + num2 = num2 << 25 + num1 = num1 + num2 + flag = False + elif c == '2': + length = not flag + if not length: + num2 = password + num1 = num2 & m_16 + num1 = num1 >> 4 + num2 = num2 << 28 + num1 = num1 + num2 + flag = False + elif c == '3': + length = not flag + if not length: + num2 = password + num1 = num2 & m_8 + num1 = num1 >> 3 + num2 = num2 << 29 + num1 = num1 + num2 + flag = False + elif c == '4': + length = not flag + + if not length: + num2 = password + num1 = num2 << 1 + num2 = num2 >> 31 + num1 = num1 + num2 + flag = False + elif c == '5': + length = not flag + if not length: + num2 = password + num1 = num2 << 5 + num2 = num2 >> 27 + num1 = num1 + num2 + flag = False + elif c == '6': + length = not flag + if not length: + num2 = password + num1 = num2 << 12 + num2 = num2 >> 20 + num1 = num1 + num2 + flag = False + elif c == '7': + length = not flag + if not length: + num2 = password + num1 = num2 & 0xFF00 + num1 = num1 + ((num2 & 0xFF) << 24) + num1 = num1 + ((num2 & 0xFF0000) >> 16) + num2 = (num2 & m_16777216) >> 8 + num1 = num1 + num2 + flag = False + elif c == '8': + length = not flag + if not length: + num2 = password + num1 = num2 & 0xFFFF + num1 = num1 << 16 + num1 = num1 + (num2 >> 24) + num2 = num2 & 0xFF0000 + num2 = num2 >> 8 + num1 = num1 + num2 + flag = False + elif c == '9': + length = not flag + if not length: + num2 = password + num1 = ~num2 + flag = False + else: + num1 = num2 + num2 = num1 + return num1 & m_1 diff --git a/src/reopenwebnet/protocol.py b/src/reopenwebnet/protocol.py new file mode 100644 index 0000000..a910191 --- /dev/null +++ b/src/reopenwebnet/protocol.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +import asyncio +import time +from logging import getLogger + +from reopenwebnet import messages +from reopenwebnet.password import calculate_password + +_LOGGER = getLogger(__name__) + + +class OpenWebNetProtocol(asyncio.Protocol): + def __init__(self, session_type, password, on_session_start, event_listener, on_connection_lost, + name="opwenwebnet"): + self.session_type = session_type + self.password = password + self.write_delay = 0.1 + self.on_session_start = on_session_start + self.event_listener = event_listener + self.on_connection_lost = on_connection_lost + self.name = name + + self.state = 'NOT_CONNECTED' + self.buffer = "" + self.transport = None + self.next_message = 0 + + def connection_made(self, transport): + self.state = 'CONNECTED' + self.transport = transport + + def data_received(self, data): + data = data.decode('utf-8') + self.buffer += data + + msgs, remainder = messages.parse_messages(self.buffer) + self.buffer = "" if remainder is None else remainder + + if self.state == 'ERROR': + _LOGGER.error("got data in error state:", data) + + elif self.state == 'CONNECTED': + if msgs[0] == messages.ACK: + self._send_message(self.session_type) + self.state = 'SESSION_REQUESTED' + else: + _LOGGER.error('Did not get initial ack on connect') + self.state = 'ERROR' + + elif self.state == 'SESSION_REQUESTED': + if msgs[-1] == messages.NACK: + self.state = 'ERROR' + nonce = msgs[0].value[2:-2] + + password = calculate_password(self.password, nonce) + self._send_message(messages.FixedMessage(f"*#{password}##", messages.TYPE_OTHER)) + self.state = 'PASSWORD_SENT' + + elif self.state == 'PASSWORD_SENT': + if msgs[-1] == messages.ACK: + if self.on_session_start: + self.on_session_start.set_result(True) + self.state = 'EVENT_SESSION_ACTIVE' + else: + _LOGGER.error('Failed to establish event session') + self.state = 'ERROR' + + elif self.state == 'EVENT_SESSION_ACTIVE': + _LOGGER.debug("sending messages to event listener %s", msgs) + if self.event_listener is not None: + self.event_listener(msgs) + + def _send_message(self, message): + now = time.time() + if now < self.next_message: + time.sleep(self.next_message - now) + self.next_message = now + self.write_delay + self.transport.write(str(message).encode('utf-8')) + + def send_message(self, message): + if self.state != 'EVENT_SESSION_ACTIVE': + _LOGGER.error("Not sending message - session not active yet") + # TODO: use an event to indicate when session is active + return + self._send_message(message) + + def connection_lost(self, exc): + _LOGGER.debug("[%s] in protocol.connection_lost: %s", self.name, exc) + self.state = 'NOT_CONNECTED' + self.transport = None + self.on_connection_lost.set_result(False) diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..eab8aa2 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +""" + Dummy conftest.py for reopenwebnet. + + If you don't know what this is for, just leave it empty. + Read more about conftest.py under: + https://pytest.org/latest/plugins.html +""" + +# import pytest diff --git a/tests/test_messages.py b/tests/test_messages.py new file mode 100644 index 0000000..56d2b3f --- /dev/null +++ b/tests/test_messages.py @@ -0,0 +1,72 @@ +# -*- coding: utf-8 -*- + +from nose.tools import assert_equal +from reopenwebnet import messages +from reopenwebnet.messages import parse_message + + +def test_parse_messages_ack(): + assert_equal( parse_message(str(messages.ACK)), messages.ACK) + + +def test_parse_messages_nack(): + assert_equal( parse_message(str(messages.NACK)), messages.NACK) + + +def test_parse_messages_normal(): + actual = parse_message("*1*2*3##") + + assert_equal(actual.type, messages.TYPE_NORMAL) + assert_equal(str(actual), '*1*2*3##') + assert_equal(actual.who, '1') + assert_equal(actual.what, '2') + assert_equal(actual.where, '3') + + +def test_parse_messages_status_request(): + actual = parse_message("*#1*2##") + + assert_equal(actual.type, messages.TYPE_STATUS_REQUEST) + assert_equal(str(actual), '*#1*2##') + assert_equal(actual.who, '1') + assert_equal(actual.where, '2') + + +def test_parse_messages_dimension_request(): + actual = parse_message("*#1*2*3##") + + assert_equal(actual.type, messages.TYPE_DIMENSION_REQUEST) + assert_equal(str(actual), "*#1*2*3##") + assert_equal(actual.who, '1') + assert_equal(actual.where, '2') + assert_equal(actual.dimension, '3') + + +def test_parse_messages_dimension_reading(): + actual = parse_message("*#1*02*4*100*2##") + + assert_equal(actual.type, messages.TYPE_DIMENSION_READING) + assert_equal(str(actual), "*#1*02*4*100*2##") + assert_equal(actual.who, '1') + assert_equal(actual.where, '02') + assert_equal(actual.dimension, '4') + assert_equal(actual.values, ['100', '2']) + + +def test_parse_messages_dimension_writing(): + actual = parse_message("*#1*2*#3*4*5##") + + assert_equal(actual.type, messages.TYPE_DIMENSION_WRITING) + assert_equal(str(actual), "*#1*2*#3*4*5##") + assert_equal(actual.who, '1') + assert_equal(actual.where, '2') + assert_equal(actual.dimension, '3') + assert_equal(actual.values, ['4', '5']) + +def test_parse_nonce_message(): + actual = parse_message("*#123456789##") + + assert_equal(actual.type, messages.TYPE_OTHER) + assert_equal(actual.value, "*#123456789##") + +