generated from homebridge/homebridge-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 51
Initial air purifier support #1012
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
Open
Avamander
wants to merge
15
commits into
itavero:master
Choose a base branch
from
Avamander:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6b56147
Initial air purifier support
Avamander fa201c2
Changed double quotes to single quotes
Avamander bf1a510
Minor touch-ups
Avamander 5d43509
Fixed a minor typo
Avamander bf65dff
Removed else if because the linter complained
Avamander cf7c753
Added missing import, fixed invalid characteristics
Avamander 52ed269
Update air_purifier.ts
Avamander c84ace1
Merge branch 'itavero:master' into master
Avamander 9b00bff
Update air_purifier.ts
Avamander 3ab23a0
Update air_quality.ts
Avamander e981b55
Update air_purifier.ts
Avamander 101798b
Update air_purifier.ts
Avamander d4f0c19
Update air_purifier.ts
Avamander 597765d
Update air_purifier.ts
Avamander 9658e19
Update air_purifier.ts
Avamander File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
import { BasicAccessory, ServiceCreator, ServiceHandler } from './interfaces'; | ||
import { | ||
exposesCanBeGet, | ||
ExposesEntry, | ||
ExposesEntryWithProperty, | ||
exposesHasNumericProperty, | ||
exposesHasProperty, | ||
exposesIsPublished, | ||
} from '../z2mModels'; | ||
import { hap } from '../hap'; | ||
import { copyExposesRangeToCharacteristic, getOrAddCharacteristic, groupByEndpoint } from '../helpers'; | ||
import { Characteristic, CharacteristicSetCallback, CharacteristicValue, Service, WithUUID } from 'homebridge'; | ||
|
||
export class AirPurifierCreator implements ServiceCreator { | ||
createServicesFromExposes(accessory: BasicAccessory, exposes: ExposesEntry[]): void { | ||
const endpointMap = groupByEndpoint( | ||
exposes | ||
.filter( | ||
(e) => | ||
exposesHasProperty(e) && | ||
exposesIsPublished(e) && | ||
AirPurifierHandler.propertyFactories.find((f) => f.canUseExposesEntry(e)) !== undefined | ||
) | ||
.map((e) => e as ExposesEntryWithProperty) | ||
); | ||
endpointMap.forEach((value, key) => { | ||
if (!accessory.isServiceHandlerIdKnown(AirPurifierHandler.generateIdentifier(key))) { | ||
this.createService(key, value, accessory); | ||
} | ||
}); | ||
} | ||
|
||
private createService(endpoint: string | undefined, exposes: ExposesEntryWithProperty[], accessory: BasicAccessory): void { | ||
try { | ||
const handler = new AirPurifierHandler(endpoint, exposes, accessory); | ||
accessory.registerServiceHandler(handler); | ||
} catch (error) { | ||
accessory.log.warn( | ||
'Failed to setup Air Purifier service ' + `for accessory ${accessory.displayName} for endpoint ${endpoint}: ${error}` | ||
); | ||
} | ||
} | ||
} | ||
|
||
export declare type WithExposesValidator<T> = T & { | ||
canUseExposesEntry(entry: ExposesEntry): boolean; | ||
}; | ||
|
||
interface AirPurifierProperty { | ||
readonly expose: ExposesEntryWithProperty; | ||
readonly state: number; | ||
updateState(state: Record<string, unknown>): void; | ||
} | ||
|
||
abstract class PassthroughAirPurifierProperty implements AirPurifierProperty { | ||
public state: number; | ||
|
||
constructor( | ||
public expose: ExposesEntryWithProperty, | ||
protected accessory: BasicAccessory, | ||
protected service: Service, | ||
protected characteristic: WithUUID<new () => Characteristic> | ||
) { | ||
this.state = 0; | ||
const c = getOrAddCharacteristic(service, characteristic); | ||
c.on('set', this.handleSetOn.bind(this)); | ||
copyExposesRangeToCharacteristic(expose, c); | ||
} | ||
|
||
updateState(state: Record<string, unknown>): void { | ||
if (this.expose.property in state) { | ||
const sensorValue = state[this.expose.property] as CharacteristicValue; | ||
if (sensorValue !== null && sensorValue !== undefined) { | ||
this.service.updateCharacteristic(this.characteristic, sensorValue); | ||
this.state = this.convertToAirPurifier(sensorValue) ?? 0; | ||
} | ||
} | ||
} | ||
|
||
abstract convertToAirPurifier(sensorValue: CharacteristicValue): number | undefined; | ||
|
||
abstract handleSetOn(value: CharacteristicValue, callback: CharacteristicSetCallback): void; | ||
} | ||
|
||
class CurrentAirPurifierStateProperty extends PassthroughAirPurifierProperty { | ||
private static readonly NAME = 'fan_state'; | ||
|
||
static canUseExposesEntry(entry: ExposesEntry): boolean { | ||
return exposesHasNumericProperty(entry) && entry.name === CurrentAirPurifierStateProperty.NAME; | ||
} | ||
|
||
constructor(expose: ExposesEntryWithProperty, accessory: BasicAccessory, service: Service) { | ||
super(expose, accessory, service, hap.Characteristic.CurrentAirPurifierState); | ||
} | ||
|
||
convertToAirPurifier(sensorValue: CharacteristicValue): number | undefined { | ||
if (sensorValue === 'ON') { | ||
return hap.Characteristic.CurrentAirPurifierState.PURIFYING_AIR; | ||
} | ||
if (sensorValue === 'OFF') { | ||
return hap.Characteristic.CurrentAirPurifierState.IDLE; | ||
} | ||
|
||
return hap.Characteristic.CurrentAirPurifierState.INACTIVE; | ||
} | ||
|
||
handleSetOn(value: CharacteristicValue, callback: CharacteristicSetCallback): void { | ||
const data = {}; | ||
data['fan_state'] = (value as boolean) ? 'ON' : 'OFF'; | ||
this.accessory.queueDataForSetAction(data); | ||
callback(null); | ||
} | ||
} | ||
|
||
class TargetAirPurifierStateProperty extends PassthroughAirPurifierProperty { | ||
private static readonly NAME = 'fan_mode'; | ||
|
||
static canUseExposesEntry(entry: ExposesEntry): boolean { | ||
return exposesHasNumericProperty(entry) && entry.name === TargetAirPurifierStateProperty.NAME; | ||
} | ||
Avamander marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
constructor(expose: ExposesEntryWithProperty, accessory: BasicAccessory, service: Service) { | ||
super(expose, accessory, service, hap.Characteristic.TargetAirPurifierState); | ||
} | ||
|
||
convertToAirPurifier(sensorValue: CharacteristicValue): number | undefined { | ||
if (sensorValue === 'auto') { | ||
return hap.Characteristic.TargetAirPurifierState.AUTO; | ||
} | ||
|
||
return hap.Characteristic.TargetAirPurifierState.MANUAL; | ||
} | ||
Avamander marked this conversation as resolved.
Show resolved
Hide resolved
Avamander marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
handleSetOn(value: CharacteristicValue, callback: CharacteristicSetCallback): void { | ||
const data = {}; | ||
data['fan_mode'] = (value as boolean) ? 'auto' : 'off'; | ||
this.accessory.queueDataForSetAction(data); | ||
callback(null); | ||
} | ||
} | ||
|
||
class RotationSpeedProperty extends PassthroughAirPurifierProperty { | ||
private static readonly NAME = 'fan_speed'; | ||
|
||
static canUseExposesEntry(entry: ExposesEntry): boolean { | ||
return exposesHasNumericProperty(entry) && entry.name === RotationSpeedProperty.NAME; | ||
} | ||
|
||
constructor(expose: ExposesEntryWithProperty, accessory: BasicAccessory, service: Service) { | ||
super(expose, accessory, service, hap.Characteristic.RotationSpeed); | ||
} | ||
|
||
convertToAirPurifier(sensorValue: CharacteristicValue): number | undefined { | ||
if (typeof sensorValue !== 'number') { | ||
return 0; | ||
} | ||
|
||
return Math.ceil(sensorValue * 11.11); | ||
} | ||
|
||
handleSetOn(value: CharacteristicValue, callback: CharacteristicSetCallback): void { | ||
const data = {}; | ||
const speed = Math.floor((value as number) / 11.11); | ||
if (speed > 0) { | ||
data['fan_mode'] = speed; | ||
} else { | ||
data['fan_mode'] = 'off'; | ||
} | ||
this.accessory.queueDataForSetAction(data); | ||
callback(null); | ||
} | ||
} | ||
|
||
class LockPhysicalControlsProperty extends PassthroughAirPurifierProperty { | ||
private static readonly NAME = 'child_lock'; | ||
|
||
static canUseExposesEntry(entry: ExposesEntry): boolean { | ||
return exposesHasNumericProperty(entry) && entry.name === LockPhysicalControlsProperty.NAME; | ||
} | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
constructor(expose: ExposesEntryWithProperty, accessory: BasicAccessory, service: Service) { | ||
super(expose, accessory, service, hap.Characteristic.LockPhysicalControls); | ||
} | ||
|
||
convertToAirPurifier(sensorValue: CharacteristicValue): number | undefined { | ||
if (typeof sensorValue === 'undefined' || sensorValue === null) { | ||
return hap.Characteristic.LockPhysicalControls.CONTROL_LOCK_DISABLED; | ||
} | ||
|
||
return hap.Characteristic.LockPhysicalControls.CONTROL_LOCK_ENABLED; | ||
} | ||
|
||
handleSetOn(value: CharacteristicValue, callback: CharacteristicSetCallback): void { | ||
const data = {}; | ||
data['child_lock'] = (value as boolean) ? 'LOCK' : 'UNLOCK'; | ||
this.accessory.queueDataForSetAction(data); | ||
callback(null); | ||
} | ||
} | ||
|
||
class AirPurifierHandler implements ServiceHandler { | ||
public static readonly propertyFactories: WithExposesValidator< | ||
new (expose: ExposesEntryWithProperty, accessory: BasicAccessory, service: Service) => AirPurifierProperty | ||
>[] = [CurrentAirPurifierStateProperty, TargetAirPurifierStateProperty, RotationSpeedProperty, LockPhysicalControlsProperty]; | ||
|
||
private readonly properties: AirPurifierProperty[] = []; | ||
private readonly service: Service; | ||
|
||
public mainCharacteristics: Characteristic[] = []; | ||
|
||
constructor( | ||
endpoint: string | undefined, | ||
exposes: ExposesEntryWithProperty[], | ||
private readonly accessory: BasicAccessory | ||
) { | ||
this.identifier = AirPurifierHandler.generateIdentifier(endpoint); | ||
|
||
const serviceName = accessory.getDefaultServiceDisplayName(endpoint); | ||
accessory.log.debug(`Configuring Air Purifier for ${serviceName}`); | ||
this.service = accessory.getOrAddService(new hap.Service.AirPurifier(serviceName, endpoint)); | ||
this.mainCharacteristics.push(getOrAddCharacteristic(this.service, hap.Characteristic.CurrentAirPurifierState)); | ||
this.mainCharacteristics.push(getOrAddCharacteristic(this.service, hap.Characteristic.TargetAirPurifierState)); | ||
|
||
for (const e of exposes) { | ||
const factory = AirPurifierHandler.propertyFactories.find((f) => f.canUseExposesEntry(e)); | ||
if (factory === undefined) { | ||
accessory.log.warn(`Air Purifier does not know how to handle ${e.property} (on ${serviceName})`); | ||
continue; | ||
} | ||
this.properties.push(new factory(e, accessory, this.service)); | ||
} | ||
|
||
if (this.properties.length === 0) { | ||
throw new Error(`Air Purifier (${serviceName}) did not receive any suitable exposes entries.`); | ||
} | ||
} | ||
|
||
identifier: string; | ||
get getableKeys(): string[] { | ||
const keys: string[] = []; | ||
for (const property of this.properties) { | ||
if (exposesCanBeGet(property.expose)) { | ||
keys.push(property.expose.property); | ||
} | ||
} | ||
return keys; | ||
} | ||
|
||
updateState(state: Record<string, unknown>): void { | ||
for (const p of this.properties) { | ||
p.updateState(state); | ||
} | ||
} | ||
|
||
static generateIdentifier(endpoint: string | undefined) { | ||
let identifier = hap.Service.AirPurifier.UUID; | ||
if (endpoint !== undefined) { | ||
identifier += '_' + endpoint.trim(); | ||
} | ||
return identifier; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.