虽然domoticz官网提供了一个插件开发教程,但是在实际的开发过程中就会发现有很多内容写的不够详细,无法参考。 官网教程链接: https://en.domoticz.cn/wiki/Developing_a_Python_plugin 国内的中文wiki页面比较陈旧,文档也不是最新的,不建议参考。
其实真个插件主要分为两个部分:
-
插件模板
-
插件代码
插件模板主要就是插件的定义,以及相关的参数配置选项,格式为xml:
<plugin key="BabyWeatherPlugin" name="Baby Weather Plugin" author="obaby" version="1.0.0" wikilink="http://www.h4ck.org.cn" externallink="https://www.h4ck.org.cn/"> <description> <h2>Baby Weather Plugin</h2><br /> 支持从国内的天气服务器获取天气信息 <h3>Features</h3> <ul style="list-style-type:square"> <li>支持和风天气</li> <li>支持彩云天气</li> <li>支持今天明天的天气预报信息</li> </ul> <h3>Devices</h3> <ul style="list-style-type:square"> <li>Temperature - 当前温度</li> <li>Feeling Temperature - 当前体感温度</li> <li>Humidity - 湿度</li> <li>Pressure - 气压</li> </ul> <h3>Configuration</h3> API KEY请自行注册相关的开发者账号,然后获取key。 技术支持:http://www.h4ck.org.cn 彩云天气:https://open.caiyunapp.com/ 和风天气:https://dev.heweather.com/ </description> <params> <param field="Mode1" label="服务器" width="100px"/> <options> <option label="彩云天气" value="Caiyun"></option> <option label="和风天气" value="heweather"></option> </options> <param field="Mode2" label="API KEY" width="600px" required="true" default="**********************"/> <param field="Mode3" label="经度" width="600px" required="true" default="116.40"/> <param field="Mode4" label="纬度" width="600px" required="true" default="39.915156"/> <param field="Mode5" label="更新频率(分钟)" width="60px" required="true" default="60"/> <param field="Mode6" label="Debug" width="75px"/> <options> <option label="True" value="Debug"></option> <option label="False" value="Normal" default="true"></option> </options> </params> </plugin> |
这里面的定义需要注意的是参数的名称并不能自己随意定义, 仅能使用如下的参数名称:
- SerialPort – used by ‘serial’ transports
- Address – used by non-serial transports
- Port – used by non-serial transports
- Mode1 – General purpose
- Mode2 – General purpose
- Mode3 – General purpose
- Mode4 – General purpose
- Mode5 – General purpose
- Mode6 – General purpose
- Username – Username for authentication
- Password – Password for authentication
例如 <param field=”Mode3″ label=”经度” width=”600px” required=”true” default=”116.40″/>修改为 <param field=”GpsLong” label=”经度” width=”600px” required=”true” default=”116.40″/> 然后就挂了,提示找不到属性。
除此之外,定义部分其他的内容文档里写的都比较清楚了。在代码中取参数的数值通过Mode3获取。
在代码部分,需要注意的是Update函数:
Devices[Unit].Update(nValue=nValue, sValue=str(sValue), SignalLevel=5, Image=8)
对于部分Device的sValue参数文档中给出了说明,要传递的数值: Wind sensor (sValue: “<WindDirDegrees>;<WindDirText>;<WindAveMeterPerSecond10>;<WindGustMeterPerSecond10>;<Temp_c>;<WindChill_c>”) 但是这个文档的数值并不是全部的内容:
Filling is in progress, table doesn’t contain full available list yet
https://github.com/Xorfor/Domoticz-API/wiki/Device 这个页面提供了更详细的说明,在更新sValue的时候需要注意要传的并不是一个字符串,而是多个;分隔的字符串。
Device/Sensor | nvalue | svalue | Example |
---|---|---|---|
Alert sensor | LEVEL | TEXT | |
Barometer | BAR | BAR_FOR | dev.update(1020, “3”) |
Counter | COUNTER | ||
Custom sensor | 0 | VALUE | |
Distance | 0 | DISTANCE | |
Electricity (instant and counter) | 0 | POWER;ENERGY | |
Electricity Current/Ampere 3 Phase | 0 | AMPERE1;AMPERE2;AMPERE3 | |
Electricity P1 smart meter | 0 | USAGE1;USAGE2;RETURN1;RETURN2;CONS;PROD | |
Gas | 0 | USAGE | |
Humidity | HUM | HUM_STAT | dev.update(45, “2”) |
Lux | LUX | ||
Moisture (Soil) | MOISTURE | ||
Percentage | 0 | PERCENTAGE | |
Pressure | 0 | BAR | |
Rain | 0 | RAINRATE;RAINCOUNTER | |
Temperature | 0 | TEMP | dev.update(0, “20.3″) |
Temperature/humidity | 0 | TEMP;HUM;HUM_STAT | |
Temperature/humidity/barometer | 0 | TEMP;HUM;HUM_STAT;BAR;BAR_FOR | |
Temperature/barometer | 0 | TEMP;BAR;BAR_FOR;ALTITUDE | |
Text | 0 | TEXT | |
UV | 0 | UV;TEMP | |
Visibility | 0 | VISIBILITY | |
Voltage | 0 | VOLTAGE | |
Wind | 0 | BEARING;DIRECTION;WS;WG;TEMP;CHILL |
在线xls 转markdown :https://tableconvert.com
例如更新wind Device需要更新如下参数:
if wind_speed and wind_direction: self.update_device_value(8, 0, str(wind_direction) + ";" + self.getWindDirection(wind_direction) + ";" + str(round(float(wind_speed) * 10)) + ";" + str(round(float(wind_speed) * 10)) + ";0;0") |
<
p class=”md-end-block md-p md-focus”>参考链接: https://github.com/Xorfor/Domoticz-API/wiki/Device#examples https://en.domoticz.cn/wiki/Developing_a_Python_plugin
原创文章,转载请注明: 转载自 obaby@mars
本文标题: 《Domoticz 插件开发教程》
本文链接地址: http://h4ck.org.cn/2020/06/domoticz-%e6%8f%92%e4%bb%b6%e5%bc%80%e5%8f%91%e6%95%99%e7%a8%8b/