### Python Plugin Framework

The Python Plugin Framework allows to create an interface between Hardware (or Virtual Hardware) and Domoticz.
Read the manual first: https://www.domoticz.com/wiki/Developing_a_Python_plugin
These are my first experineces in writing a Domoticz Plugin. It makes real fun must say.
Goal
To learn how to develop a Domoticz Plugin, build an Homepage Counter.
The Homepage Counter reads the content of a textfile located at an Internetserver.
The textfile contains one line holding the pagecounts (or hits).
The hits are to be displayed at a Domoticz Device.
The environment runs on a Raspberry Pi 3 (Raspian Jessie) with Domoticz latest Beta version.
Status 20170809 = 3.8263
Setup
Created folder /home/pi/domoticz/plugins/HomepageCounter.
The plugin is defined in the scripts plugin.py - code example, see "Plugin.py Full Code" below.
XML Plugin.py
The hardware device is defined in the XMl section. It has 3 fields:
Address for the URL to the pagecounter file.
Check interval in seconds for how often the pagecounter file should be checked.
Debug flag.
Python Plugin.py
When starting, the device is checked. If not found, then a new device from type "Counter Incremental" is created.
If the device is created, make changes accordingly as outlined under Testing.
On every Check Interval, the new homepage counter is ready by retrieving from the server (HTTP GET request).
The current homepage counter is read from the Device.
The delta between current and new counter is calculated and if > 0 the Homepage Counter is updated.
Testing
For testing, used these steps - there might be better ways of doing so.
• Create or update plugin.py - used Raspberry Pi program Thonny
• In Domoticz deleted the Hardware Device "Homepage Counter"
• Restarted Domoticz from the shell: sudo service domoticz.sh restart
• In Domoticz, added new Hardware Device "Homepage Counter"
• In Domoticz, opened the Log and checked for entries related to "plugin system" and "Homepage Counter".
• The log shows, if new hardware is created - if all goes well, then
• In Domoticz, goto Devices and add the "Homepage Counter" device (change the name if required)
• In Domoticz, select the "Homepage Counter" device in Utility and edit: Type=Counter, Offset=0, Quantity=Hits, Units leave empty.
• Refresh the homepage and check the Domoticz log and the Device in Utility
Plugin.py Full Code
Domoticz Home Automation - HomepageCounter
Read the content of a homepagecounter text file and display as device
@author Robert W.B. Linn
@version 20170809
..
"""
<plugin key="HomepageCounter" name="Homepage Counter" author="rwbl" version="1.0.0" externallink="https://mysite">
<params>
<param field="Address" label="URL Homepage Counter" width="300px" required="true" default="http://address/pagecounterfile.txt"/>
<param field="Mode1" label="Check Interval (seconds)" width="75px" required="true" default="60"/>
<param field="Mode6" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import urllib
import urllib.request
class BasePlugin:
enabled = False
pluginState = "Not Ready"
def __init__(self):
return
def onStart(self):
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
if (len(Devices) == 0):
Domoticz.Device(Name="Homepage Counter", Unit=1, TypeName="Counter Incremental", Options={"Counter Incremental": "1;Hits"}).Create()
Domoticz.Log("Device created.")
DumpConfigToLog()
#url = "http..."
url = Parameters["Address"]
response = urllib.request.urlopen(url).read()
Domoticz.Debug("Previous Counter:" + str(response,'utf-8'))
Domoticz.Heartbeat(int(Parameters["Mode1"]))
def onStop(self):
Domoticz.Log("Plugin is stopping.")
def onHeartbeat(self):
Domoticz.Debug("onHeartbeat called")
# Get the url with the text file containing the homepage counter value
url = Parameters["Address"]
# Open the url and read the content
response = urllib.request.urlopen(url).read()
# Convert the response from byte to string and strip special characters
hpc = str(response,'utf-8')
hpc = hpc.strip(' \t\n\r')
# Set the new counter as int
NewCounter = int(hpc)
# Capture the current sensor value for device with unit = 1
CurCounter = Devices[1].nValue
# If first time the CurCounter is 0
if CurCounter == 0:
CurCounter = NewCounter
Devices[1].Update( nValue=NewCounter, sValue=str(NewCounter) )
Domoticz.Log("HomepageCounter initialized:" + str(NewCounter) )
# Calculate the delta counter
DeltaCounter = NewCounter - CurCounter
# Update the counter if the delta counter > 0
if DeltaCounter > 0:
Devices[1].Update( nValue=DeltaCounter, sValue=str(DeltaCounter) )
CurCounter = Devices[1].nValue
Domoticz.Log("HomepageCounter updated by " + str(DeltaCounter) + " to " + str(CurCounter) )
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
return
Log Example with Debug True
2017-08-09 15:37:58.586 (Homepage Counter) Stop directive received.
2017-08-09 15:37:58.626 (Homepage Counter) Plugin is stopping.
2017-08-09 15:37:58.752 (Homepage Counter) Exiting work loop.
2017-08-09 15:37:58.786 (Homepage Counter) Stopping threads.
2017-08-09 15:37:58.786 (Homepage Counter) Stopped.
2017-08-09 15:37:58.786 (Homepage Counter) Started.
2017-08-09 15:37:59.149 (Homepage Counter) Entering work loop.
2017-08-09 15:37:59.149 (Homepage Counter) Initialized version 1.0.0, author 'rwbl'
2017-08-09 15:37:59.202 (Homepage Counter) Debug log level set to: 'true'.
2017-08-09 15:37:59.568 (Homepage Counter) Previous Counter:1752711
2017-08-09 15:37:59.569 (Homepage Counter) Heartbeat interval set to: 60.
2017-08-09 15:38:08.683 (Homepage Counter) Calling message handler 'onHeartbeat'.
2017-08-09 15:38:08.683 (Homepage Counter) onHeartbeat called
2017-08-09 15:39:08.661 (Homepage Counter) Calling message handler 'onHeartbeat'.
2017-08-09 15:39:08.662 (Homepage Counter) onHeartbeat called
2017-08-09 15:39:08.787 (Homepage Counter) Updating device from 1752711:'1752711' to have values 2:'2'.
2017-08-09 15:39:08.794 (Homepage Counter) HomepageCounter updated by 2 to 1752713

Changed: 2018-06-07 09:11, Category: Domoticz Home Automation, File: T6C186M1352.htm, ID: 1352