github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/system/zigbee2mqtt/device.go (about) 1 // This file is part of the Smart Home 2 // Program complex distribution https://github.com/e154/smart-home 3 // Copyright (C) 2016-2023, Filippov Alex 4 // 5 // This library is free software: you can redistribute it and/or 6 // modify it under the terms of the GNU Lesser General Public 7 // License as published by the Free Software Foundation; either 8 // version 3 of the License, or (at your option) any later version. 9 // 10 // This library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 // Library General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public 16 // License along with this library. If not, see 17 // <https://www.gnu.org/licenses/>. 18 19 package zigbee2mqtt 20 21 import ( 22 "sync" 23 24 "github.com/e154/smart-home/models" 25 ) 26 27 // Device ... 28 type Device struct { 29 friendlyName string 30 modelLock *sync.Mutex 31 model models.Zigbee2mqttDevice 32 payload DeviceInfo 33 } 34 35 // NewDevice ... 36 func NewDevice(friendlyName string, model *models.Zigbee2mqttDevice) *Device { 37 return &Device{ 38 friendlyName: friendlyName, 39 modelLock: &sync.Mutex{}, 40 model: *model, 41 } 42 } 43 44 // AddFunc ... 45 func (d *Device) AddFunc(name string) { 46 d.modelLock.Lock() 47 defer d.modelLock.Unlock() 48 for _, f := range d.model.Functions { 49 if name == f { 50 return 51 } 52 } 53 d.model.Functions = append(d.model.Functions, name) 54 } 55 56 // DeviceType ... 57 func (d *Device) DeviceType(devType string) { 58 d.modelLock.Lock() 59 defer d.modelLock.Unlock() 60 61 if d.model.Type != "" && devType == "sensor" { 62 return 63 } 64 d.model.Type = devType 65 } 66 67 // GetModel ... 68 func (d *Device) GetModel() models.Zigbee2mqttDevice { 69 d.modelLock.Lock() 70 defer d.modelLock.Unlock() 71 return d.model 72 } 73 74 // SetStatus ... 75 func (d *Device) SetStatus(status string) { 76 d.modelLock.Lock() 77 d.model.Status = status 78 d.modelLock.Unlock() 79 } 80 81 // Status ... 82 func (d *Device) Status() string { 83 d.modelLock.Lock() 84 defer d.modelLock.Unlock() 85 return d.model.Status 86 } 87 88 // SetName ... 89 func (d *Device) SetName(name string) { 90 d.modelLock.Lock() 91 d.model.Name = name 92 d.modelLock.Unlock() 93 } 94 95 // SetModel ... 96 func (d *Device) SetModel(model string) { 97 d.modelLock.Lock() 98 d.model.Model = model 99 d.modelLock.Unlock() 100 } 101 102 // SetDescription ... 103 func (d *Device) SetDescription(desc string) { 104 d.modelLock.Lock() 105 d.model.Description = desc 106 d.modelLock.Unlock() 107 } 108 109 // SetVendor ... 110 func (d *Device) SetVendor(vendor string) { 111 d.modelLock.Lock() 112 d.model.Manufacturer = vendor 113 d.modelLock.Unlock() 114 } 115 116 // GetImage ... 117 func (d *Device) GetImage() string { 118 d.modelLock.Lock() 119 defer d.modelLock.Unlock() 120 d.model.GetImageUrl() 121 return d.model.ImageUrl 122 }