@@ -11,6 +11,7 @@ import (
1111 "io"
1212 "log"
1313 "net/http"
14+ "strings"
1415 "time"
1516
1617 "github.com/gorilla/websocket"
@@ -116,14 +117,14 @@ func initTemplate() components.UnitAsset {
116117
117118 // var uat components.UnitAsset // this is an interface, which we then initialize
118119 uat := & UnitAsset {
119- Name : "SmartPlugExample " ,
120+ Name : "SmartSwitch1 " ,
120121 Details : map [string ][]string {"Location" : {"Kitchen" }},
121- Model : "Smart plug " ,
122+ Model : "ZHASwitch " ,
122123 Uniqueid : "14:ef:14:10:00:6f:d0:d7-11-1201" ,
123124 Period : 10 ,
124125 Setpt : 20 ,
125- // Switches adds power plug and light uniqueids, power plugs adds Consumption & Power sensors to this map
126- Slaves : map [string ]string {"ConsumptionExample " : "14:ef:14:10:00:6f:d0:d7-1f-000c " , "PowerExample " : "14 :ef:14:10 :00:6f:d0:d7-15-000c " },
126+ // Only switches needs to manually add power plug and light uniqueids, power plugs get their sensors added automatically
127+ Slaves : map [string ]string {"Plug1 " : "14:ef:14:10:00:6f:d0:d7-XX-XXXX " , "Plug2 " : "24 :ef:24:20 :00:6f:d0:d2-XX-XXXX " },
127128 Apikey : "1234" ,
128129 ServicesMap : components.Services {
129130 setPointService .SubPath : & setPointService ,
@@ -182,6 +183,7 @@ func newResource(uac UnitAsset, sys *components.System, servs []components.Servi
182183 log .Println ("Error occured during startup, while calling getWebsocketPort():" , err )
183184 // TODO: Check if we need to kill program if this doesn't pass?
184185 }
186+
185187 }
186188 switch ua .Model {
187189 case "ZHAThermostat" :
@@ -191,6 +193,7 @@ func newResource(uac UnitAsset, sys *components.System, servs []components.Servi
191193 }
192194 case "Smart plug" :
193195 // Not all smart plugs should be handled by the feedbackloop, some should be handled by a switch
196+ ua .getSensors ()
194197 if ua .Period > 0 {
195198 // start the unit assets feedbackloop, this fetches the temperature from ds18b20 and and toggles
196199 // between on/off depending on temperature in the room and a set temperature in the unitasset
@@ -289,6 +292,43 @@ func findGateway() (err error) {
289292 return
290293}
291294
295+ // Function to get sensors connected to a smart plug and place them in the "slaves" array
296+ type sensorJSON struct {
297+ UniqueID string `json:"uniqueid"`
298+ Type string `json:"type"`
299+ }
300+
301+ func (ua * UnitAsset ) getSensors () (err error ) {
302+ // Create and send a get request to get all sensors connected to deConz gateway
303+ apiURL := "http://" + gateway + "/api/" + ua .Apikey + "/sensors"
304+ req , err := createGetRequest (apiURL )
305+ if err != nil {
306+ return err
307+ }
308+ data , err := sendGetRequest (req )
309+ if err != nil {
310+ return err
311+ }
312+ // Unmarshal data from get request into an easy to use JSON format
313+ var sensors map [string ]sensorJSON
314+ json .Unmarshal (data , & sensors )
315+ // Take only the mac address of the unitasset to check against mac address of each sensor
316+ macAddr := ua .Uniqueid [0 :23 ]
317+ for _ , sensor := range sensors {
318+ uniqueid := sensor .UniqueID
319+ check := strings .Contains (uniqueid , macAddr )
320+ if check == true {
321+ if sensor .Type == "ZHAConsumption" {
322+ ua .Slaves ["ZHAConsumption" ] = sensor .UniqueID
323+ }
324+ if sensor .Type == "ZHAPower" {
325+ ua .Slaves ["ZHAPower" ] = sensor .UniqueID
326+ }
327+ }
328+ }
329+ return
330+ }
331+
292332//-------------------------------------Thing's resource methods
293333
294334// getSetPoint fills out a signal form with the current thermal setpoint
@@ -411,7 +451,7 @@ type consumptionJSON struct {
411451}
412452
413453func (ua * UnitAsset ) getConsumption () (f forms.SignalA_v1a , err error ) {
414- apiURL := "http://" + gateway + "/api/" + ua .Apikey + "/sensors/" + ua .Slaves ["Consumption " ]
454+ apiURL := "http://" + gateway + "/api/" + ua .Apikey + "/sensors/" + ua .Slaves ["ZHAConsumption " ]
415455 // Create a get request
416456 req , err := createGetRequest (apiURL )
417457 if err != nil {
@@ -450,7 +490,7 @@ type powerJSON struct {
450490}
451491
452492func (ua * UnitAsset ) getPower () (f forms.SignalA_v1a , err error ) {
453- apiURL := "http://" + gateway + "/api/" + ua .Apikey + "/sensors/" + ua .Slaves ["Power " ]
493+ apiURL := "http://" + gateway + "/api/" + ua .Apikey + "/sensors/" + ua .Slaves ["ZHAPower " ]
454494 // Create a get request
455495 req , err := createGetRequest (apiURL )
456496 if err != nil {
@@ -489,7 +529,7 @@ type currentJSON struct {
489529}
490530
491531func (ua * UnitAsset ) getCurrent () (f forms.SignalA_v1a , err error ) {
492- apiURL := "http://" + gateway + "/api/" + ua .Apikey + "/sensors/" + ua .Slaves ["Power " ]
532+ apiURL := "http://" + gateway + "/api/" + ua .Apikey + "/sensors/" + ua .Slaves ["ZHAPower " ]
493533 // Create a get request
494534 req , err := createGetRequest (apiURL )
495535 if err != nil {
@@ -528,7 +568,7 @@ type voltageJSON struct {
528568}
529569
530570func (ua * UnitAsset ) getVoltage () (f forms.SignalA_v1a , err error ) {
531- apiURL := "http://" + gateway + "/api/" + ua .Apikey + "/sensors/" + ua .Slaves ["Power " ]
571+ apiURL := "http://" + gateway + "/api/" + ua .Apikey + "/sensors/" + ua .Slaves ["ZHAPower " ]
532572 // Create a get request
533573 req , err := createGetRequest (apiURL )
534574 if err != nil {
0 commit comments