-
Notifications
You must be signed in to change notification settings - Fork 1
/
L_Youless2.lua
173 lines (153 loc) · 7.02 KB
/
L_Youless2.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
--[==[
Module L_Youless2.lua
Written by V. Pathuis (Vinx)
Only supports UI7
V1.0 - 28 December 2019
Initial version.
Support for LS110 & LS120
Support for Watt & KWH
Support for S0 Watt & KWH, optional as child device (LS120 only)
Youless: http://youless.nl
Uses YouLess API as described here: http://wiki.td-er.nl/index.php?title=YouLess
--]==]
local HA_SERVICE = "urn:micasaverde-com:serviceId:HaDevice1"
local ENERGY_SERVICE = "urn:micasaverde-com:serviceId:EnergyMetering1"
local YOULESS_SERVICE = "urn:youless-nl:serviceId:YouLess"
local YOULESS_INTERVAL = 60
local YOULESS_MODEL = ""
local YOULESS_S0_ALTID = "YouLess_S0"
local YOULESS_DEVICE = 0
function findChild(parentDevice, label)
for k, v in pairs(luup.devices) do
if (v.device_num_parent == parentDevice and v.id == label) then
return k
end
end
end
local function readYouLess()
status,page = luup.inet.wget(YOULESS_URL_ACTUAL)
luup.log("Youless2: reading started")
if (status == -1) then
luup.log("Youless2: Error connecting to YouLess. Could it be there is a password set?")
luup.variable_set(HA_SERVICE,"CommFailure",1, YOULESS_DEVICE)
else
-- read pwr (watts)
local watts = string.match(page,'"pwr":(.-),')
if (watts ~= nil and watts ~= '') then
watts = tonumber(watts)
luup.variable_set(ENERGY_SERVICE, "Watts", watts, YOULESS_DEVICE)
luup.log("Youless2: pwr (watts)="..watts)
else
luup.log("Youless2: Response from YouLess respons invalid: does not contain pwr")
luup.variable_set(HA_SERVICE,"CommFailure",1, YOULESS_DEVICE)
end
-- read cnt (kwh)
local kwh = string.match(page,'"cnt":"(.-)"')
if (kwh ~= nil and kwh ~= '') then
kwh = string.gsub(kwh,",",".")
kwh = tonumber(kwh)
luup.variable_set(ENERGY_SERVICE,"KWH", kwh, YOULESS_DEVICE)
luup.log("Youless2: kwh (cnt)=".. kwh)
else
luup.log("Youless2: Response from YouLess respons invalid: does not contain cnt")
luup.variable_set(HA_SERVICE,"CommFailure",1, YOULESS_DEVICE)
end
if (YOULESS_MODEL ~= "LS110") then -- following variables are not supported by LS110
childS0id = findChild(YOULESS_DEVICE, YOULESS_S0_ALTID)
if ((childS0id or "") ~= "") then
luup.log("Youless2: Child S0=" .. childS0id)
-- read watts s0 (ps0)
local wattss0 = string.match(page,'"ps0":(.-),')
if ((wattss0 or "") ~= "") then
wattss0 = tonumber(wattss0)
luup.log("Youless2: ps0="..wattss0)
luup.variable_set(ENERGY_SERVICE, "Watts_S0", wattss0, YOULESS_DEVICE)
luup.variable_set(ENERGY_SERVICE, "Watts", wattss0, childS0id)
else
luup.log("Youless2: No ps0 found")
end
-- read kwh s0 (cs0)
local kwhs0 = string.match(page,'"cs0":"(.-)"')
if ((kwhs0 or "") ~= "") then
kwhs0 = string.gsub( kwhs0, "%s+", "")
kwhs0 = string.gsub(kwhs0,",",".")
kwhs0 = tonumber(kwhs0)
luup.variable_set(ENERGY_SERVICE,"KWH_S0", kwhs0, YOULESS_DEVICE)
luup.variable_set(ENERGY_SERVICE, "KWH", kwhs0, childS0id)
luup.log("Youless2: kwhs0=".. kwhs0)
else
luup.log("Youless2: No cs0 found")
end
else
luup.log("Youless2: No child S0 found")
end
end
end
luup.log("Youless2: reading finished")
end
function refreshCache()
readYouLess()
luup.call_timer("refreshCache", 1, YOULESS_INTERVAL, "")
end
function Youless_Init(youless_device)
luup.log("Youless2: Starting")
YOULESS_DEVICE = youless_device
YOULESS_IP = luup.attr_get('ip',YOULESS_DEVICE)
if (YOULESS_IP == nil or YOULESS_IP == "") then
return false, "No IP specified. Visit the Advanced tab to specify the Youless ip-address and reload luup.", string.format("%s[%d]", luup.devices[YOULESS_DEVICE].description, youless_device)
end
YOULESS_URL_ACTUAL = "http://" .. YOULESS_IP .. "/a?f=j"
YOULESS_URL_DEVICE = "http://" .. YOULESS_IP .. "/d"
-- check the connection
local status, page = luup.inet.wget(YOULESS_URL_ACTUAL)
if ((status or 0) ~= 0) then
return false, "Connection to YouLess failed. Verify the Youless ip-address and that no password is set on the Youless", string.format("%s[%d]", luup.devices[YOULESS_DEVICE].description, YOULESS_DEVICE)
end
status,page = luup.inet.wget(YOULESS_URL_DEVICE)
if ((status or 0) ~= 0) then
YOULESS_MODEL = "LS110" -- device page not available, therefore assuming LS110
luup.variable_set(ENERGY_SERVICE, "Model", YOULESS_MODEL, YOULESS_DEVICE)
luup.log("Youless2: device page not found, therefore model="..YOULESS_MODEL)
else
local model = string.match(page,'"model":"(.-)",')
if (model ~= nil and model ~= '') then
YOULESS_MODEL = string.gsub( model, "%s+", "")
luup.variable_set(ENERGY_SERVICE, "Model", YOULESS_MODEL, YOULESS_DEVICE)
luup.log("Youless2: model="..YOULESS_MODEL)
else
luup.log("Youless2: Found device page, but failed to get model name")
end
end
luup.log("Youless2: YouLess found at " .. YOULESS_IP)
if ((YOULESS_MODEL or "") ~= "LS110") then
local ChildDeviceS0 = luup.variable_get(YOULESS_SERVICE, "ChildDeviceS0", YOULESS_DEVICE)
-- create variables if they don't exist
if ((ChildDeviceS0 or "") == "") then
luup.variable_set(YOULESS_SERVICE, "ChildDeviceS0", 0, YOULESS_DEVICE)
end
if (ChildDeviceS0 ~= nil) then luup.log("Youless2: ChildDeviceS0=" .. ChildDeviceS0) end
-- create child devices if needed
local child_devices = luup.chdev.start(YOULESS_DEVICE)
if (ChildDeviceS0 == "1") then
luup.log("Youless2: Adding child device for YouLess S0")
local init=ENERGY_SERVICE .. ",ActualUsage=1\n" ..
ENERGY_SERVICE .. ",Watts=0\n" ..
ENERGY_SERVICE .. ",KWH=0\n"
luup.chdev.append(YOULESS_DEVICE,
child_devices, -- handle
YOULESS_S0_ALTID, -- altid
"YouLess S0", -- device name
"", -- device_type, derived from the device file
"D_PowerMeter1.xml", -- device file for given device
"", -- Implementation file
init, -- initiating variables
true) -- embedded
end
luup.chdev.sync(YOULESS_DEVICE, child_devices)
end
readYouLess()
luup.log("Youless2: init succesful")
-- This doesn't work in UI5: luup.set_failure(0, YOULESS_DEVICE)
luup.variable_set(HA_SERVICE,"CommFailure",0, YOULESS_DEVICE)
luup.call_timer("refreshCache", 1, YOULESS_INTERVAL, "")
end