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
@@ -132,25 +134,43 @@ export class HomeassistantDiscovery extends AbstractDiscovery{
132134
133135 // get from save state
134136 let entityState = this . state . get ( { id : entityName , type :deviceType , subtype :data . message . subtype } )
137+ entityState . deviceType = deviceType ;
135138 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 } ) ;
139+ this . rfxtrx . sendCommand ( deviceType , subTypeValue , entityState . rfxFunction , entityTopic ) ;
140+ this . mqtt . publish ( this . mqtt . topics . devices + '/' + entityName , JSON . stringify ( entityState ) , ( error : any ) => { } , { retain : true , qos : 1 } ) ;
138141 }
139142
140143 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 ;
144+ if ( entityState . deviceType === 'lighting1' || entityState . deviceType === 'lighting2' || entityState . deviceType === 'lighting3'
145+ || entityState . deviceType === 'lighting5' || entityState . deviceType === 'lighting6' ) {
146+ const cmd = value . toLowerCase ( ) . split ( " " )
147+ let command = cmd [ 0 ] ;
148+ if ( cmd [ 0 ] === "group" ) {
149+ command = cmd [ 1 ] ;
150+
151+ }
152+ if ( command === "on" ) {
153+ entityState . commandNumber = ( cmd [ 0 ] === "group" ) ?4 :1 ; //WORK only for lithing2
146154 entityState . rfxFunction = 'switchOn' ;
147- } else {
148- entityState . commandNumber = ( value === "Group On" ) ?3 :0 ;
149- entityState . rfxFunction = 'switchOff' ;
155+ } else if ( command === "off" ) {
156+ entityState . rfxFunction = ( cmd [ 0 ] === "group" ) ?3 :0 ; //WORK only for lithing2
157+ entityState . rfxCommand = 'switchOff' ;
158+ } else {
159+ if ( cmd [ 0 ] === "level" ) {
160+ entityState . rfxFunction = 'setLevel' ;
161+ entityState . rfxOpt = cmd [ 1 ] ;
162+ }
150163 }
151- entityState . command = value ;
164+ } else if ( entityState . deviceType === "lighting4" ) {
165+ entityState . rfxFunction = 'sendData' ;
166+ } else if ( entityState . deviceType === "chime1" ) {
167+ entityState . rfxFunction = 'chime' ;
168+ } else {
169+ logger . error ( 'device type (' + entityState . deviceType + ') not supported' ) ;
152170 }
153171
172+
173+
154174 //TODO get command for other deviceType
155175 }
156176
@@ -160,19 +180,42 @@ export class HomeassistantDiscovery extends AbstractDiscovery{
160180 const devicePrefix = this . config . discovery_device ;
161181 let id = payload . id ;
162182 let deviceId = payload . subTypeValue + "_" + id . replace ( "0x" , "" ) ;
183+ let deviceTopic = payload . id
184+ let deviceName = deviceId ;
163185 let entityId = payload . subTypeValue + "_" + id . replace ( "0x" , "" ) ;
164- let entityTopic = payload . id ;
165186 let entityName = payload . id ;
166-
187+ let entityTopic = payload . id
188+
189+ const deviceConf = this . devicesConfig . find ( ( dev : any ) => dev . id === id ) ;
190+
191+ if ( deviceConf ?. name !== undefined ) {
192+ entityTopic = deviceConf . name ;
193+ deviceTopic = deviceConf . name ;
194+ }
195+
196+
167197 if ( payload . unitCode !== undefined && ! this . rfxtrx . isGroup ( payload ) ) {
168198 entityId += '_' + payload . unitCode ;
169199 entityTopic += '/' + payload . unitCode ;
170200 entityName += '_' + payload . unitCode ;
201+ if ( deviceConf ?. units ) {
202+ deviceConf ?. units . forEach ( unit => {
203+ if ( parseInt ( unit . unitCode ) === parseInt ( payload . unitCode ) ) {
204+ if ( unit . name ! ) {
205+ entityTopic = unit . name ;
206+ }
207+ }
208+ } ) ;
209+ }
171210 }
172211
173212 this . state . set ( { id : entityName , type :payload . type , subtype :payload . subtype } , payload , "event" ) ;
174213
175- const deviceJson = new DeviceEntity ( [ devicePrefix + '_' + deviceId ] , deviceId ) ;
214+ if ( deviceConf ?. friendlyName ) {
215+ deviceName = deviceConf ?. friendlyName ;
216+ }
217+
218+ const deviceJson = new DeviceEntity ( [ devicePrefix + '_' + deviceId , devicePrefix + '_' + deviceName ] , deviceName ) ;
176219
177220 if ( payload . rssi !== undefined ) {
178221 const json = {
@@ -182,16 +225,16 @@ export class HomeassistantDiscovery extends AbstractDiscovery{
182225 entity_category : "diagnostic" ,
183226 icon : "mdi:signal" ,
184227 json_attributes_topic : this . topicDevice + '/' + entityTopic ,
185- name : deviceId + " Linkquality" ,
186- object_id : deviceId + '_linkquality' ,
228+ name : deviceName + " Linkquality" ,
229+ object_id : deviceTopic + '_linkquality' ,
187230 origin : this . discoveryOrigin ,
188231 state_class : "measurement" ,
189232 state_topic : this . topicDevice + '/' + entityTopic ,
190- unique_id : deviceId + '_linkquality_' + devicePrefix ,
233+ unique_id : deviceTopic + '_linkquality_' + devicePrefix ,
191234 unit_of_measurement :"dBm" ,
192235 value_template : "{{ value_json.rssi }}"
193236 } ;
194- this . publishDiscovery ( 'sensor/' + deviceId + '/linkquality/config' , JSON . stringify ( json ) ) ;
237+ this . publishDiscovery ( 'sensor/' + deviceTopic + '/linkquality/config' , JSON . stringify ( json ) ) ;
195238 }
196239 if ( payload . type === 'lighting1' || payload . type === 'lighting2' || payload . type === 'lighting3'
197240 || payload . type === 'lighting5' || payload . type === 'lighting6' ) {
@@ -221,7 +264,7 @@ export class HomeassistantDiscovery extends AbstractDiscovery{
221264 unique_id : entityId + '_' + devicePrefix ,
222265 value_template :"{{ value_json.command }}"
223266 } ;
224- this . publishDiscovery ( 'switch/' + entityId + '/config' , JSON . stringify ( json ) ) ;
267+ this . publishDiscovery ( 'switch/' + entityTopic + '/config' , JSON . stringify ( json ) ) ;
225268 }
226269
227270 }
0 commit comments