homebridge-roborock-vacuum-update
Version:
Comprehensive Homebridge plugin for Roborock vacuum cleaners with full HomeKit integration including mopping, dock features, and advanced controls.
87 lines • 3.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RoomsService = void 0;
const rxjs_1 = require("rxjs");
const plugin_service_class_1 = require("./plugin_service_class");
const ensure_name_1 = require("./utils/ensure_name");
class RoomsService extends plugin_service_class_1.PluginServiceClass {
constructor(coreContext, accessory, setCleaning) {
super(coreContext);
this.accessory = accessory;
this.setCleaning = setCleaning;
this.roomIdsToClean = new Set();
this.rooms = {};
this.roomTimeout = null;
if (this.config.rooms) {
for (const room of this.config.rooms) {
this.createRoom(room.id, room.name);
}
}
}
async init() {
// Reset room switches when cleaning stops
this.deviceManager.stateChanged$
.pipe((0, rxjs_1.filter)(({ key }) => key === "state"))
.subscribe(({ value }) => {
const isCleaning = this.deviceManager.isCleaning;
if (!isCleaning) {
this.services.forEach((room) => {
room
.getCharacteristic(this.hap.Characteristic.On)
.updateValue(false);
});
this.roomIdsToClean.clear();
}
});
}
get services() {
return [...Object.values(this.rooms)];
}
createRoom(roomId, roomName) {
this.log.info(`Creating room switch: ${roomName} (ID: ${roomId})`);
const switchName = `${this.config.cleanword} ${roomName}`;
const room = Object.assign(this.accessory.getServiceById(this.hap.Service.Switch, `room-${roomId}`)
|| this.accessory.addService(this.hap.Service.Switch, switchName, `room-${roomId}`), { roomId });
(0, ensure_name_1.ensureName)(this.hap, room, switchName);
room
.getCharacteristic(this.hap.Characteristic.On)
.onGet(() => this.getCleaningRoom(room.roomId))
.onSet((newState) => this.setCleaningRoom(newState, room.roomId));
this.rooms[roomName] = room;
}
async getCleaningRoom(roomId) {
return this.roomIdsToClean.has(roomId);
}
async setCleaningRoom(state, roomId) {
try {
if (state && !this.deviceManager.isCleaning && !this.deviceManager.isPaused) {
this.log.info(`Enable cleaning Room ID ${roomId}`);
this.roomIdsToClean.delete(roomId);
this.roomIdsToClean.add(roomId);
this.checkRoomTimeout();
}
else if (!state && !this.deviceManager.isCleaning && !this.deviceManager.isPaused) {
this.log.info(`Disable cleaning Room ID ${roomId}`);
this.roomIdsToClean.delete(roomId);
this.checkRoomTimeout();
}
}
catch (err) {
this.log.error(`setCleaningRoom failed:`, err);
throw err;
}
}
checkRoomTimeout() {
if (this.config.roomTimeout > 0) {
this.log.info(`Starting timeout to clean rooms (${this.config.roomTimeout}s)`);
if (this.roomTimeout) {
clearTimeout(this.roomTimeout);
}
if (this.roomIdsToClean.size > 0) {
this.roomTimeout = setTimeout(() => this.setCleaning(true), this.config.roomTimeout * 1000);
}
}
}
}
exports.RoomsService = RoomsService;
//# sourceMappingURL=rooms_service.js.map