-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for fans and air fresheners (#91)
- Loading branch information
Showing
7 changed files
with
128 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,71 @@ | ||
import * as A from 'fp-ts/Array'; | ||
import * as O from 'fp-ts/Option'; | ||
import * as TE from 'fp-ts/TaskEither'; | ||
import { flow, identity, pipe } from 'fp-ts/lib/function'; | ||
import { CharacteristicValue, Service } from 'homebridge'; | ||
import { SupportedActionsType, SupportedNamespacesType } from '../domain/alexa'; | ||
import * as mapper from '../mapper/power-mapper'; | ||
import BaseAccessory from './base-accessory'; | ||
import { FanNamespaces, FanState } from '../domain/alexa/fan'; | ||
|
||
export default class FanAccessory extends BaseAccessory { | ||
static requiredOperations: SupportedActionsType[] = ['turnOn', 'turnOff']; | ||
service: Service; | ||
namespaces = FanNamespaces; | ||
isExternalAccessory = false; | ||
|
||
configureServices() { | ||
this.service = | ||
this.platformAcc.getService(this.Service.Fanv2) || | ||
this.platformAcc.addService(this.Service.Fanv2, this.device.displayName); | ||
|
||
this.service | ||
.getCharacteristic(this.Characteristic.Active) | ||
.onGet(this.handlePowerGet.bind(this)) | ||
.onSet(this.handlePowerSet.bind(this)); | ||
} | ||
|
||
async handlePowerGet(): Promise<boolean> { | ||
const alexaNamespace: SupportedNamespacesType = 'Alexa.PowerController'; | ||
const determinePowerState = flow( | ||
O.filterMap<FanState[], FanState>( | ||
A.findFirst(({ namespace }) => namespace === alexaNamespace), | ||
), | ||
O.map(({ value }) => value === 'ON'), | ||
O.tap((s) => | ||
O.of(this.logWithContext('debug', `Get power result: ${s}`)), | ||
), | ||
); | ||
|
||
return pipe( | ||
this.getState(determinePowerState), | ||
TE.match((e) => { | ||
this.logWithContext('errorT', 'Get power', e); | ||
throw this.serviceCommunicationError; | ||
}, identity), | ||
)(); | ||
} | ||
|
||
async handlePowerSet(value: CharacteristicValue): Promise<void> { | ||
this.logWithContext('debug', `Triggered set power: ${value}`); | ||
if (typeof value !== 'boolean') { | ||
throw this.invalidValueError; | ||
} | ||
const action = mapper.mapHomeKitPowerToAlexaAction(value); | ||
return pipe( | ||
this.platform.alexaApi.setDeviceState(this.device.id, action), | ||
TE.match( | ||
(e) => { | ||
this.logWithContext('errorT', 'Set power', e); | ||
throw this.serviceCommunicationError; | ||
}, | ||
() => { | ||
this.updateCacheValue({ | ||
value: mapper.mapHomeKitPowerToAlexaValue(value), | ||
namespace: 'Alexa.PowerController', | ||
}); | ||
}, | ||
), | ||
)(); | ||
} | ||
} |
This file contains 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,11 @@ | ||
import { CapabilityState, SupportedNamespaces } from './index'; | ||
|
||
export interface FanState { | ||
namespace: keyof typeof FanNamespaces & | ||
keyof typeof SupportedNamespaces; | ||
value: CapabilityState['value']; | ||
} | ||
|
||
export const FanNamespaces = { | ||
'Alexa.PowerController': 'Alexa.PowerController', | ||
} as const; |
This file contains 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
This file contains 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