77
88"use strict" ;
99
10- import uc from "uc- integration-api" ;
10+ import * as uc from "@unfoldedcircle/ integration-api" ;
1111import i18n from "i18n" ;
1212import path from "path" ;
1313import * as config from "./config.js" ;
@@ -18,6 +18,8 @@ import { log } from "./loggers.js";
1818// Node.js 20.11 / 21.2
1919const __dirname = import . meta. dirname ;
2020
21+ const driver = new uc . IntegrationAPI ( ) ;
22+
2123i18n . configure ( {
2224 locales : [ "en" , "de" , "fr" ] ,
2325 defaultLocale : "en" ,
@@ -31,34 +33,34 @@ i18n.configure({
3133 */
3234const configuredDevices = new Map ( ) ;
3335
34- uc . on ( uc . EVENTS . CONNECT , async ( ) => {
35- await uc . setDeviceState ( uc . DEVICE_STATES . CONNECTED ) ;
36+ driver . on ( uc . Events . Connect , async ( ) => {
37+ await driver . setDeviceState ( uc . DeviceStates . Connected ) ;
3638
3739 configuredDevices . forEach ( ( configured ) => configured . connect ( ) ) ;
3840} ) ;
3941
40- uc . on ( uc . EVENTS . DISCONNECT , async ( ) => {
41- await uc . setDeviceState ( uc . DEVICE_STATES . DISCONNECTED ) ;
42+ driver . on ( uc . Events . Disconnect , async ( ) => {
43+ await driver . setDeviceState ( uc . DeviceStates . Disconnected ) ;
4244
4345 configuredDevices . forEach ( ( configured ) => configured . disconnect ( ) ) ;
4446} ) ;
4547
46- uc . on ( uc . EVENTS . ENTER_STANDBY , async ( ) => {
48+ driver . on ( uc . Events . EnterStandby , async ( ) => {
4749 log . debug ( "Going to standby." ) ;
4850
4951 configuredDevices . forEach ( ( configured ) => configured . disconnect ( ) ) ;
5052} ) ;
5153
52- uc . on ( uc . EVENTS . EXIT_STANDBY , async ( ) => {
54+ driver . on ( uc . Events . ExitStandby , async ( ) => {
5355 log . debug ( "Came back from standby. Getting state updates." ) ;
5456
5557 configuredDevices . forEach ( ( configured ) => configured . connect ( ) ) ;
5658} ) ;
5759
58- uc . on ( uc . EVENTS . SUBSCRIBE_ENTITIES , async ( entityIds ) => {
60+ driver . on ( uc . Events . SubscribeEntities , async ( entityIds ) => {
5961 for ( const index in entityIds ) {
6062 const entityId = entityIds [ index ] ;
61- const entity = uc . configuredEntities . getEntity ( entityId ) ;
63+ const entity = driver . getConfiguredEntities ( ) . getEntity ( entityId ) ;
6264 if ( entity ) {
6365 log . debug ( `Subscribe: ${ entityId } ` ) ;
6466
@@ -82,7 +84,7 @@ uc.on(uc.EVENTS.SUBSCRIBE_ENTITIES, async (entityIds) => {
8284 }
8385} ) ;
8486
85- uc . on ( uc . EVENTS . UNSUBSCRIBE_ENTITIES , async ( entityIds ) => {
87+ driver . on ( uc . Events . UnsubscribeEntities , async ( entityIds ) => {
8688 entityIds . forEach ( ( entityId ) => {
8789 log . debug ( `Unsubscribe: ${ entityId } ` ) ;
8890 // TODO anything to do in unsubscribe?
@@ -95,28 +97,28 @@ uc.on(uc.EVENTS.UNSUBSCRIBE_ENTITIES, async (entityIds) => {
9597 *
9698 * Called by the integration-API if a command is sent to a configured entity.
9799 *
98- * @param {uc.Entities. Entity } entity button entity
100+ * @param {uc.Entity } entity button entity
99101 * @param {string } cmdId command
100102 * @param {Object<string, *> } params optional command parameters
101- * @return {Promise<string > } status of the command
103+ * @return {Promise<uc.StatusCodes > } status of the command
102104 */
103105async function cmdHandler ( entity , cmdId , params ) {
104106 const deviceId = _deviceIdFromEntityId ( entity . id ) ;
105107 if ( ! deviceId ) {
106- return uc . STATUS_CODES . SERVICE_NOT_FOUND ;
108+ return uc . StatusCodes . NotFound ;
107109 }
108110 const device = configuredDevices . get ( deviceId ) ;
109111 if ( ! device ) {
110- return uc . STATUS_CODES . SERVICE_NOT_FOUND ;
112+ return uc . StatusCodes . NotFound ;
111113 }
112114 if ( entity . entity_type === "ir_emitter" ) {
113115 switch ( cmdId ) {
114116 case "send_ir" :
115117 if ( params . format && params . format !== "PRONTO" ) {
116- return uc . STATUS_CODES . BAD_REQUEST ;
118+ return uc . StatusCodes . BadRequest ;
117119 }
118120 device . sendPronto ( params . port || "1:1" , params . code , params . repeat ) . catch ( ( reason ) => {
119- // TODO improve error handling. An invalid request should return BAD_REQUEST
121+ // TODO improve error handling. An invalid request should return BadRequest
120122 // Verify UI & core implementation: can we delay the cmd ack, or does it prevent IR-repeat?
121123 log . error ( "send_ir command failed: %s" , reason ) ;
122124 } ) ;
@@ -128,13 +130,13 @@ async function cmdHandler(entity, cmdId, params) {
128130 break ;
129131 default :
130132 // invalid command
131- return uc . STATUS_CODES . BAD_REQUEST ;
133+ return uc . StatusCodes . BadRequest ;
132134 }
133135 } else {
134- return uc . STATUS_CODES . BAD_REQUEST ;
136+ return uc . StatusCodes . BadRequest ;
135137 }
136138
137- return uc . STATUS_CODES . OK ;
139+ return uc . StatusCodes . Ok ;
138140}
139141
140142function _deviceIdFromEntityId ( entityId ) {
@@ -177,7 +179,7 @@ function _addConfiguredDevice(device, connect = true) {
177179 break ;
178180 case DEVICE_STATES . OFFLINE :
179181 // hack: UNAVAILABLE is a common state for all entity types
180- newState = uc . Entities . Sensor . STATES . UNAVAILABLE ;
182+ newState = uc . SensorStates . Unavailable ;
181183 break ;
182184 default :
183185 log . warn ( "Unhandled device state event:" , data . state ) ;
@@ -186,7 +188,7 @@ function _addConfiguredDevice(device, connect = true) {
186188
187189 const entityIds = configured . entityIds ( ) ;
188190 for ( const entityId of entityIds ) {
189- const entity = uc . configuredEntities . getEntity ( entityId ) ;
191+ const entity = driver . getConfiguredEntities ( ) . getEntity ( entityId ) ;
190192 if ( ! entity ) {
191193 continue ;
192194 }
@@ -195,10 +197,10 @@ function _addConfiguredDevice(device, connect = true) {
195197 continue ;
196198 }
197199
198- uc . configuredEntities . updateEntityAttributes (
200+ driver . getConfiguredEntities ( ) . updateEntityAttributes (
199201 entityId ,
200202 // hack: state key string is always the same, independent of entity type
201- new Map ( [ [ uc . Entities . Sensor . ATTRIBUTES . STATE , newState ] ] )
203+ { [ uc . SensorAttributes . State ] : newState }
202204 ) ;
203205 }
204206 } ) ;
@@ -225,11 +227,11 @@ function _registerAvailableEntities(device) {
225227 const entities = device . entities ( ) ;
226228
227229 for ( const entity of entities ) {
228- if ( uc . availableEntities . contains ( entity . id ) ) {
229- uc . availableEntities . removeEntity ( entity . id ) ;
230+ if ( driver . getAvailableEntities ( ) . contains ( entity . id ) ) {
231+ driver . getAvailableEntities ( ) . removeEntity ( entity . id ) ;
230232 }
231233 entity . setCmdHandler ( cmdHandler ) ;
232- uc . availableEntities . addEntity ( entity ) ;
234+ driver . getAvailableEntities ( ) . addAvailableEntity ( entity ) ;
233235 }
234236
235237 return true ;
@@ -256,8 +258,8 @@ function onDeviceRemoved(device) {
256258 configured . removeAllListeners ( ) ;
257259 } ) ;
258260 configuredDevices . clear ( ) ;
259- uc . configuredEntities . clear ( ) ;
260- uc . availableEntities . clear ( ) ;
261+ driver . getConfiguredEntities ( ) . clear ( ) ;
262+ driver . getAvailableEntities ( ) . clear ( ) ;
261263 } else if ( configuredDevices . has ( device . id ) ) {
262264 log . debug ( "Disconnecting from removed device %s" , device . id ) ;
263265 const configured = configuredDevices . get ( device . id ) ;
@@ -270,25 +272,25 @@ function onDeviceRemoved(device) {
270272
271273 const ids = device . entityIds ( ) ;
272274 for ( const entityId of ids ) {
273- uc . configuredEntities . removeEntity ( entityId ) ;
274- uc . availableEntities . removeEntity ( entityId ) ;
275+ driver . getConfiguredEntities ( ) . removeEntity ( entityId ) ;
276+ driver . getAvailableEntities ( ) . removeEntity ( entityId ) ;
275277 }
276278 }
277279}
278280
279281async function main ( ) {
280282 // load configured devices
281- config . devices . init ( uc . configDirPath , onDeviceAdded , onDeviceRemoved ) ;
283+ config . devices . init ( driver . getConfigDirPath ( ) , onDeviceAdded , onDeviceRemoved ) ;
282284
283285 // Note: device will be moved to configured devices with the subscribe_events request!
284286 // This will also start the device connection.
285287 config . devices . all ( ) . forEach ( ( device ) => {
286288 _addConfiguredDevice ( device , false ) ;
287289 } ) ;
288290
289- uc . init ( "driver.json" , driverSetupHandler ) ;
291+ driver . init ( "driver.json" , driverSetupHandler ) ;
290292
291- const info = uc . getDriverVersion ( ) ;
293+ const info = driver . getDriverVersion ( ) ;
292294 log . info ( "Global Caché integration %s started" , info . version . driver ) ;
293295}
294296
0 commit comments