Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support durable custom names in config #67

Merged
merged 1 commit into from
Aug 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export interface Config extends PlatformConfig {
port?: number;
broadcast?: string;
address?: string;
devices?: { host: string }[];
devices?: { host?: string, mac?: string, name?: string }[];
}
export interface Device {
model: string;
Expand Down
14 changes: 8 additions & 6 deletions src/util/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export function registerDiscoveryHandler(
}
}

export function sendDiscoveyBroadcast(service: HomebridgeWizLan) {
export function sendDiscoveryBroadcast(service: HomebridgeWizLan) {
const { ADDRESS, MAC, BROADCAST } = getNetworkConfig(service);

const log = makeLogger(service, "Discovery");
Expand All @@ -235,11 +235,13 @@ export function sendDiscoveyBroadcast(service: HomebridgeWizLan) {
// Send discovery message to listed devices
if (Array.isArray(service.config.devices)) {
for (const device of service.config.devices) {
service.socket.send(
`{"method":"registration","params":{"phoneMac":"${MAC}","register":false,"phoneIp":"${ADDRESS}"}}`,
BROADCAST_PORT,
device.host
);
if (device.host) {
service.socket.send(
`{"method":"registration","params":{"phoneMac":"${MAC}","register":false,"phoneIp":"${ADDRESS}"}}`,
BROADCAST_PORT,
device.host
);
}
}
}
}
26 changes: 22 additions & 4 deletions src/wiz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { PLATFORM_NAME, PLUGIN_NAME } from "./constants";
import { Config, Device } from "./types";
import Accessories from './accessories';
import { bindSocket, createSocket, registerDiscoveryHandler, sendDiscoveyBroadcast } from "./util/network";
import { bindSocket, createSocket, registerDiscoveryHandler, sendDiscoveryBroadcast } from "./util/network";

export default class HomebridgeWizLan {
public readonly Service: typeof Service = this.api.hap.Service;
Expand Down Expand Up @@ -38,7 +38,7 @@ export default class HomebridgeWizLan {
// run the method to discover / register your devices as accessories
bindSocket(this, () => {
registerDiscoveryHandler(this, this.tryAddDevice.bind(this));
sendDiscoveyBroadcast(this);
sendDiscoveryBroadcast(this);
});
});
}
Expand Down Expand Up @@ -98,12 +98,29 @@ export default class HomebridgeWizLan {
}

const uuid = this.api.hap.uuid.generate(device.mac);
const name = `Wiz ${accessory.getName(device)} ${device.mac}`;
const defaultName = `Wiz ${accessory.getName(device)} ${device.mac}`;
let name = defaultName

const existingAccessory = this.accessories.find(
(accessory) => accessory.UUID === uuid
);

this.log.debug(`Considering alternative names in ${JSON.stringify(this.config.devices)} from ${JSON.stringify(this.config)}...`);
if (Array.isArray(this.config.devices)) {
this.log.debug(`Found some configs...`);
for (const configDevice of this.config.devices) {
this.log.debug(`Pondering ${JSON.stringify(configDevice)} versus ${JSON.stringify(device)}...`);
if ((configDevice.mac && device.mac == configDevice.mac) ||
(configDevice.host && device.ip == configDevice.host)) {
this.log.debug(`Found a match...`);
if (configDevice.name) {
this.log.debug(`Changing name to ${configDevice.name}...`);
name = configDevice.name;
}
}
}
}

// check the accessory was not restored from cache
if (!existingAccessory) {
// create a new accessory
Expand All @@ -122,7 +139,8 @@ export default class HomebridgeWizLan {
]);
} else {
existingAccessory.context = device;
this.log.info("Updating accessory:", name);
this.log.info(`Updating accessory: ${name}${name == existingAccessory.displayName ? "" : ` [formerly ${existingAccessory.displayName}]`}`);
existingAccessory.displayName = name;
this.api.updatePlatformAccessories([existingAccessory]);
// try initializing again in case it didn't the last time
// (e.g. platform upgrade)
Expand Down