Skip to content
Open
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
9 changes: 9 additions & 0 deletions src/colorlightservice.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AdaptiveLightingController, AdaptiveLightingControllerMode } from "homebridge";
import {
LightService,
LightServiceParameters as LightServiceParameters,
Expand All @@ -10,10 +11,18 @@ import {
} from "./lightservice";

export class ColorLightService extends LightService implements ConcreteLightService {
private adaptiveLightingController?: AdaptiveLightingController;

constructor(parameters: LightServiceParameters) {
super(parameters);
this.service.displayName = "Color Light";
this.installHandlers();
if (this.platform.config.ctforcolor) {
this.adaptiveLightingController = new this.platform.AdaptiveLightingController(this.service, {
controllerMode: AdaptiveLightingControllerMode.AUTOMATIC
});
this.accessory.configureController(this.adaptiveLightingController);
}
}

private async installHandlers() {
Expand Down
28 changes: 28 additions & 0 deletions src/specs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,33 @@ export const MODEL_SPECS: { [index: string]: Specs } = {
backgroundLight: true,
name: "Monitor Hanging Light",
color: false
},
plate2: {
colorTemperature: { min: 1700, max: 6500 },
nightLight: false,
backgroundLight: false,
name: "Smart Light Panels",
color: true
},
color8: {
colorTemperature: { min: 1700, max: 6500 },
nightLight: false,
backgroundLight: false,
name: "W3 Color Lightbulb",
color: true
},
colora: {
colorTemperature: { min: 1700, max: 6500 },
nightLight: false,
backgroundLight: false,
name: "W3 Color Lightbulb",
color: true
},
bslamp3: {
colorTemperature: { min: 1700, max: 6500 },
nightLight: false,
backgroundLight: false,
name: "Bedside Lamp D2",
color: true
}
};
12 changes: 10 additions & 2 deletions src/yeeaccessory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,10 @@ export class YeeAccessory {
this.error("Failed to retrieve attributes", error);
}

if (this.interval) {
clearInterval(this.interval);
delete this.interval;
}
if (this.platform.config.interval !== 0) {
this.interval = setInterval(this.onInterval, this.platform.config.interval || 60_000);
}
Expand All @@ -386,6 +390,10 @@ export class YeeAccessory {
clearInterval(this.interval);
delete this.interval;
}
for (const [key, item] of this.transactions.entries()) {
item.reject(new Error("disconnected"));
this.transactions.delete(key);
}
};

private onDeviceError = (error) => {
Expand Down Expand Up @@ -488,7 +496,7 @@ export class YeeAccessory {
private clearOldTransactions() {
for (const [key, item] of this.transactions.entries()) {
// clear transactions older than 60s
if (item.timestamp > Date.now() + 60_000) {
if (Date.now() - item.timestamp > 60_000) {
this.log(`error: timeout for request ${key}`);
item.reject(new Error("timeout"));
this.transactions.delete(key);
Expand All @@ -499,7 +507,7 @@ export class YeeAccessory {
private onInterval = () => {
if (this.connected) {
// if flooded wait for 5 minutes
if (this.floodAlarm && Date.now() - this.floodAlarm > 180_000_000) {
if (this.floodAlarm && Date.now() - this.floodAlarm > 300_000) {
this.log(`flooded. waiting ${(Date.now() - this.floodAlarm) / 60_000}s`);
} else {
// seconds since last update
Expand Down
18 changes: 15 additions & 3 deletions src/yeedevice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ export class Device extends EventEmitter {
connect() {
try {
this.forceDisconnect = false;
if (this.socket) {
this.socket.removeAllListeners();
this.socket.destroy();
this.socket = undefined;
}
this.socket = new net.Socket({ allowHalfOpen: false });
this.bindSocket();
this.socket.connect({ host: this.info.host, port: this.info.port }, () => {
Expand Down Expand Up @@ -138,20 +143,27 @@ export class Device extends EventEmitter {
}

if (error) {
if (error.message.includes("EHOSTUNREACH")) {
const msg = error.message ?? "";
if (msg.includes("EHOSTUNREACH")) {
// unreachable, no need to retry
this.disconnect(true);
} else {
console.log(`Socket Closed with error "${error.name}, retrying to connect in 5s"`, error.message);
console.log(`Socket Closed with error "${error.name}, retrying to connect in 5s"`, msg);
this.disconnect(false);
if (this.retryTimer) {
clearTimeout(this.retryTimer);
delete this.retryTimer;
}
this.retryTimer = setTimeout(this.connect.bind(this), 5000);
this.retryTimer = setTimeout(this.reconnect.bind(this), 5000);
}
} else {
// graceful close (e.g. device rebooted) — schedule reconnect
this.disconnect(false);
if (this.retryTimer) {
clearTimeout(this.retryTimer);
delete this.retryTimer;
}
this.retryTimer = setTimeout(this.reconnect.bind(this), 5000);
}
}

Expand Down