@@ -211,7 +211,6 @@ class ThingURLDevice extends Device {
211211 } else if ( link . href . startsWith ( 'ws://' ) ||
212212 link . href . startsWith ( 'wss://' ) ) {
213213 this . wsUrl = link . href ;
214- this . createWebSocket ( ) ;
215214 } else {
216215 this . links . push ( link ) ;
217216 }
@@ -225,9 +224,26 @@ class ThingURLDevice extends Device {
225224 }
226225 }
227226
228- // If there's no websocket endpoint, poll the device for updates.
229- if ( ! this . ws ) {
230- Promise . all ( this . propertyPromises ) . then ( ( ) => this . poll ( ) ) ;
227+ this . startReading ( ) ;
228+ }
229+
230+ startReading ( now = false ) {
231+ // If this is a recent gateway version, we hold off on polling/opening the
232+ // WebSocket until the user has actually saved the device.
233+ if ( Adapter . prototype . hasOwnProperty ( 'handleDeviceSaved' ) && ! now ) {
234+ return ;
235+ }
236+
237+ if ( this . wsUrl ) {
238+ if ( ! this . ws ) {
239+ this . createWebSocket ( ) ;
240+ }
241+ } else {
242+ // If there's no websocket endpoint, poll the device for updates.
243+ // eslint-disable-next-line no-lonely-if
244+ if ( ! this . scheduledUpdate ) {
245+ Promise . all ( this . propertyPromises ) . then ( ( ) => this . poll ( ) ) ;
246+ }
231247 }
232248 }
233249
@@ -490,6 +506,7 @@ class ThingURLAdapter extends Adapter {
490506 super ( addonManager , manifest . id , manifest . id ) ;
491507 addonManager . addAdapter ( this ) ;
492508 this . knownUrls = { } ;
509+ this . savedDevices = new Set ( ) ;
493510 this . pollInterval = POLL_INTERVAL ;
494511 }
495512
@@ -567,7 +584,7 @@ class ThingURLAdapter extends Adapter {
567584 if ( known ) {
568585 continue ;
569586 }
570- await this . removeThing ( this . devices [ id ] ) ;
587+ await this . removeThing ( this . devices [ id ] , true ) ;
571588 }
572589 await this . addDevice ( id , thingUrl , thingDescription , url ) ;
573590 }
@@ -580,7 +597,7 @@ class ThingURLAdapter extends Adapter {
580597 const device = this . devices [ id ] ;
581598 if ( device . mdnsUrl === url ) {
582599 device . closeWebSocket ( ) ;
583- this . removeThing ( device ) ;
600+ this . removeThing ( device , true ) ;
584601 }
585602 }
586603
@@ -604,20 +621,44 @@ class ThingURLAdapter extends Adapter {
604621 new ThingURLDevice ( this , deviceId , deviceURL , description , mdnsUrl ) ;
605622 Promise . all ( device . propertyPromises ) . then ( ( ) => {
606623 this . handleDeviceAdded ( device ) ;
624+
625+ if ( this . savedDevices . has ( deviceId ) ) {
626+ device . startReading ( true ) ;
627+ }
628+
607629 resolve ( device ) ;
608630 } ) . catch ( ( e ) => reject ( e ) ) ;
609631 }
610632 } ) ;
611633 }
612634
635+ /**
636+ * Handle a user saving a device. Note that incoming devices may not be for
637+ * this adapter.
638+ *
639+ * @param {string } deviceId - ID of the device
640+ */
641+ handleDeviceSaved ( deviceId ) {
642+ this . savedDevices . add ( deviceId ) ;
643+
644+ if ( this . devices . hasOwnProperty ( deviceId ) ) {
645+ this . devices [ deviceId ] . startReading ( true ) ;
646+ }
647+ }
648+
613649 /**
614650 * Remove a ThingURLDevice from the ThingURLAdapter.
615651 *
616652 * @param {Object } device The device to remove.
653+ * @param {boolean } internal Whether or not this is being called internally
617654 * @return {Promise } which resolves to the device removed.
618655 */
619- removeThing ( device ) {
656+ removeThing ( device , internal ) {
620657 return this . removeDeviceFromConfig ( device ) . then ( ( ) => {
658+ if ( ! internal ) {
659+ this . savedDevices . delete ( device . id ) ;
660+ }
661+
621662 if ( this . devices . hasOwnProperty ( device . id ) ) {
622663 this . handleDeviceRemoved ( device ) ;
623664 device . closeWebSocket ( ) ;
0 commit comments