-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshelly.go
1152 lines (963 loc) · 26.7 KB
/
shelly.go
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package shelly
import (
"bufio"
"context"
"crypto/sha256"
"encoding/json"
"fmt"
"io"
"github.com/mongoose-os/mos/common/mgrpc"
"github.com/mongoose-os/mos/common/mgrpc/frame"
)
const (
// DefaultAuthenticationUsername is the only username allowed for auth.
DefaultAuthenticationUsername = "admin"
)
type ShellyGetStatusRequest struct{}
func (r *ShellyGetStatusRequest) Method() string {
return "Shelly.GetStatus"
}
func (r *ShellyGetStatusRequest) NewTypedResponse() *ShellyGetStatusResponse {
return &ShellyGetStatusResponse{}
}
func (r *ShellyGetStatusRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyGetStatusRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ShellyGetStatusResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type ShellyGetStatusResponse struct {
System *SysStatus `json:"sys,omitempty"`
Wifi *WifiStatus `json:"wifi,omitempty"`
Ethernet *EthStatus `json:"eth,omitempty"`
BLE *BLEStatus `json:"ble,omitempty"`
Cloud *CloudStatus `json:"cloud,omitempty"`
MQTT *MQTTStatus `json:"mqtt,omitempty"`
// WebSocket *WsStatus `json:"ws,omitempty"`
// Scripts []*ScriptStatus
Inputs []*InputStatus `json:"inputs,omitempty"`
// ModBus *ModBusStatus
// Voltmeters []*VoltmeterStatus
Covers []*CoverStatus `json:"covers,omitempty"`
Switches []*SwitchStatus `json:"switches,omitempty"`
Lights []*LightStatus `json:"lights,omitempty"`
DevicePowers []*DevicePowerStatus `json:"device_powers,omitempty"`
Humidities []*HumidityStatus `json:"humidities,omitempty"`
Temperatures []*TemperatureStatus `json:"temperatures,omitempty"`
// EMs []*EMStatus
// EM1s []*EM1Status
// PM1s []*PM1Status
// EMDatas []*EMDataStatus
// EM1Datas []EM1DataStatus
// Smokes []*SmokeStatus
}
func (r *ShellyGetStatusResponse) UnmarshalJSON(b []byte) error {
theRest := make(map[string]json.RawMessage)
if err := json.Unmarshal(b, &theRest); err != nil {
return err
}
if v, ok := theRest["ble"]; ok {
var s BLEStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.BLE = &s
}
if v, ok := theRest["sys"]; ok {
var s SysStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.System = &s
}
if v, ok := theRest["cloud"]; ok {
var s CloudStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Cloud = &s
}
if v, ok := theRest["mqtt"]; ok {
var s MQTTStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.MQTT = &s
}
if v, ok := theRest["wifi"]; ok {
var s WifiStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Wifi = &s
}
if v, ok := theRest["eth"]; ok {
var s EthStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Ethernet = &s
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("switch:%d", i)]
if !ok {
continue
}
var s SwitchStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Switches = append(r.Switches, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("cover:%d", i)]
if !ok {
continue
}
var s CoverStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Covers = append(r.Covers, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("input:%d", i)]
if !ok {
continue
}
var s InputStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Inputs = append(r.Inputs, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("light:%d", i)]
if !ok {
continue
}
var s LightStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Lights = append(r.Lights, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("devicepower:%d", i)]
if !ok {
continue
}
var s DevicePowerStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.DevicePowers = append(r.DevicePowers, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("humidity:%d", i)]
if !ok {
continue
}
var s HumidityStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Humidities = append(r.Humidities, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("temperature:%d", i)]
if !ok {
continue
}
var s TemperatureStatus
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Temperatures = append(r.Temperatures, &s)
}
return nil
}
type ShellyGetDeviceInfoRequest struct {
// Ident is a flag specifying if extra identifying information should be displayed.
Ident bool
}
func (r *ShellyGetDeviceInfoRequest) Method() string {
return "Shelly.GetDeviceInfo"
}
func (r *ShellyGetDeviceInfoRequest) NewTypedResponse() *ShellyGetDeviceInfoResponse {
return &ShellyGetDeviceInfoResponse{}
}
func (r *ShellyGetDeviceInfoRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyGetDeviceInfoRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ShellyGetDeviceInfoResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type ShellyGetDeviceInfoResponse struct {
// ID of the device.
ID string `json:"id"`
// MAC of the device.
MAC string `json:"mac"`
// Model of the device
Model string `json:"model"`
// Gen is the generation of the device
Gen json.Number `json:"gen"`
// FW_ID is the firmware id of the device.
FW_ID string `json:"fw_id"`
// Ver is the version of the device firmware.
Ver string `json:"ver"`
// App is the application name.
App string `json:"app"`
// Profile is the name of the device profile (only applicable for multi-profile devices)
Profile string `json:"profile"`
// AuthEn is true if authentication is enabled.
AuthEn bool `json:"auth_en"`
// Name of the domain (null if authentication is not enabled)
AuthDomain *string `json:"auth_domain"`
// Present only when false. If true, device is shown in 'Discovered devices'. If false, the device is hidden.
Discoverable *bool `json:"discoverable"`
// Key is cloud key of the device (see note below), present only when the ident parameter is set to true.
Key string `json:"key"`
// Batch used to provision the device, present only when the ident parameter is set to true.
Batch string `json:"batch"`
// FW_SBits are shelly internal flags, present only when the ident parameter is set to true.
FW_SBits string
}
// ShellyCheckForUpdateRequest
type ShellyCheckForUpdateRequest struct{}
func (r *ShellyCheckForUpdateRequest) Method() string {
return "Shelly.CheckForUpdate"
}
func (r *ShellyCheckForUpdateRequest) NewTypedResponse() *ShellyCheckForUpdateResponse {
return &ShellyCheckForUpdateResponse{}
}
func (r *ShellyCheckForUpdateRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyCheckForUpdateRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ShellyCheckForUpdateResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type ShellyCheckForUpdateResponse struct {
// Stable indicates the new stable version of the firmware.
Stable *FirmwareUpdateVersion `json:"stable,omitempty"`
// Beta indicates the new beta version of the firmware.
Beta *FirmwareUpdateVersion `json:"beta,omitempty"`
}
type FirmwareUpdateVersion struct {
// Version is the new version available.
Version string `json:"version,omitempty"`
// BuildID is the build ID of the update.
BuildID string `json:"build_id,omitempty"`
}
type ShellyUpdateRequest struct {
// Stage is the the type of the new version - either stable or beta. By default updates to
// stable version. (Optional)
Stage string `json:"stage,omitempty"`
// URL address of the update. (Optional). If set Stage must be "".
URL string `json:"url,omitempty"`
}
func (r *ShellyUpdateRequest) Method() string {
return "Shelly.Update"
}
func (r *ShellyUpdateRequest) NewTypedResponse() *RPCEmptyResponse {
return &RPCEmptyResponse{}
}
func (r *ShellyUpdateRequest) NewResponse() any {
return r.NewTypedResponse()
}
type ShellyFactoryResetRequest struct{}
func (r *ShellyFactoryResetRequest) Method() string {
return "Shelly.FactoryReset"
}
func (r *ShellyFactoryResetRequest) NewTypedResponse() *RPCEmptyResponse {
return &RPCEmptyResponse{}
}
func (r *ShellyFactoryResetRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyFactoryResetRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*RPCEmptyResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type ShellyResetWiFiConfigRequest struct{}
func (r *ShellyResetWiFiConfigRequest) Method() string {
return "Shelly.ResetWiFiConfig"
}
func (r *ShellyResetWiFiConfigRequest) NewTypedResponse() *RPCEmptyResponse {
return &RPCEmptyResponse{}
}
func (r *ShellyResetWiFiConfigRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyResetWiFiConfigRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*RPCEmptyResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type ShellyRebootRequest struct {
// DelayMS sets the delay until reboot in milliseconds. Any values are valid but the minimum
// is capped at 500 ms. Default value: 1000 ms. (Optional)
DelayMS int `json:"delay_ms,omitempty"`
}
func (r *ShellyRebootRequest) Method() string {
return "Shelly.Reboot"
}
func (r *ShellyRebootRequest) NewTypedResponse() *RPCEmptyResponse {
return &RPCEmptyResponse{}
}
func (r *ShellyRebootRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyRebootRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*RPCEmptyResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type ShellySetAuthRequest struct {
// User must be set to admin. Only one user is supported. (Required)
User string `json:"user,omitempty"`
// Realm must be the id of the device. Only one realm is supported. (Required)
Realm string `json:"realm,omitempty"`
// HA1 "user:realm:password" encoded in SHA256 (null to disable authentication).
HA1 *string `json:"ha1"`
}
func (r *ShellySetAuthRequest) Method() string {
return "Shelly.SetAuth"
}
func (r *ShellySetAuthRequest) NewTypedResponse() *RPCEmptyResponse {
return &RPCEmptyResponse{}
}
func (r *ShellySetAuthRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellySetAuthRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*RPCEmptyResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
func NewShellySetAuthRequest(deviceID, password string) *ShellySetAuthRequest {
out := &ShellySetAuthRequest{
User: DefaultAuthenticationUsername,
Realm: deviceID,
}
if password == "" {
return out
}
out.HA1 = StrPtr(
fmt.Sprintf("%x",
sha256.Sum256([]byte(out.User+":"+out.Realm+":"+password))))
return out
}
// BuildShellyAuthRequest builds the request, fetching the deviceID for realm.
func BuildShellyAuthRequest(
ctx context.Context,
c mgrpc.MgRPC,
password string,
) (*ShellySetAuthRequest, error) {
resp, _, err := (&ShellyGetDeviceInfoRequest{}).Do(ctx, c, nil)
if err != nil {
return nil, err
}
deviceID := resp.ID
return NewShellySetAuthRequest(deviceID, password), nil
}
type ShellyPutUserCARequest struct {
// Contents of the PEM file (null if you want to delete the existing data). (Required)
Data *string `json:"data"`
// Append is true if more data will be appended afterwards, default false.
Append bool `json:"append,omitempty"`
}
func (r *ShellyPutUserCARequest) Method() string {
return "Shelly.PutUserCA"
}
func (r *ShellyPutUserCARequest) NewTypedResponse() *RPCEmptyResponse {
return &RPCEmptyResponse{}
}
func (r *ShellyPutUserCARequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyPutUserCARequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*RPCEmptyResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
// ShellyPutUserCA is a helper method which uploads the provided data to the Shelly.PutUserCA method,
// line-by-line to accomodate limits on payload size.
func ShellyPutUserCA(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
data io.Reader,
) error {
s := bufio.NewScanner(data)
req := &ShellyPutUserCARequest{}
for s.Scan() {
req.Data = StrPtr(s.Text() + "\n")
if _, _, err := req.Do(ctx, c, credsCallback); err != nil {
return err
}
req.Append = true
}
if err := s.Err(); err != nil {
return fmt.Errorf("reading input data for Shelly.PutUserCA: %w", err)
}
return nil
}
type ShellyPutTLSClientCertRequest struct {
// Data contents of the PEM file (null if you want to delete the existing data). (Required)
Data *string `json:"data"`
// Append is true if more data will be appended afterwards, default false.
Append bool `json:"append,omitempty"`
}
func (r *ShellyPutTLSClientCertRequest) Method() string {
return "Shelly.PutTLSClientCert"
}
func (r *ShellyPutTLSClientCertRequest) NewTypedResponse() *RPCEmptyResponse {
return &RPCEmptyResponse{}
}
func (r *ShellyPutTLSClientCertRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyPutTLSClientCertRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*RPCEmptyResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
// ShellyPutTLSClientCert is a helper method which uploads the provided data to the
// Shelly.PutTLSClientCert method, line-by-line to accomodate limits on payload size.
func ShellyPutTLSClientCert(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
data io.Reader,
) error {
s := bufio.NewScanner(data)
req := &ShellyPutTLSClientCertRequest{}
for s.Scan() {
req.Data = StrPtr(s.Text() + "\n")
if _, _, err := req.Do(ctx, c, credsCallback); err != nil {
return err
}
req.Append = true
}
if err := s.Err(); err != nil {
return fmt.Errorf("reading input data for Shelly.TLSClientCert: %w", err)
}
return nil
}
type ShellyPutTLSClientKeyRequest struct {
// Contents of the PEM file (null if you want to delete the existing data). (Required)
Data *string `json:"data"`
// Append is true if more data will be appended afterwards, default false.
Append bool `json:"append,omitempty"`
}
func (r *ShellyPutTLSClientKeyRequest) Method() string {
return "Shelly.PutTLSClientKey"
}
func (r *ShellyPutTLSClientKeyRequest) NewTypedResponse() *RPCEmptyResponse {
return &RPCEmptyResponse{}
}
func (r *ShellyPutTLSClientKeyRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyPutTLSClientKeyRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*RPCEmptyResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
// ShellyPutTLSClientKey is a helper method which uploads the provided data to the
// Shelly.PutTLSClientKey method, line-by-line to accomodate limits on payload size.
func ShellyPutTLSClientKey(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
data io.Reader,
) error {
s := bufio.NewScanner(data)
req := &ShellyPutTLSClientKeyRequest{}
for s.Scan() {
req.Data = StrPtr(s.Text() + "\n")
if _, _, err := req.Do(ctx, c, credsCallback); err != nil {
return err
}
req.Append = true
}
if err := s.Err(); err != nil {
return fmt.Errorf("reading input data for Shelly.PutTLSClientKey: %w", err)
}
return nil
}
type ShellyGetConfigResponse struct {
System *SysConfig `json:"sys,omitempty"`
Wifi *WifiConfig `json:"wifi,omitempty"`
Ethernet *EthConfig `json:"eth,omitempty"`
BLE *BLEConfig `json:"ble,omitempty"`
Cloud *CloudConfig `json:"cloud,omitempty"`
MQTT *MQTTConfig `json:"mqtt,omitempty"`
// WebSocket *WsConfig `json:"ws,omitempty"`
// Scripts []*ScriptConfig
Inputs []*InputConfig `json:"inputs,omitempty"`
// ModBus *ModBusConfig
// Voltmeters []*VoltmeterConfig
Covers []*CoverConfig `json:"covers,omitempty"`
Switches []*SwitchConfig `json:"switches,omitempty"`
Lights []*LightConfig `json:"lights,omitempty"`
// DevicePowers []*DevicePowerConfig
Humidities []*HumidityConfig `json:"humidities,omitempty"`
Temperatures []*TemperatureConfig `json:"temperatures,omitempty"`
// EMs []*EMConfig
// EM1s []*EM1Config
// PM1s []*PM1Config
// EMDatas []*EMDataConfig
// EM1Datas []EM1DataConfig
// Smokes []*SmokeConfig
}
func (r *ShellyGetConfigResponse) UnmarshalJSON(b []byte) error {
theRest := make(map[string]json.RawMessage)
if err := json.Unmarshal(b, &theRest); err != nil {
return err
}
if v, ok := theRest["ble"]; ok {
var s BLEConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.BLE = &s
}
if v, ok := theRest["sys"]; ok {
var s SysConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.System = &s
}
if v, ok := theRest["cloud"]; ok {
var s CloudConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Cloud = &s
}
if v, ok := theRest["mqtt"]; ok {
var s MQTTConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.MQTT = &s
}
if v, ok := theRest["wifi"]; ok {
var s WifiConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Wifi = &s
}
if v, ok := theRest["eth"]; ok {
var s EthConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Ethernet = &s
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("switch:%d", i)]
if !ok {
continue
}
var s SwitchConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Switches = append(r.Switches, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("cover:%d", i)]
if !ok {
continue
}
var s CoverConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Covers = append(r.Covers, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("input:%d", i)]
if !ok {
continue
}
var s InputConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Inputs = append(r.Inputs, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("light:%d", i)]
if !ok {
continue
}
var s LightConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Lights = append(r.Lights, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("humidity:%d", i)]
if !ok {
continue
}
var s HumidityConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Humidities = append(r.Humidities, &s)
}
for i := 0; i < 4; i++ {
v, ok := theRest[fmt.Sprintf("temperature:%d", i)]
if !ok {
continue
}
var s TemperatureConfig
if err := json.Unmarshal(v, &s); err != nil {
return err
}
r.Temperatures = append(r.Temperatures, &s)
}
return nil
}
type ShellyGetConfigRequest struct{}
func (r *ShellyGetConfigRequest) Method() string {
return "Shelly.GetConfig"
}
func (r *ShellyGetConfigRequest) NewTypedResponse() *ShellyGetConfigResponse {
return &ShellyGetConfigResponse{}
}
func (r *ShellyGetConfigRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyGetConfigRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ShellyGetConfigResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type ShellyListMethodsResponse struct {
// Methods is a list of allowed methods.
Methods []string `json:"methods,omitempty"`
}
type ShellyListMethodsRequest struct{}
func (r *ShellyListMethodsRequest) Method() string {
return "Shelly.ListMethods"
}
func (r *ShellyListMethodsRequest) NewTypedResponse() *ShellyListMethodsResponse {
return &ShellyListMethodsResponse{}
}
func (r *ShellyListMethodsRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyListMethodsRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ShellyListMethodsResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type ShellyListProfilesComponent struct {
// Type of component.
Type string `json:"type"`
// Count of component instances.
Count int `json:"count"`
}
type ShellyListProfilesResponse struct {
// Profiles is a KV mapping of available profiles to an enumeration of their components.
Profiles map[string][]ShellyListProfilesComponent `json:"profiles,omitempty"`
}
type ShellyListProfilesRequest struct{}
func (r *ShellyListProfilesRequest) Method() string {
return "Shelly.ListProfiles"
}
func (r *ShellyListProfilesRequest) NewTypedResponse() *ShellyListProfilesResponse {
return &ShellyListProfilesResponse{}
}
func (r *ShellyListProfilesRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellyListProfilesRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ShellyListProfilesResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type ShellySetProfileResponse struct {
// ProfileWas will be set to the profile before updating. If the new value is different
// than the profile_was value, the device will immediately reboot.
ProfileWas string `json:"profile_was,omitempty"`
}
type ShellySetProfileRequest struct {
// Profile is the name of the profile to set.
Profile string `json:"profile"`
}
func (r *ShellySetProfileRequest) Method() string {
return "Shelly.SetProfile"
}
func (r *ShellySetProfileRequest) NewTypedResponse() *ShellySetProfileResponse {
return &ShellySetProfileResponse{}
}
func (r *ShellySetProfileRequest) NewResponse() any {
return r.NewTypedResponse()
}
func (r *ShellySetProfileRequest) Do(
ctx context.Context,
c mgrpc.MgRPC,
credsCallback mgrpc.GetCredsCallback,
) (
*ShellySetProfileResponse,
*frame.Response,
error,
) {
resp := r.NewTypedResponse()
raw, err := Do(ctx, c, credsCallback, r, resp)
return resp, raw, err
}
type ShellyListTimezonesResponse struct {
// Timezones is a list of available timezones.
Timezones []string `json:"timezones"`
}
type ShellyListTimezonesRequest struct{}
func (r *ShellyListTimezonesRequest) Method() string {
return "Shelly.ListTimezones"