Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
Add excludeUSN option
Browse files Browse the repository at this point in the history
  • Loading branch information
langovoi committed Jan 15, 2019
1 parent 8ff06ba commit d07d021
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 17 deletions.
24 changes: 23 additions & 1 deletion Device.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Device {
return;
}

this._client.unsubscribe('RenderingControl', this._handleEvent);
this.stop();
this._client = null;
}

Expand All @@ -50,6 +50,8 @@ class Device {
this._updateAccessory(description);
}

this.onStart();

if (accessoryCreated) {
this._platform.addDevice(this);
this._platform.addAccessory(this.accessory);
Expand All @@ -61,6 +63,26 @@ class Device {
return this._accessory;
}

bye() {
if (this._client) {
if(Object.keys(this._client.subscriptions).length !== 0) {
this._client.subscriptions = {};
this._client.releaseEventingServer();
}
this._client = null;
}

this.onBye();
}

alive() {
this.onAlive();
}

onStart() {
throw new Error('onAlive must be implemented');
}

onAlive() {
throw new Error('onAlive must be implemented');
}
Expand Down
8 changes: 2 additions & 6 deletions MediaRenderer1.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ class MediaRenderer1 extends Device {
if (description.serialNumber) {
informationService.getCharacteristic(homebridge.hap.Characteristic.SerialNumber).updateValue(description.serialNumber);
}
}

onStart() {
this._client.subscribe('RenderingControl', this._handleEvent);
}

Expand All @@ -95,12 +97,6 @@ class MediaRenderer1 extends Device {
}

onBye() {
if (this._client) {
this._client.subscriptions = {};
this._client.releaseEventingServer();
this._client = null;
}

this.accessory.getService(homebridge.hap.Service.Lightbulb).getCharacteristic(homebridge.hap.Characteristic.On).updateValue(false);
}

Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ Simply add to your Homebridge config new platform called "UPnP" and restart Home
}
```

Also you can provide custom config for [node-ssdp](https://github.com/diversario/node-ssdp/tree/v4.0.0) by `ssdpClient` and `ssdpServer` options:
### Configure network interfaces

You can provide custom config for [node-ssdp](https://github.com/diversario/node-ssdp/tree/v4.0.0) by `ssdpClient` and `ssdpServer` options:

```json
{
Expand All @@ -52,3 +54,26 @@ Also you can provide custom config for [node-ssdp](https://github.com/diversario
]
}
```

### Exclude devices

Found device's USN. It must be in logs of Homebridge:

```
...
[2019-1-15 21:21:15] [UPnP] Add device [TV] Samsung 7 Series (55) (USN: XXXXXXXXX)
...
```

Then use `excludeUSN` option:

```json
{
"platforms": [
{
"platform": "UPnP",
"excludeUSN": ["XXXXXXXXX"]
}
]
}
```
55 changes: 47 additions & 8 deletions UPnPPlatform.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ class UPnPPlatform {
constructor(log, config, api) {
this.log = log;

this._config = config;

this._client = new Client({
...config.ssdpClient,
customLogger: this.log.debug.bind(this.log)
Expand All @@ -14,6 +16,9 @@ class UPnPPlatform {
customLogger: this.log.debug.bind(this.log)
});
this._devices = [];
this._preparingDevices = [];
this._accessoriesToRemove = [];

this._STToDevice = {
'urn:schemas-upnp-org:device:MediaRenderer:1': require('./MediaRenderer1')
};
Expand All @@ -31,20 +36,34 @@ class UPnPPlatform {
}
}

_isUSNExcluded(USN) {
const excluded = this._config.excludeUSN || [];

return excluded.includes(USN)
}

configureAccessory(accessory) {
const Device = this._STToDevice[accessory.context.ST];

if (Device) {
if (Device && !this._isUSNExcluded(accessory.context.USN)) {
const device = new Device(this, accessory.context.USN, accessory);

this.addDevice(device);
} else {
this.removeAccessory(accessory)
this._accessoriesToRemove.push(accessory);
}
}

_start() {
this.removeAccessories(this._accessoriesToRemove);

this._accessoriesToRemove = [];

this._client.on('response', (headers) => {
if(this._isUSNExcluded(headers.USN)) {
return;
}

const Device = this._STToDevice[headers.ST];

if(!Device) {
Expand All @@ -53,14 +72,25 @@ class UPnPPlatform {

let device = this._devices.find(device => device.USN === headers.USN);

if(!device) {
device = this._preparingDevices.find(device => device.USN === headers.USN);
}

if(!device) {
device = new Device(this, headers.USN);
this._preparingDevices.push(device);
}

device.setLocation(headers.LOCATION);
if(!device.hasLocation()) {
device.setLocation(headers.LOCATION);
}
});

this._server.on('advertise-alive', (headers) => {
if(this._isUSNExcluded(headers.USN)) {
return;
}

const Device = this._STToDevice[headers.NT];

if (!Device) {
Expand All @@ -69,12 +99,17 @@ class UPnPPlatform {

let device = this._devices.find(device => device.USN === headers.USN);

if(!device) {
device = this._preparingDevices.find(device => device.USN === headers.USN);
}

if(!device) {
device = new Device(this, headers.USN);
this._preparingDevices.push(device);
}

if(device.hasLocation()) {
device.onAlive();
device.alive();
} else {
device.setLocation(headers.LOCATION);
}
Expand All @@ -89,7 +124,7 @@ class UPnPPlatform {
const device = this._devices.find(device => device.USN === headers.USN);

if(device) {
device.onBye();
device.bye();
}
});

Expand All @@ -101,12 +136,16 @@ class UPnPPlatform {
this.api.registerPlatformAccessories(require('./package.json').name, 'UPnP', [accessory]);
}

removeAccessory(accessory) {
this.api.unregisterPlatformAccessories(require('./package.json').name, 'UPnP', [accessory]);
removeAccessories(accessories) {
this.api.unregisterPlatformAccessories(require('./package.json').name, 'UPnP', accessories);
}

addDevice(device) {
this.log('Add device', device.accessory.displayName);
if(this._preparingDevices.includes(device)) {
this._preparingDevices.splice(this._preparingDevices.indexOf(device), 1);
}

this.log('Add device', device.accessory.displayName, `(USN: ${device.accessory.context.USN})`);
this._devices.push(device);
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "homebridge-upnp",
"version": "0.1.8",
"version": "0.1.9",
"main": "index.js",
"author": "Mark Langovoi <[email protected]>",
"license": "MIT",
Expand Down

0 comments on commit d07d021

Please sign in to comment.