Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeChSien committed Aug 26, 2024
1 parent 508b483 commit 2711cc1
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 59 deletions.
9 changes: 3 additions & 6 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import eslintConfigPrettier from "eslint-config-prettier";

export default [tseslint.config(
export default tseslint.config(
{
ignores: ['dist/**'],
ignores: ['dist/**', 'node_modules/**'],
},
{
rules: {
Expand Down Expand Up @@ -33,6 +32,4 @@ export default [tseslint.config(
},
eslint.configs.recommended,
...tseslint.configs.recommended,
),
eslintConfigPrettier
]
)
47 changes: 24 additions & 23 deletions src/MEAircon.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Curl } from 'node-libcurl';
import { Curl } from 'node-libcurl'
import xml2js from 'xml2js'
import { DRIVE_MODE, MEAirconStates, POWER_ON_OFF } from './types.js'
import { encrypt, decrypt } from './utils/crypt.js'
import { isGeneralStatesPayload, parseGeneralStates } from './parsers/generalStates.js'
import { isSensorStatesPayload, parseSensorStates } from './parsers/sensorStates.js'
import { isErrorStatesPayload, parseErrorStates } from './parsers/errorStates.js'
import { extend08Command } from './commands/extend08.js';
import { generalCommand } from './commands/general.js';
import { extend08Command } from './commands/extend08.js'
import { generalCommand } from './commands/general.js'

interface MEAirconIdentify {
mac: string
Expand All @@ -16,7 +16,7 @@ interface MEAirconIdentify {
}

const REQUEST_TEMPLATE = {
CONNECT: 'ON'
CONNECT: 'ON',
}

const parseIdentity = async (response: string): Promise<MEAirconIdentify> => {
Expand All @@ -28,7 +28,7 @@ const parseIdentity = async (response: string): Promise<MEAirconIdentify> => {
mac: MAC[0],
serial: SERIAL[0],
rssi: RSSI[0],
appVersion: APP_VER[0]
appVersion: APP_VER[0],
} as MEAirconIdentify
}

Expand All @@ -42,21 +42,21 @@ const parseStates = async (response: string): Promise<MEAirconStates> => {
if (isGeneralStatesPayload(payload)) {
newStates = {
...newStates,
...parseGeneralStates(payload)
...parseGeneralStates(payload),
}
}

if (isSensorStatesPayload(payload)) {
newStates = {
...newStates,
...parseSensorStates(payload)
...parseSensorStates(payload),
}
}

if (isErrorStatesPayload(payload)) {
newStates = {
...newStates,
...parseErrorStates(payload)
...parseErrorStates(payload),
}
}
})
Expand Down Expand Up @@ -100,7 +100,7 @@ export default class MEAircon {
'Proxy-Connection: keep-alive',
'Accept: */*',
'User-Agent: KirigamineRemote/5.1.0 (jp.co.MitsubishiElectric.KirigamineRemote; build:3; iOS 17.5.1) Alamofire/5.9.1',
'Accept-Language: zh-Hant-JP;q=1.0, ja-JP;q=0.9'
'Accept-Language: zh-Hant-JP;q=1.0, ja-JP;q=0.9',
])
curl.on('end', (_statusCode: string, data: string) => {
resolve(data)
Expand All @@ -115,14 +115,15 @@ export default class MEAircon {
const response = await this.postRequest(REQUEST_TEMPLATE)
this.states = {
...this.states,
...await parseStates(response)
...await parseStates(response),
}
this.identity = {
...this.identity,
...await parseIdentity(response)
...await parseIdentity(response),
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
getAttribute(attrName: keyof MEAirconStates): any {
switch (attrName) {
case 'powerOnOff':
Expand All @@ -147,48 +148,48 @@ export default class MEAircon {
async setPowerOnOff(power: boolean) {
this.states = {
...this.states,
powerOnOff: power ? POWER_ON_OFF.ON : POWER_ON_OFF.OFF
powerOnOff: power ? POWER_ON_OFF.ON : POWER_ON_OFF.OFF,
}
await this.postRequest({
...REQUEST_TEMPLATE,
CODE: {
VALUE: [
getBuzzPayload(this.states),
generalCommand(this.states, { powerOnOff: true })
]
}
generalCommand(this.states, { powerOnOff: true }),
],
},
})
}

async setDriveMode(driveMode: DRIVE_MODE) {
this.states = {
...this.states,
driveMode
driveMode,
}
await this.postRequest({
...REQUEST_TEMPLATE,
CODE: {
VALUE: [
getBuzzPayload(this.states),
generalCommand(this.states, { driveMode: true })
]
}
generalCommand(this.states, { driveMode: true }),
],
},
})
}

async setTemerature(temperature: number) {
this.states = {
...this.states,
temperature: temperature * 10
temperature: temperature * 10,
}
await this.postRequest({
...REQUEST_TEMPLATE,
CODE: {
VALUE: [
getBuzzPayload(this.states),
generalCommand(this.states, { temperature: true })
]
}
generalCommand(this.states, { temperature: true }),
],
},
})
}
}
10 changes: 5 additions & 5 deletions src/MEAirconPlatform.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { API, Characteristic, DynamicPlatformPlugin, Logging, PlatformAccessory, PlatformConfig, Service } from 'homebridge';
import type { API, Characteristic, DynamicPlatformPlugin, Logging, PlatformAccessory, PlatformConfig, Service } from 'homebridge'

import { MEAirconPlatformAccessory } from './MEAirconPlatformAccessory.js';
import { MEAirconPlatformAccessory } from './MEAirconPlatformAccessory.js'

export class MEAirconPlatform implements DynamicPlatformPlugin {
public readonly Service: typeof Service;
public readonly Characteristic: typeof Characteristic;

public readonly accessories: PlatformAccessory[] = [];
public readonly accessories: PlatformAccessory[] = []

constructor(
public readonly log: Logging,
Expand All @@ -16,7 +16,7 @@ export class MEAirconPlatform implements DynamicPlatformPlugin {
this.Service = api.hap.Service;
this.Characteristic = api.hap.Characteristic;

this.log.debug('Finished initializing platform:', this.config.name);
this.log.debug('Finished initializing platform:', this.config.name)

this.api.on('didFinishLaunching', () => {
log.debug('Executed didFinishLaunching callback')
Expand All @@ -36,7 +36,7 @@ export class MEAirconPlatform implements DynamicPlatformPlugin {
try {
await airconAccessory.init()
} catch (e) {
this.log.error('Cannot init aircon:', aircon.name);
this.log.error('Cannot init aircon:', aircon.name)
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/extend08.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const EXAMPLE_STATES = {
windSpeed: WIND_SPEED.AUTO,
verticalWindDirection: {
right: VERTICAL_WIND_DIRECTION.AUTO,
left: VERTICAL_WIND_DIRECTION.AUTO
left: VERTICAL_WIND_DIRECTION.AUTO,
},
horizontalWindDirection: HORIZONTAL_WIND_DIRECTION.AUTO,
dehumSetting: 60,
isPowerSaving: false,
windAndWindBreakDirect: 2
windAndWindBreakDirect: 2,
}

describe('commands > general', () => {
Expand Down
11 changes: 6 additions & 5 deletions src/commands/extend08.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import { MEAirconStates } from '../types.js'
import { calcFCC } from '../utils/calcFCC.js'

Expand All @@ -15,15 +16,15 @@ export const extend08Command = (states: Pick<MEAirconStates, 'dehumSetting' | 'i
controls.buzzer && (segmentXValue |= parseInt('10', 16))
controls.windAndWindBreak && (segmentXValue |= parseInt('20', 16))

let segmentX = ('00' + segmentXValue.toString(16)).slice(-2)
const segmentX = ('00' + segmentXValue.toString(16)).slice(-2)
let segmentY = '00'
controls.dehum && (segmentY = ('00' + states.dehumSetting.toString(16)).slice(-2))
let segmentZ = states.isPowerSaving ? '0A' : '00' // be aware of it, we removed some checking logic for power saving

const segmentZ = states.isPowerSaving ? '0A' : '00' // be aware of it, we removed some checking logic for power saving

let segmentA = '00';
controls.windAndWindBreak && (segmentA = ('00' + states.windAndWindBreakDirect).slice(-2))
let payload = "4101301008" + segmentX + "0000" + segmentY + segmentZ + segmentA + (controls.buzzer ? "01" : "00") + "0000000000000000";

const payload = "4101301008" + segmentX + "0000" + segmentY + segmentZ + segmentA + (controls.buzzer ? "01" : "00") + "0000000000000000";
return 'fc' + payload + calcFCC(payload)
}
4 changes: 2 additions & 2 deletions src/commands/general.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ const EXAMPLE_STATES = {
windSpeed: WIND_SPEED.AUTO,
verticalWindDirection: {
right: VERTICAL_WIND_DIRECTION.AUTO,
left: VERTICAL_WIND_DIRECTION.AUTO
left: VERTICAL_WIND_DIRECTION.AUTO,
},
horizontalWindDirection: HORIZONTAL_WIND_DIRECTION.AUTO,
dehumSetting: 60,
isPowerSaving: false,
windAndWindBreakDirect: 2
windAndWindBreakDirect: 2,
}

describe('commands > general', () => {
Expand Down
3 changes: 2 additions & 1 deletion src/commands/general.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-unused-expressions */
import { GeneralStates, GeneralControls } from '../types.js'
import { calcFCC } from '../utils/calcFCC.js'

Expand Down Expand Up @@ -35,7 +36,7 @@ export const generalCommand = (states: Omit<GeneralStates, 'dehumSetting' | 'isP
segment7: '00',
segment13: '00',
segment14: '00',
segment15: '00'
segment15: '00',
}

let segment1Value = 0
Expand Down
8 changes: 4 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import type { API } from 'homebridge';
import type { API } from 'homebridge'

import { MEAirconPlatform } from './MEAirconPlatform.js';
import { PLATFORM_NAME } from './settings.js';
import { MEAirconPlatform } from './MEAirconPlatform.js'
import { PLATFORM_NAME } from './settings.js'

/**
* This method registers the platform with Homebridge
*/
export default (api: API) => {
api.registerPlatform(PLATFORM_NAME, MEAirconPlatform);
api.registerPlatform(PLATFORM_NAME, MEAirconPlatform)
};
2 changes: 1 addition & 1 deletion src/parsers/errorStates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('parsers > errorStates', () => {
it('parseErrorStates', () => {
expect(parseErrorStates(EXAMPLE_PAYLOAD)).toStrictEqual({
isAbnormalState: false,
errorCode: '8000'
errorCode: '8000',
})
})
})
2 changes: 1 addition & 1 deletion src/parsers/errorStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export const parseErrorStates = (payload: string): ErrorStates => {

return {
isAbnormalState,
errorCode
errorCode,
}
}
4 changes: 2 additions & 2 deletions src/parsers/generalStates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ describe('parsers > generalStates', () => {
windSpeed: WIND_SPEED.AUTO,
verticalWindDirection: {
right: VERTICAL_WIND_DIRECTION.AUTO,
left: VERTICAL_WIND_DIRECTION.AUTO
left: VERTICAL_WIND_DIRECTION.AUTO,
},
horizontalWindDirection: HORIZONTAL_WIND_DIRECTION.AUTO,
dehumSetting: 60,
isPowerSaving: false,
windAndWindBreakDirect: 2
windAndWindBreakDirect: 2,
})
})
})
4 changes: 2 additions & 2 deletions src/parsers/generalStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ export const parseGeneralStates = (payload: string): GeneralStates => {
windSpeed,
verticalWindDirection: {
right: rightVerticalWindDirection,
left: leftVerticaWindDirection
left: leftVerticaWindDirection,
},
horizontalWindDirection,
dehumSetting,
isPowerSaving,
windAndWindBreakDirect
windAndWindBreakDirect,
}
}
2 changes: 1 addition & 1 deletion src/parsers/sensorStates.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('parsers > sensorStates', () => {
outsideTemperature: 330,
roomTemperature: 245,
thermalSensor: false,
windSpeedPr557: 0
windSpeedPr557: 0,
})
})
})
2 changes: 1 addition & 1 deletion src/parsers/sensorStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ export const parseSensorStates = (payload: string): SensorStates => {
outsideTemperature,
roomTemperature,
thermalSensor,
windSpeedPr557
windSpeedPr557,
}
}
6 changes: 5 additions & 1 deletion src/utils/calcFCC.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/* eslint-disable no-var */
/* eslint-disable @typescript-eslint/no-unused-expressions */
export const calcFCC = (t: string) => {
for (var e, n, i = 0, o = 0; o < 20; o++) e = t.substring(2 * o, (2 * o) + 2), i += parseInt(e, 16);
for (var e, n, i = 0, o = 0; o < 20; o++) {
e = t.substring(2 * o, (2 * o) + 2), i += parseInt(e, 16);
}
return 1 === (n = (i = 256 - (i %= 256)).toString(16)).length && (n = "0" + n), n.slice(-2)
}
4 changes: 2 additions & 2 deletions src/utils/crypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const encrypt = (payload: string) => {
mode: CryptoJS.mode.CBC,
iv: randomWord,
padding: CryptoJS.pad.ZeroPadding,
keySize: KEY_SIZE
keySize: KEY_SIZE,
}).toString()
encrypted = Hex.stringify(randomWord) + Hex.stringify(Base64.parse(encrypted));
return Base64.stringify(Hex.parse(encrypted))
Expand All @@ -34,7 +34,7 @@ export const decrypt = (payload: string) => {
mode: CryptoJS.mode.CBC,
iv,
padding: CryptoJS.pad.ZeroPadding,
keySize: KEY_SIZE
keySize: KEY_SIZE,
}).toString(Hex)
decrypted = decrypted.slice(2 * KEY_SIZE, decrypted.length)
return Buffer.from(decrypted, 'hex').toString('utf8')
Expand Down

0 comments on commit 2711cc1

Please sign in to comment.