Added option to enable services
This commit is contained in:
		| @@ -47,7 +47,18 @@ | |||||||
|             "Bin full sensor" |             "Bin full sensor" | ||||||
|           ] |           ] | ||||||
|         }, |         }, | ||||||
|         "uniqueItems": true |         "default": [ | ||||||
|  |           "Clean house", | ||||||
|  |           "Clean spot", | ||||||
|  |           "Go to dock", | ||||||
|  |           "Find me", | ||||||
|  |           "Schedule", | ||||||
|  |           "Eco", | ||||||
|  |           "Nogo lines", | ||||||
|  |           "Extra care", | ||||||
|  |           "Docked sensor", | ||||||
|  |           "Bin full sensor" | ||||||
|  |         ] | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|   }, |   }, | ||||||
|   | |||||||
| @@ -12,16 +12,16 @@ export class NeatoVacuumRobotAccessory | |||||||
| 	// Homebridge | 	// Homebridge | ||||||
| 	private log: Logger; | 	private log: Logger; | ||||||
| 	private batteryService: Service; | 	private batteryService: Service; | ||||||
| 	private cleanService: Service; | 	private cleanService: Service | null; | ||||||
| 	private findMeService: Service; | 	private findMeService: Service | null; | ||||||
| 	private goToDockService: Service; | 	private goToDockService: Service | null; | ||||||
| 	private dockStateService: Service; | 	private dockStateService: Service | null; | ||||||
| 	private binFullService: Service; | 	private binFullService: Service | null; | ||||||
| 	private ecoService: Service; | 	private ecoService: Service | null; | ||||||
| 	private noGoLinesService: Service; | 	private noGoLinesService: Service | null; | ||||||
| 	private extraCareService: Service; | 	private extraCareService: Service | null; | ||||||
| 	private scheduleService: Service; | 	private scheduleService: Service | null; | ||||||
| 	private spotCleanService: Service; | 	private spotCleanService: Service | null; | ||||||
|  |  | ||||||
| 	// Context | 	// Context | ||||||
| 	private robot: any; | 	private robot: any; | ||||||
| @@ -31,7 +31,7 @@ export class NeatoVacuumRobotAccessory | |||||||
| 	private readonly backgroundUpdateInterval: number; | 	private readonly backgroundUpdateInterval: number; | ||||||
| 	private readonly prefix: boolean; | 	private readonly prefix: boolean; | ||||||
| 	private readonly availableServices: string[]; | 	private readonly availableServices: string[]; | ||||||
| 	 |  | ||||||
| 	// Transient | 	// Transient | ||||||
| 	private isSpotCleaning: boolean; | 	private isSpotCleaning: boolean; | ||||||
| 	private timer: any; | 	private timer: any; | ||||||
| @@ -47,14 +47,13 @@ export class NeatoVacuumRobotAccessory | |||||||
| 			private readonly config: PlatformConfig) | 			private readonly config: PlatformConfig) | ||||||
| 	{ | 	{ | ||||||
| 		this.log = platform.log; | 		this.log = platform.log; | ||||||
| 		 |  | ||||||
| 		this.robot = accessory.context.robot; | 		this.robot = accessory.context.robot; | ||||||
| 		this.options = accessory.context.options || new Options(); | 		this.options = accessory.context.options || new Options(); | ||||||
|  |  | ||||||
| 		this.backgroundUpdateInterval = NeatoVacuumRobotAccessory.parseBackgroundUpdateInterval(this.config['backgroundUpdate']); | 		this.backgroundUpdateInterval = NeatoVacuumRobotAccessory.parseBackgroundUpdateInterval(this.config['backgroundUpdate']); | ||||||
| 		this.prefix = this.config['prefix'] || PREFIX; | 		this.prefix = this.config['prefix'] || PREFIX; | ||||||
| 		this.availableServices = this.config['services'] || SERVICES; | 		this.availableServices = this.config['services'] || SERVICES; | ||||||
| 		this.log.debug(JSON.stringify(this.availableServices)); |  | ||||||
|  |  | ||||||
| 		this.isSpotCleaning = false; | 		this.isSpotCleaning = false; | ||||||
|  |  | ||||||
| @@ -75,50 +74,82 @@ export class NeatoVacuumRobotAccessory | |||||||
| 					this.debug(DebugType.INFO, JSON.stringify("Error: " + error)); | 					this.debug(DebugType.INFO, JSON.stringify("Error: " + error)); | ||||||
| 				} | 				} | ||||||
| 				this.debug(DebugType.INFO, "Status: " + JSON.stringify(result)); | 				this.debug(DebugType.INFO, "Status: " + JSON.stringify(result)); | ||||||
|  | 				this.debug(DebugType.INFO, | ||||||
|  | 						"Config: Background Update Interval: " + this.backgroundUpdateInterval + ", Prefix: " + this.prefix + ", Enabled services: " + JSON.stringify(this.availableServices)); | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}); | ||||||
|  |  | ||||||
| 		// Services | 		// Services | ||||||
| 		this.cleanService = this.getSwitchService("Clean House"); | 		this.cleanService = this.getSwitchService(RobotService.CLEAN_HOUSE); | ||||||
| 		this.spotCleanService = this.getSwitchService("Clean Spot"); | 		this.spotCleanService = this.getSwitchService(RobotService.CLEAN_SPOT); | ||||||
| 		this.goToDockService = this.getSwitchService("Go to Dock"); | 		this.goToDockService = this.getSwitchService(RobotService.GO_TO_DOCK); | ||||||
| 		this.dockStateService = this.getOccupancyService("Docked") | 		this.dockStateService = this.getOccupancyService(RobotService.DOCKED) | ||||||
| 		this.binFullService = this.getOccupancyService("Bin Full") | 		this.binFullService = this.getOccupancyService(RobotService.BIN_FULL) | ||||||
| 		this.findMeService = this.getSwitchService("Find Me"); | 		this.findMeService = this.getSwitchService(RobotService.FIND_ME); | ||||||
| 		this.scheduleService = this.getSwitchService("Schedule"); | 		this.scheduleService = this.getSwitchService(RobotService.SCHEDULE); | ||||||
| 		this.ecoService = this.getSwitchService("Option: Eco Mode"); | 		this.ecoService = this.getSwitchService(RobotService.ECO); | ||||||
| 		this.noGoLinesService = this.getSwitchService("Option: NoGo Lines"); | 		this.noGoLinesService = this.getSwitchService(RobotService.NOGO_LINES); | ||||||
| 		this.extraCareService = this.getSwitchService("Option: Extra Care"); | 		this.extraCareService = this.getSwitchService(RobotService.EXTRA_CARE); | ||||||
| 		this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery) | 		this.batteryService = this.accessory.getService(this.platform.Service.Battery) || this.accessory.addService(this.platform.Service.Battery) | ||||||
|  |  | ||||||
| 		this.cleanService.getCharacteristic(this.platform.Characteristic.On) | 		if (this.cleanService) | ||||||
| 				.onSet(this.setCleanHouse.bind(this)) | 		{ | ||||||
| 				.onGet(this.getCleanHouse.bind(this)); | 			this.cleanService.getCharacteristic(this.platform.Characteristic.On) | ||||||
| 		this.spotCleanService.getCharacteristic(this.platform.Characteristic.On) | 					.onSet(this.setCleanHouse.bind(this)) | ||||||
| 				.onSet(this.setSpotClean.bind(this)) | 					.onGet(this.getCleanHouse.bind(this)); | ||||||
| 				.onGet(this.getSpotClean.bind(this)); | 		} | ||||||
| 		this.goToDockService.getCharacteristic(this.platform.Characteristic.On) | 		if (this.spotCleanService) | ||||||
| 				.onSet(this.setGoToDock.bind(this)) | 		{ | ||||||
| 				.onGet(this.getGoToDock.bind(this)); | 			this.spotCleanService.getCharacteristic(this.platform.Characteristic.On) | ||||||
| 		this.dockStateService.getCharacteristic(this.platform.Characteristic.OccupancyDetected) | 					.onSet(this.setSpotClean.bind(this)) | ||||||
| 				.onGet(this.getDocked.bind(this)); | 					.onGet(this.getSpotClean.bind(this)); | ||||||
| 		this.binFullService.getCharacteristic(this.platform.Characteristic.OccupancyDetected) | 		} | ||||||
| 				.onGet(this.getBinFull.bind(this)); | 		if (this.goToDockService) | ||||||
| 		this.findMeService.getCharacteristic(this.platform.Characteristic.On) | 		{ | ||||||
| 				.onSet(this.setFindMe.bind(this)) | 			this.goToDockService.getCharacteristic(this.platform.Characteristic.On) | ||||||
| 				.onGet(this.getFindMe.bind(this)); | 					.onSet(this.setGoToDock.bind(this)) | ||||||
| 		this.scheduleService.getCharacteristic(this.platform.Characteristic.On) | 					.onGet(this.getGoToDock.bind(this)); | ||||||
| 				.onSet(this.setSchedule.bind(this)) | 		} | ||||||
| 				.onGet(this.getSchedule.bind(this)); | 		if (this.dockStateService) | ||||||
| 		this.ecoService.getCharacteristic(this.platform.Characteristic.On) | 		{ | ||||||
| 				.onSet(this.setEco.bind(this)) | 			this.dockStateService.getCharacteristic(this.platform.Characteristic.OccupancyDetected) | ||||||
| 				.onGet(this.getEco.bind(this)); | 					.onGet(this.getDocked.bind(this)); | ||||||
| 		this.noGoLinesService.getCharacteristic(this.platform.Characteristic.On) | 		} | ||||||
| 				.onSet(this.setNoGoLines.bind(this)) | 		if (this.binFullService) | ||||||
| 				.onGet(this.getNoGoLines.bind(this)); | 		{ | ||||||
| 		this.extraCareService.getCharacteristic(this.platform.Characteristic.On) | 			this.binFullService.getCharacteristic(this.platform.Characteristic.OccupancyDetected) | ||||||
| 				.onSet(this.setExtraCare.bind(this)) | 					.onGet(this.getBinFull.bind(this)); | ||||||
| 				.onGet(this.getExtraCare.bind(this)); | 		} | ||||||
|  | 		if (this.findMeService) | ||||||
|  | 		{ | ||||||
|  | 			this.findMeService.getCharacteristic(this.platform.Characteristic.On) | ||||||
|  | 					.onSet(this.setFindMe.bind(this)) | ||||||
|  | 					.onGet(this.getFindMe.bind(this)); | ||||||
|  | 		} | ||||||
|  | 		if (this.scheduleService) | ||||||
|  | 		{ | ||||||
|  | 			this.scheduleService.getCharacteristic(this.platform.Characteristic.On) | ||||||
|  | 					.onSet(this.setSchedule.bind(this)) | ||||||
|  | 					.onGet(this.getSchedule.bind(this)); | ||||||
|  | 		} | ||||||
|  | 		if (this.ecoService) | ||||||
|  | 		{ | ||||||
|  | 			this.ecoService.getCharacteristic(this.platform.Characteristic.On) | ||||||
|  | 					.onSet(this.setEco.bind(this)) | ||||||
|  | 					.onGet(this.getEco.bind(this)); | ||||||
|  | 		} | ||||||
|  | 		if (this.noGoLinesService) | ||||||
|  | 		{ | ||||||
|  | 			this.noGoLinesService.getCharacteristic(this.platform.Characteristic.On) | ||||||
|  | 					.onSet(this.setNoGoLines.bind(this)) | ||||||
|  | 					.onGet(this.getNoGoLines.bind(this)); | ||||||
|  | 		} | ||||||
|  | 		if (this.extraCareService) | ||||||
|  | 		{ | ||||||
|  | 			this.extraCareService.getCharacteristic(this.platform.Characteristic.On) | ||||||
|  | 					.onSet(this.setExtraCare.bind(this)) | ||||||
|  | 					.onGet(this.getExtraCare.bind(this)); | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		// Start background update | 		// Start background update | ||||||
| 		this.updateRobotPeriodically().then(() => { | 		this.updateRobotPeriodically().then(() => { | ||||||
| @@ -140,23 +171,47 @@ export class NeatoVacuumRobotAccessory | |||||||
| 	private getSwitchService(serviceName: string) | 	private getSwitchService(serviceName: string) | ||||||
| 	{ | 	{ | ||||||
| 		let displayName = this.prefix ? this.robot.name + serviceName : serviceName; | 		let displayName = this.prefix ? this.robot.name + serviceName : serviceName; | ||||||
| 		return this.accessory.getService(displayName) || this.accessory.addService(this.platform.Service.Switch, displayName, serviceName) | 		 | ||||||
|  | 		if (this.availableServices.includes(serviceName)) | ||||||
|  | 		{ | ||||||
|  | 			return this.accessory.getService(displayName) || this.accessory.addService(this.platform.Service.Switch, displayName, serviceName); | ||||||
|  | 		} | ||||||
|  | 		else | ||||||
|  | 		{ | ||||||
|  | 			if(this.accessory.getService(displayName)) | ||||||
|  | 			{ | ||||||
|  | 				this.accessory.removeService(<Service>this.accessory.getService(displayName)); | ||||||
|  | 			} | ||||||
|  | 			return null; | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	private getOccupancyService(serviceName: string) | 	private getOccupancyService(serviceName: string) | ||||||
| 	{ | 	{ | ||||||
| 		let displayName = this.prefix ? this.robot.name + serviceName : serviceName; | 		let displayName = this.prefix ? this.robot.name + serviceName : serviceName; | ||||||
| 		return this.accessory.getService(displayName) || this.accessory.addService(this.platform.Service.OccupancySensor, displayName, serviceName) |  | ||||||
|  | 		if (this.availableServices.includes(serviceName)) | ||||||
|  | 		{ | ||||||
|  | 			return this.accessory.getService(displayName) || this.accessory.addService(this.platform.Service.OccupancySensor, displayName, serviceName); | ||||||
|  | 		} | ||||||
|  | 		else | ||||||
|  | 		{ | ||||||
|  | 			if(this.accessory.getService(displayName)) | ||||||
|  | 			{ | ||||||
|  | 				this.accessory.removeService(<Service>this.accessory.getService(displayName)); | ||||||
|  | 			} | ||||||
|  | 			return null; | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 	 |  | ||||||
| 	private static parseBackgroundUpdateInterval(configValue: any) | 	private static parseBackgroundUpdateInterval(configValue: any) | ||||||
| 	{ | 	{ | ||||||
| 		// Parse as number | 		// Parse as number | ||||||
| 		let backgroundUpdateInterval = parseInt(configValue) || BACKGROUND_INTERVAL; | 		let backgroundUpdateInterval = parseInt(configValue) || BACKGROUND_INTERVAL; | ||||||
| 		 |  | ||||||
| 		// must be integer and positive | 		// must be integer and positive | ||||||
| 		backgroundUpdateInterval = ((backgroundUpdateInterval % 1) !== 0 || backgroundUpdateInterval < 0) ? BACKGROUND_INTERVAL : backgroundUpdateInterval; | 		backgroundUpdateInterval = ((backgroundUpdateInterval % 1) !== 0 || backgroundUpdateInterval < 0) ? BACKGROUND_INTERVAL : backgroundUpdateInterval; | ||||||
| 		 |  | ||||||
| 		return backgroundUpdateInterval; | 		return backgroundUpdateInterval; | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| @@ -270,7 +325,10 @@ export class NeatoVacuumRobotAccessory | |||||||
| 			await this.updateRobot(); | 			await this.updateRobot(); | ||||||
|  |  | ||||||
| 			setTimeout(() => { | 			setTimeout(() => { | ||||||
| 				this.goToDockService.updateCharacteristic(this.platform.Characteristic.On, false); | 				if (this.goToDockService) | ||||||
|  | 				{ | ||||||
|  | 					this.goToDockService.updateCharacteristic(this.platform.Characteristic.On, false); | ||||||
|  | 				} | ||||||
| 			}, 1000); | 			}, 1000); | ||||||
|  |  | ||||||
| 			try | 			try | ||||||
| @@ -290,7 +348,7 @@ export class NeatoVacuumRobotAccessory | |||||||
| 				} | 				} | ||||||
| 				else | 				else | ||||||
| 				{ | 				{ | ||||||
| 					this.log.warn(this.robot.name + "=| Can't go to dock at the moment"); | 					this.log.warn("[" + this.robot.name + "] Can't go to dock at the moment"); | ||||||
| 				} | 				} | ||||||
| 			} | 			} | ||||||
| 			catch (error) | 			catch (error) | ||||||
| @@ -409,7 +467,10 @@ export class NeatoVacuumRobotAccessory | |||||||
| 		{ | 		{ | ||||||
| 			this.debug(DebugType.ACTION, "Find me"); | 			this.debug(DebugType.ACTION, "Find me"); | ||||||
| 			setTimeout(() => { | 			setTimeout(() => { | ||||||
| 				this.findMeService.updateCharacteristic(this.platform.Characteristic.On, false); | 				if (this.findMeService) | ||||||
|  | 				{ | ||||||
|  | 					this.findMeService.updateCharacteristic(this.platform.Characteristic.On, false); | ||||||
|  | 				} | ||||||
| 			}, 1000); | 			}, 1000); | ||||||
|  |  | ||||||
| 			try | 			try | ||||||
| @@ -510,33 +571,30 @@ export class NeatoVacuumRobotAccessory | |||||||
|  |  | ||||||
| 	async updateCharacteristics() | 	async updateCharacteristics() | ||||||
| 	{ | 	{ | ||||||
| 		// Update Switches | 		if (this.cleanService) | ||||||
| 		// Clean | 		{ | ||||||
| 		this.cleanService.updateCharacteristic(this.platform.Characteristic.On, await this.getCleanHouse()); | 			this.cleanService.updateCharacteristic(this.platform.Characteristic.On, await this.getCleanHouse()); | ||||||
|  | 		} | ||||||
| 		// Spot Clean | 		if (this.spotCleanService) | ||||||
| 		this.spotCleanService.updateCharacteristic(this.platform.Characteristic.On, await this.getSpotClean()); | 		{ | ||||||
|  | 			this.spotCleanService.updateCharacteristic(this.platform.Characteristic.On, await this.getSpotClean()); | ||||||
| 		// Go To Dock | 		} | ||||||
| 		this.goToDockService.updateCharacteristic(this.platform.Characteristic.On, await this.getGoToDock()); | 		if (this.goToDockService) | ||||||
|  | 		{ | ||||||
| 		// Docked | 			this.goToDockService.updateCharacteristic(this.platform.Characteristic.On, await this.getGoToDock()); | ||||||
| 		this.dockStateService.updateCharacteristic(this.platform.Characteristic.OccupancyDetected, await this.getDocked()); | 		} | ||||||
|  | 		if (this.dockStateService) | ||||||
| 		// Bin full | 		{ | ||||||
| 		this.binFullService.updateCharacteristic(this.platform.Characteristic.OccupancyDetected, await this.getBinFull()); | 			this.dockStateService.updateCharacteristic(this.platform.Characteristic.OccupancyDetected, await this.getDocked()); | ||||||
|  | 		} | ||||||
| 		// Schedule | 		if (this.binFullService) | ||||||
| 		this.scheduleService.updateCharacteristic(this.platform.Characteristic.On, await this.getSchedule()); | 		{ | ||||||
|  | 			this.binFullService.updateCharacteristic(this.platform.Characteristic.OccupancyDetected, await this.getBinFull()); | ||||||
| 		// Eco | 		} | ||||||
| 		this.ecoService.updateCharacteristic(this.platform.Characteristic.On, await this.getEco()); | 		if (this.scheduleService) | ||||||
|  | 		{ | ||||||
| 		// Extra Care | 			this.scheduleService.updateCharacteristic(this.platform.Characteristic.On, await this.getSchedule()); | ||||||
| 		this.extraCareService.updateCharacteristic(this.platform.Characteristic.On, await this.getExtraCare()); | 		} | ||||||
|  |  | ||||||
| 		// NoGo Lines |  | ||||||
| 		this.noGoLinesService.updateCharacteristic(this.platform.Characteristic.On, await this.getNoGoLines()); |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	private debug(debugType: DebugType, message: String) | 	private debug(debugType: DebugType, message: String) | ||||||
| @@ -569,20 +627,20 @@ enum DebugType | |||||||
| 	INFO | 	INFO | ||||||
| } | } | ||||||
|  |  | ||||||
| enum Action | enum RobotService | ||||||
| { | { | ||||||
| 	CLEAN_HOUSE = "CLEAN_HOUSE", | 	CLEAN_HOUSE = "Clean house", | ||||||
| 	CLEAN_SPOT = "CLEAN_SPOT", | 	CLEAN_SPOT = "Clean spot", | ||||||
| 	GO_TO_DOCK = "GO_TO_DOCK", | 	GO_TO_DOCK = "Go to dock", | ||||||
| 	DOCKED = "DOCKED", | 	DOCKED = "Docked sensor", | ||||||
| 	BIN_FULL = "BIN_FULL", | 	BIN_FULL = "Bin full sensor", | ||||||
| 	FIND_ME = "FIND_ME", | 	FIND_ME = "Find me", | ||||||
| 	SCHEDULE = "SCHEDULE", | 	SCHEDULE = "Schedule", | ||||||
| 	ECO = "ECO", | 	ECO = "Eco", | ||||||
| 	NOGO_LINES = "NOGO_LINES", | 	NOGO_LINES = "Nogo lines", | ||||||
| 	EXTRA_CARE = "EXTRA_CARE" | 	EXTRA_CARE = "Extra care" | ||||||
| } | } | ||||||
|  |  | ||||||
| const BACKGROUND_INTERVAL = 30; | const BACKGROUND_INTERVAL = 30; | ||||||
| const PREFIX = false; | const PREFIX = false; | ||||||
| const SERVICES = Object.values(Action); | const SERVICES = Object.values(RobotService); | ||||||
		Reference in New Issue
	
	Block a user