22
33var rfxcom = require ( 'rfxcom' ) ;
44import { IRfxcom } from './RfxcomBridge' ;
5- import { Settings , SettingHass } from './Settings' ;
5+ import { Settings , SettingHass , SettingDevice } from './Settings' ;
66import Mqtt from './Mqtt' ;
77import { DeviceEntity , DeviceBridge , BridgeInfo , MQTTMessage , MqttEventListener } from './models' ;
88import utils from './utils' ;
@@ -92,9 +92,11 @@ export default class Discovery implements MqttEventListener{
9292export class HomeassistantDiscovery extends AbstractDiscovery {
9393
9494 protected state : State ;
95+ protected devicesConfig : SettingDevice [ ] ;
9596
9697 constructor ( mqtt : Mqtt , rfxtrx : IRfxcom , config : Settings ) {
9798 super ( mqtt , rfxtrx , config ) ;
99+ this . devicesConfig = config . rfxcom . devices ;
98100 this . state = new State ( config ) ;
99101 }
100102
@@ -133,24 +135,41 @@ export class HomeassistantDiscovery extends AbstractDiscovery{
133135 // get from save state
134136 let entityState = this . state . get ( { id : entityName , type :deviceType , subtype :data . message . subtype } )
135137 this . updateEntityStateFromValue ( entityState , value ) ;
136- this . rfxtrx . sendCommand ( deviceType , subTypeValue , entityState . rfxFunction , id + "/" + unitCode ) ;
137- this . mqtt . publish ( this . mqtt . topics . devices + '/' + entityTopic , JSON . stringify ( entityState ) , ( error : any ) => { } , { retain : true , qos : 1 } ) ;
138+ this . rfxtrx . sendCommand ( deviceType , subTypeValue , entityState . rfxFunction , entityName ) ;
139+ this . mqtt . publish ( this . mqtt . topics . devices + '/' + entityName , JSON . stringify ( entityState ) , ( error : any ) => { } , { retain : true , qos : 1 } ) ;
138140 }
139141
140142 updateEntityStateFromValue ( entityState : any , value : string ) {
141- if ( entityState . type === 'lighting1' || entityState . type === 'lighting2' || entityState . type === 'lighting3'
142- || entityState . type === 'lighting5' || entityState . type === 'lighting6' ) {
143- if ( value === "On" || value === "Group On" ) {
144- //TODO load value from rfxcom commands
145- entityState . commandNumber = ( value === "Group On" ) ?4 :1 ;
143+ if ( entityState . deviceType === 'lighting1' || entityState . deviceType === 'lighting2' || entityState . deviceType === 'lighting3'
144+ || entityState . deviceType === 'lighting5' || entityState . deviceType === 'lighting6' ) {
145+ const cmd = value . toLowerCase ( ) . split ( " " )
146+ let command = cmd [ 0 ] ;
147+ if ( cmd [ 0 ] === "group" ) {
148+ command = cmd [ 1 ] ;
149+
150+ }
151+ if ( command === "on" ) {
152+ entityState . commandNumber = ( cmd [ 0 ] === "group" ) ?4 :1 ; //WORK only for lithing2
146153 entityState . rfxFunction = 'switchOn' ;
147- } else {
148- entityState . commandNumber = ( value === "Group On" ) ?3 :0 ;
149- entityState . rfxFunction = 'switchOff' ;
154+ } else if ( command === "off" ) {
155+ entityState . rfxFunction = ( cmd [ 0 ] === "group" ) ?3 :0 ; //WORK only for lithing2
156+ entityState . rfxCommand = 'switchOff' ;
157+ } else {
158+ if ( cmd [ 0 ] === "level" ) {
159+ entityState . rfxFunction = 'setLevel' ;
160+ entityState . rfxOpt = cmd [ 1 ] ;
161+ }
150162 }
151- entityState . command = value ;
163+ } else if ( entityState . deviceType === "lighting4" ) {
164+ entityState . rfxFunction = 'sendData' ;
165+ } else if ( entityState . deviceType === "chime1" ) {
166+ entityState . rfxFunction = 'chime' ;
167+ } else {
168+ logger . error ( 'device type (' + entityState . deviceType + ') not supported' ) ;
152169 }
153170
171+
172+
154173 //TODO get command for other deviceType
155174 }
156175
@@ -160,19 +179,42 @@ export class HomeassistantDiscovery extends AbstractDiscovery{
160179 const devicePrefix = this . config . discovery_device ;
161180 let id = payload . id ;
162181 let deviceId = payload . subTypeValue + "_" + id . replace ( "0x" , "" ) ;
182+ let deviceTopic = payload . id
183+ let deviceName = deviceId ;
163184 let entityId = payload . subTypeValue + "_" + id . replace ( "0x" , "" ) ;
164- let entityTopic = payload . id ;
165185 let entityName = payload . id ;
166-
186+ let entityTopic = payload . id
187+
188+ const deviceConf = this . devicesConfig . find ( ( dev : any ) => dev . id === id ) ;
189+
190+ if ( deviceConf ?. name !== undefined ) {
191+ entityTopic = deviceConf . name ;
192+ deviceTopic = deviceConf . name ;
193+ }
194+
195+
167196 if ( payload . unitCode !== undefined && ! this . rfxtrx . isGroup ( payload ) ) {
168197 entityId += '_' + payload . unitCode ;
169198 entityTopic += '/' + payload . unitCode ;
170199 entityName += '_' + payload . unitCode ;
200+ if ( deviceConf ?. units ) {
201+ deviceConf ?. units . forEach ( unit => {
202+ if ( parseInt ( unit . unitCode ) === parseInt ( payload . unitCode ) ) {
203+ if ( unit . name ! ) {
204+ entityTopic = unit . name ;
205+ }
206+ }
207+ } ) ;
208+ }
171209 }
172210
173211 this . state . set ( { id : entityName , type :payload . type , subtype :payload . subtype } , payload , "event" ) ;
174212
175- const deviceJson = new DeviceEntity ( [ devicePrefix + '_' + deviceId ] , deviceId ) ;
213+ if ( deviceConf ?. friendlyName ) {
214+ deviceName = deviceConf ?. friendlyName ;
215+ }
216+
217+ const deviceJson = new DeviceEntity ( [ devicePrefix + '_' + deviceId , devicePrefix + '_' + deviceName ] , deviceName ) ;
176218
177219 if ( payload . rssi !== undefined ) {
178220 const json = {
@@ -182,16 +224,16 @@ export class HomeassistantDiscovery extends AbstractDiscovery{
182224 entity_category : "diagnostic" ,
183225 icon : "mdi:signal" ,
184226 json_attributes_topic : this . topicDevice + '/' + entityTopic ,
185- name : deviceId + " Linkquality" ,
186- object_id : deviceId + '_linkquality' ,
227+ name : deviceName + " Linkquality" ,
228+ object_id : deviceTopic + '_linkquality' ,
187229 origin : this . discoveryOrigin ,
188230 state_class : "measurement" ,
189231 state_topic : this . topicDevice + '/' + entityTopic ,
190- unique_id : deviceId + '_linkquality_' + devicePrefix ,
232+ unique_id : deviceTopic + '_linkquality_' + devicePrefix ,
191233 unit_of_measurement :"dBm" ,
192234 value_template : "{{ value_json.rssi }}"
193235 } ;
194- this . publishDiscovery ( 'sensor/' + deviceId + '/linkquality/config' , JSON . stringify ( json ) ) ;
236+ this . publishDiscovery ( 'sensor/' + deviceTopic + '/linkquality/config' , JSON . stringify ( json ) ) ;
195237 }
196238 if ( payload . type === 'lighting1' || payload . type === 'lighting2' || payload . type === 'lighting3'
197239 || payload . type === 'lighting5' || payload . type === 'lighting6' ) {
@@ -221,7 +263,7 @@ export class HomeassistantDiscovery extends AbstractDiscovery{
221263 unique_id : entityId + '_' + devicePrefix ,
222264 value_template :"{{ value_json.command }}"
223265 } ;
224- this . publishDiscovery ( 'switch/' + entityId + '/config' , JSON . stringify ( json ) ) ;
266+ this . publishDiscovery ( 'switch/' + entityTopic + '/config' , JSON . stringify ( json ) ) ;
225267 }
226268
227269 }
0 commit comments