Skip to content

Commit

Permalink
new models
Browse files Browse the repository at this point in the history
  • Loading branch information
bwp91 committed Jan 18, 2025
1 parent 8469fee commit a707963
Show file tree
Hide file tree
Showing 39 changed files with 888 additions and 359 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ This project tries to adhere to [Semantic Versioning](http://semver.org/). In pr

## BETA

### Added

- new models

### Changed

- simplify BLE connections and updates
Expand Down
1 change: 1 addition & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default antfu(
],
order: 'asc',
type: 'natural',
newlinesBetween: 'always',
},
],
'perfectionist/sort-named-exports': 'error',
Expand Down
4 changes: 2 additions & 2 deletions lib/device/cooler-single.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class {

// Add the heater service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.HeaterCooler)
|| this.accessory.addService(this.hapServ.HeaterCooler)
|| this.accessory.addService(this.hapServ.HeaterCooler)

// Set custom properties of the current temperature characteristic
this.service.getCharacteristic(this.hapChar.CurrentTemperature).setProps({
Expand Down Expand Up @@ -67,7 +67,7 @@ export default class {
// Initialise these caches now since they aren't determined by the initial externalUpdate()
this.cacheState = this.service.getCharacteristic(this.hapChar.Active).value === 1 ? 'on' : 'off'
this.cacheCool = this.cacheState === 'on'
&& this.service.getCharacteristic(this.hapChar.CurrentHeaterCoolerState).value === 3
&& this.service.getCharacteristic(this.hapChar.CurrentHeaterCoolerState).value === 3
? 'on'
: 'off'

Expand Down
2 changes: 1 addition & 1 deletion lib/device/diffuser-H7161.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class {

// Add the purifier service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.AirPurifier)
|| this.accessory.addService(this.hapServ.AirPurifier)
|| this.accessory.addService(this.hapServ.AirPurifier)

// Add the set handler to the switch on/off characteristic
this.service.getCharacteristic(this.hapChar.Active).onSet(async (value) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/device/diffuser-H7162.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class {

// Add the purifier service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.AirPurifier)
|| this.accessory.addService(this.hapServ.AirPurifier)
|| this.accessory.addService(this.hapServ.AirPurifier)

// Add the set handler to the switch on/off characteristic
this.service.getCharacteristic(this.hapChar.Active).onSet(async (value) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/device/heater-single.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class {

// Add the heater service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.HeaterCooler)
|| this.accessory.addService(this.hapServ.HeaterCooler)
|| this.accessory.addService(this.hapServ.HeaterCooler)

// Set custom properties of the current temperature characteristic
this.service.getCharacteristic(this.hapChar.CurrentTemperature).setProps({
Expand Down Expand Up @@ -67,7 +67,7 @@ export default class {
// Initialise these caches now since they aren't determined by the initial externalUpdate()
this.cacheState = this.service.getCharacteristic(this.hapChar.Active).value === 1 ? 'on' : 'off'
this.cacheHeat = this.cacheState === 'on'
&& this.service.getCharacteristic(this.hapChar.TargetHeaterCoolerState).value === 2
&& this.service.getCharacteristic(this.hapChar.TargetHeaterCoolerState).value === 2
? 'on'
: 'off'

Expand Down
2 changes: 1 addition & 1 deletion lib/device/heater1b.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ export default class {

// Add the fan service if it doesn't already exist
this.fanService = this.accessory.getService(this.hapServ.Fan)
|| this.accessory.addService(this.hapServ.Fan)
|| this.accessory.addService(this.hapServ.Fan)

// Add the set handler to the heater active characteristic
this.service
Expand Down
4 changes: 2 additions & 2 deletions lib/device/heater2.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ export default class {

// Add the fan service if it doesn't already exist
this.fanService = this.accessory.getService(this.hapServ.Fan)
|| this.accessory.addService(this.hapServ.Fan)
|| this.accessory.addService(this.hapServ.Fan)

// Add the night light service if it doesn't already exist
this.lightService = this.accessory.getService(this.hapServ.Lightbulb)
|| this.accessory.addService(this.hapServ.Lightbulb)
|| this.accessory.addService(this.hapServ.Lightbulb)

// Add the set handler to the heater active characteristic
this.service
Expand Down
2 changes: 1 addition & 1 deletion lib/device/humidifier-H7140.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class {

// Add the night light service if it doesn't already exist
this.lightService = this.accessory.getService(this.hapServ.Lightbulb)
|| this.accessory.addService(this.hapServ.Lightbulb)
|| this.accessory.addService(this.hapServ.Lightbulb)

// Add the set handler to the fan on/off characteristic
this.service
Expand Down
4 changes: 2 additions & 2 deletions lib/device/humidifier-H7142.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ export default class {

// Add humidity sensor service if it doesn't already exist
this.humiService = this.accessory.getService(this.hapServ.HumiditySensor)
|| this.accessory.addService(this.hapServ.HumiditySensor)
|| this.accessory.addService(this.hapServ.HumiditySensor)

// Add the night light service if it doesn't already exist
this.lightService = this.accessory.getService(this.hapServ.Lightbulb)
|| this.accessory.addService(this.hapServ.Lightbulb)
|| this.accessory.addService(this.hapServ.Lightbulb)

this.cacheHumi = this.humiService.getCharacteristic(this.hapChar.CurrentRelativeHumidity).value

Expand Down
157 changes: 157 additions & 0 deletions lib/device/humidifier-H714E.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import {
base64ToHex,
getTwoItemPosition,
hexToTwoItems,
parseError,
} from '../utils/functions.js'
import platformLang from '../utils/lang-en.js'

export default class {
constructor(platform, accessory) {
// Set up variables from the platform
this.hapChar = platform.api.hap.Characteristic
this.hapErr = platform.api.hap.HapStatusError
this.hapServ = platform.api.hap.Service
this.platform = platform

// Set up variables from the accessory
this.accessory = accessory

// Rotation speed to value in {1, 2, ..., 8}
this.speed2Value = speed => Math.min(Math.max(Number.parseInt(Math.round(speed / 10), 10), 1), 8)

// Speed codes
this.value2Code = {
1: 'MwUBAQAAAAAAAAAAAAAAAAAAADY=',
2: 'MwUBAgAAAAAAAAAAAAAAAAAAADU=',
3: 'MwUBAwAAAAAAAAAAAAAAAAAAADQ=',
4: 'MwUBBAAAAAAAAAAAAAAAAAAAADM=',
5: 'MwUBBQAAAAAAAAAAAAAAAAAAADI=',
6: 'MwUBBgAAAAAAAAAAAAAAAAAAADE=',
7: 'MwUBBwAAAAAAAAAAAAAAAAAAADA=',
8: 'MwUBCAAAAAAAAAAAAAAAAAAAAD8=',
}

// Add the fan service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.Fan) || this.accessory.addService(this.hapServ.Fan)

// Add the set handler to the fan on/off characteristic
this.service
.getCharacteristic(this.hapChar.On)
.onSet(async value => this.internalStateUpdate(value))
this.cacheState = this.service.getCharacteristic(this.hapChar.On).value ? 'on' : 'off'

// Add the set handler to the fan rotation speed characteristic
this.service
.getCharacteristic(this.hapChar.RotationSpeed)
.setProps({
minStep: 10,
validValues: [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100],
})
.onSet(async value => this.internalSpeedUpdate(value))
this.cacheSpeed = this.service.getCharacteristic(this.hapChar.RotationSpeed).value

// Output the customised options to the log
const opts = JSON.stringify({})
platform.log('[%s] %s %s.', accessory.displayName, platformLang.devInitOpts, opts)
}

async internalStateUpdate(value) {
try {
const newValue = value ? 'on' : 'off'

// Don't continue if the new value is the same as before
if (this.cacheState === newValue) {
return
}

// Send the request to the platform sender function
await this.platform.sendDeviceUpdate(this.accessory, {
cmd: 'stateHumi',
value: value ? 1 : 0,
})

// Cache the new state and log if appropriate
this.cacheState = newValue
this.accessory.log(`${platformLang.curState} [${this.cacheState}]`)
} catch (err) {
// Catch any errors during the process
this.accessory.logWarn(`${platformLang.devNotUpdated} ${parseError(err)}`)

// Throw a 'no response' error and set a timeout to revert this after 2 seconds
setTimeout(() => {
this.service.updateCharacteristic(this.hapChar.On, this.cacheState === 'on')
}, 2000)
throw new this.hapErr(-70402)
}
}

async internalSpeedUpdate(value) {
try {
// Don't continue if the speed is 0
if (value === 0) {
return
}

// Get the single Govee value {1, 2, ..., 8}
const newValue = this.speed2Value(value)

// Don't continue if the speed value won't have effect
if (newValue * 10 === this.cacheSpeed) {
return
}

// Get the scene code for this value
const newCode = this.value2Code[newValue]

// Send the request to the platform sender function
await this.platform.sendDeviceUpdate(this.accessory, {
cmd: 'ptReal',
value: newCode,
})

// Cache the new state and log if appropriate
this.cacheSpeed = newValue * 10
this.accessory.log(`${platformLang.curSpeed} [${newValue}]`)
} catch (err) {
// Catch any errors during the process
this.accessory.logWarn(`${platformLang.devNotUpdated} ${parseError(err)}`)

// Throw a 'no response' error and set a timeout to revert this after 2 seconds
setTimeout(() => {
this.service.updateCharacteristic(this.hapChar.RotationSpeed, this.cacheSpeed)
}, 2000)
throw new this.hapErr(-70402)
}
}

externalUpdate(params) {
// Check for an ON/OFF change
if (params.state && params.state !== this.cacheState) {
this.cacheState = params.state
this.service.updateCharacteristic(this.hapChar.On, this.cacheState === 'on')

// Log the change
this.accessory.log(`${platformLang.curState} [${this.cacheState}]`)
}

// Check for some other scene/mode change
(params.commands || []).forEach((command) => {
const hexString = base64ToHex(command)
const hexParts = hexToTwoItems(hexString)

// Return now if not a device query update code
if (getTwoItemPosition(hexParts, 1) !== 'aa') {
return
}

const deviceFunction = `${getTwoItemPosition(hexParts, 1)}${getTwoItemPosition(hexParts, 2)}`

switch (deviceFunction) {
default:
this.accessory.logDebugWarn(`${platformLang.newScene}: [${command}] [${hexString}]`)
break
}
})
}
}
2 changes: 1 addition & 1 deletion lib/device/humidifier-H7160.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class {

// Add the night light service if it doesn't already exist
this.lightService = this.accessory.getService(this.hapServ.Lightbulb)
|| this.accessory.addService(this.hapServ.Lightbulb)
|| this.accessory.addService(this.hapServ.Lightbulb)

// Add the set handler to the fan on/off characteristic
this.service
Expand Down
4 changes: 4 additions & 0 deletions lib/device/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import deviceHeater1A from './heater1a.js'
import deviceHeater1B from './heater1b.js'
import deviceHeater2 from './heater2.js'
import deviceHeaterSingle from './heater-single.js'
import deviceHumidifierH714E from './humidifier-H714E.js'
import deviceHumidifierH7140 from './humidifier-H7140.js'
import deviceHumidifierH7141 from './humidifier-H7141.js'
import deviceHumidifierH7142 from './humidifier-H7142.js'
Expand All @@ -35,6 +36,7 @@ import devicePurifierH7123 from './purifier-H7123.js'
import devicePurifierH7124 from './purifier-H7124.js'
import devicePurifierH7126 from './purifier-H7126.js'
import devicePurifierH7127 from './purifier-H7127.js'
import devicePurifierH7128 from './purifier-H7128.js'
import devicePurifierH7129 from './purifier-H7129.js'
import devicePurifierSingle from './purifier-single.js'
import deviceSensorButton from './sensor-button.js'
Expand Down Expand Up @@ -68,6 +70,7 @@ export default {
deviceHeater1A,
deviceHeater1B,
deviceHeater2,
deviceHumidifierH714E,
deviceHumidifierH7140,
deviceHumidifierH7141,
deviceHumidifierH7142,
Expand All @@ -90,6 +93,7 @@ export default {
devicePurifierH7124,
devicePurifierH7126,
devicePurifierH7127,
devicePurifierH7128,
devicePurifierH7129,
devicePurifierSingle,
deviceSensorButton,
Expand Down
2 changes: 1 addition & 1 deletion lib/device/light-switch.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default class {

// Add the main switch service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.Switch)
|| this.accessory.addService(this.hapServ.Switch)
|| this.accessory.addService(this.hapServ.Switch)

// Add the set handler to the lightbulb on/off characteristic
this.service.getCharacteristic(this.hapChar.On).onSet(async (value) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/device/light.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default class {

// Add the main lightbulb service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.Lightbulb)
|| this.accessory.addService(this.hapServ.Lightbulb)
|| this.accessory.addService(this.hapServ.Lightbulb)

// If adaptive lighting has just been disabled then remove and re-add service to hide AL icon
if ((this.colourSafeMode || this.alShift === -1) && this.accessory.context.adaptiveLighting) {
Expand Down
4 changes: 2 additions & 2 deletions lib/device/outlet-double.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ export default class {

// Add the outlet services if they don't already exist
this.service1 = this.accessory.getService('Outlet 1')
|| this.accessory.addService(this.hapServ.Outlet, 'Outlet 1', 'outlet1')
|| this.accessory.addService(this.hapServ.Outlet, 'Outlet 1', 'outlet1')
this.service2 = this.accessory.getService('Outlet 2')
|| this.accessory.addService(this.hapServ.Outlet, 'Outlet 2', 'outlet2')
|| this.accessory.addService(this.hapServ.Outlet, 'Outlet 2', 'outlet2')

if (!this.service1.testCharacteristic(this.hapChar.ConfiguredName)) {
this.service1.addCharacteristic(this.hapChar.ConfiguredName)
Expand Down
2 changes: 1 addition & 1 deletion lib/device/outlet-single.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class {

// Add the outlet service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.Outlet)
|| this.accessory.addService(this.hapServ.Outlet)
|| this.accessory.addService(this.hapServ.Outlet)

// Add the set handler to the switch on/off characteristic
this.service.getCharacteristic(this.hapChar.On).onSet(async (value) => {
Expand Down
6 changes: 3 additions & 3 deletions lib/device/outlet-triple.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export default class {

// Add the outlet services if they don't already exist
this.service1 = this.accessory.getService('Outlet 1')
|| this.accessory.addService(this.hapServ.Outlet, 'Outlet 1', 'outlet1')
|| this.accessory.addService(this.hapServ.Outlet, 'Outlet 1', 'outlet1')
this.service2 = this.accessory.getService('Outlet 2')
|| this.accessory.addService(this.hapServ.Outlet, 'Outlet 2', 'outlet2')
|| this.accessory.addService(this.hapServ.Outlet, 'Outlet 2', 'outlet2')
this.service3 = this.accessory.getService('Outlet 3')
|| this.accessory.addService(this.hapServ.Outlet, 'Outlet 3', 'outlet3')
|| this.accessory.addService(this.hapServ.Outlet, 'Outlet 3', 'outlet3')

// Add the set handler to the switch on/off characteristic
this.service1.getCharacteristic(this.hapChar.On).onSet(async (value) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/device/purifier-H7120.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class {

// Add the purifier service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.AirPurifier)
|| this.accessory.addService(this.hapServ.AirPurifier)
|| this.accessory.addService(this.hapServ.AirPurifier)

// Add the set handler to the switch on/off characteristic
this.service.getCharacteristic(this.hapChar.Active).onSet(async (value) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/device/purifier-H7121.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class {

// Add the purifier service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.AirPurifier)
|| this.accessory.addService(this.hapServ.AirPurifier)
|| this.accessory.addService(this.hapServ.AirPurifier)

// Add the set handler to the switch on/off characteristic
this.service.getCharacteristic(this.hapChar.Active).onSet(async (value) => {
Expand Down
2 changes: 1 addition & 1 deletion lib/device/purifier-H7122.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class {

// Add the purifier service if it doesn't already exist
this.service = this.accessory.getService(this.hapServ.AirPurifier)
|| this.accessory.addService(this.hapServ.AirPurifier)
|| this.accessory.addService(this.hapServ.AirPurifier)

// Add the air quality service if it doesn't already exist
this.airService = this.accessory.getService(this.hapServ.AirQualitySensor)
Expand Down
Loading

0 comments on commit a707963

Please sign in to comment.