Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
feat: add filter life & condition (thanks @jeffmaki!)
Browse files Browse the repository at this point in the history
part of #40
  • Loading branch information
sman591 committed Apr 20, 2021
1 parent 1490502 commit 6c94d0f
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/characteristic/filterChangeCharacteristic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { Service, Characteristic } from 'homebridge'
import type { GetDeviceResponse } from '../thinq/apiTypes'

import { HomebridgeLgThinqPlatform } from '../platform'
import AbstractCharacteristic from './abstractCharacteristic'

type State = 0 | 1

type ApiValue = boolean

export default class FilterChangeCharacteristic extends AbstractCharacteristic<
State,
// @ts-expect-error This characteristic is a hack
ApiValue,
typeof Characteristic.FilterChangeIndication
> {
constructor(
platform: HomebridgeLgThinqPlatform,
service: Service,
deviceId: string,
) {
super(
platform,
service,
deviceId,
platform.Characteristic.FilterChangeIndication,
'Operation',
// @ts-expect-error This characteristic is a hack
'_fake_filter_change_value',
)
}

// Override default handleUpdatedSnapshot() to ignore based on mode
handleUpdatedSnapshot(snapshot: GetDeviceResponse['result']['snapshot']) {
const maxTime = snapshot['airState.filterMngStates.maxTime']
const useTime = snapshot['airState.filterMngStates.useTime']
const shouldFilterBeChanged = useTime >= maxTime

super.handleUpdatedSnapshot({
...snapshot,
// @ts-expect-error This characteristic is a hack
_fake_filter_change_value: shouldFilterBeChanged,
})
}

getStateFromApiValue(apiValue: ApiValue): State {
return apiValue
? this.characteristic.CHANGE_FILTER
: this.characteristic.FILTER_OK
}

handleSet = undefined

getApiValueFromState(state: State): ApiValue {
if (state === this.characteristic.CHANGE_FILTER) {
return true
}
return false
}
}
55 changes: 55 additions & 0 deletions src/characteristic/filterLifeCharacteristic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import type { Service, Characteristic } from 'homebridge'
import type { GetDeviceResponse } from '../thinq/apiTypes'

import { HomebridgeLgThinqPlatform } from '../platform'
import AbstractCharacteristic from './abstractCharacteristic'

type State = number

type ApiValue = number

export default class FilterLifeCharacteristic extends AbstractCharacteristic<
State,
ApiValue,
typeof Characteristic.FilterLifeLevel
> {
constructor(
platform: HomebridgeLgThinqPlatform,
service: Service,
deviceId: string,
) {
super(
platform,
service,
deviceId,
platform.Characteristic.FilterLifeLevel,
'Operation',
// @ts-expect-error This characteristic is a hack
'_fake_filter_life',
)
}

// Override default handleUpdatedSnapshot() to ignore based on mode
handleUpdatedSnapshot(snapshot: GetDeviceResponse['result']['snapshot']) {
const maxTime = snapshot['airState.filterMngStates.maxTime']
const useTime = snapshot['airState.filterMngStates.useTime']
const percentUsed = (useTime / maxTime) * 100
const filterLife = Math.floor(100 - percentUsed)

super.handleUpdatedSnapshot({
...snapshot,
// @ts-expect-error This characteristic is a hack
_fake_filter_life: filterLife,
})
}

getStateFromApiValue(apiValue: ApiValue): State {
return apiValue
}

handleSet = undefined

getApiValueFromState(state: State): ApiValue {
return state
}
}
8 changes: 8 additions & 0 deletions src/getCharacteristicsForModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import CoolingThresholdTemperatureCharacteristic from './characteristic/coolingT
import HeatingThresholdTemperatureCharacteristic from './characteristic/heatingThresholdTemperatureCharacteristic'
import TargetHeaterCoolerStateCharacteristic from './characteristic/targetHeaterCoolerStateCharacteristic'
import CurrentTemperatureCharacteristic from './characteristic/currentTemperatureCharacteristic'
import FilterChangeCharacteristic from './characteristic/filterChangeCharacteristic'
import FilterLifeCharacteristic from './characteristic/filterLifeCharacteristic'

export default function getCharacteristicsForModel(
model: string,
Expand Down Expand Up @@ -44,6 +46,8 @@ export default function getCharacteristicsForModel(
true,
),
new CurrentTemperatureCharacteristic(platform, service, deviceId),
new FilterChangeCharacteristic(platform, service, deviceId),
new FilterLifeCharacteristic(platform, service, deviceId),
]
// LW8017ERSM -- 3 fan modes
// LW1517IVSM -- 4 fan modes
Expand All @@ -63,6 +67,8 @@ export default function getCharacteristicsForModel(
),
new TargetHeaterCoolerStateCharacteristic(platform, service, deviceId),
new CurrentTemperatureCharacteristic(platform, service, deviceId),
new FilterChangeCharacteristic(platform, service, deviceId),
new FilterLifeCharacteristic(platform, service, deviceId),
]
// LP1419IVSM
case 'POT_056905_WW':
Expand All @@ -89,6 +95,8 @@ export default function getCharacteristicsForModel(
),
new TargetHeaterCoolerStateCharacteristic(platform, service, deviceId),
new CurrentTemperatureCharacteristic(platform, service, deviceId),
new FilterChangeCharacteristic(platform, service, deviceId),
new FilterLifeCharacteristic(platform, service, deviceId),
]
}
}

0 comments on commit 6c94d0f

Please sign in to comment.