Skip to content

Commit

Permalink
fix eslints
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierZal committed Dec 31, 2023
1 parent 5ade54f commit ebcfb89
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 35 deletions.
10 changes: 5 additions & 5 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {
type HomeySettingValue,
} from './types'

const domain = 'www.ariston-net.remotethermo.com'
const DOMAIN = 'www.ariston-net.remotethermo.com'
const MAX_INT32: number = 2 ** 31 - 1

wrapper(axios)
axios.defaults.jar = new CookieJar()
axios.defaults.baseURL = `https://${domain}`
axios.defaults.baseURL = `https://${DOMAIN}`

export = class AristonApp extends withAPI(App) {
public retry = true
Expand Down Expand Up @@ -63,7 +64,7 @@ export = class AristonApp extends withAPI(App) {
password,
// @ts-expect-error: `CookieJar` is partially typed
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
expires: (config.jar?.store?.idx?.[domain]?.['/']?.[
expires: (config.jar?.store?.idx?.[DOMAIN]?.['/']?.[
'.AspNet.ApplicationCookie'
]?.expires ?? null) as HomeySettings['expires'],
})
Expand Down Expand Up @@ -94,12 +95,11 @@ export = class AristonApp extends withAPI(App) {
.diffNow()
.as('milliseconds')
if (ms > 0) {
const maxTimeout: number = 2 ** 31 - 1
this.#loginTimeout = this.homey.setTimeout(
async (): Promise<void> => {
await this.login()
},
Math.min(ms, maxTimeout),
Math.min(ms, MAX_INT32),
)
}
}
Expand Down
12 changes: 9 additions & 3 deletions decorators/addToLogs.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
/* eslint-disable
@typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
@typescript-eslint/no-explicit-any,
@typescript-eslint/no-unsafe-argument
*/
import type { SimpleClass } from 'homey'

const EMPTY_FUNCTION_PARENS = '()'

const addToLogs = <T extends new (...args: any[]) => SimpleClass>(
...logs: string[]
) =>
Expand All @@ -19,8 +22,11 @@ const addToLogs = <T extends new (...args: any[]) => SimpleClass>(
private commonLog(logType: 'error' | 'log', ...args: any[]): void {
super[logType](
...logs.flatMap((log: string): [any, '-'] => {
if (log.endsWith('()')) {
const funcName: string = log.slice(0, -2)
if (log.endsWith(EMPTY_FUNCTION_PARENS)) {
const funcName: string = log.slice(
0,
-EMPTY_FUNCTION_PARENS.length,
)
const func: () => any = (this as Record<any, any>)[
funcName
] as () => any
Expand Down
62 changes: 39 additions & 23 deletions drivers/nuos/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import addToLogs from '../../decorators/addToLogs'
import withAPI from '../../mixins/withAPI'
import {
OperationMode,
type Switch,
type CapabilityValue,
type CapabilityOptions,
type DeviceDetails,
Expand All @@ -19,13 +20,17 @@ import {
type ReportData,
} from '../../types'

const initialData: PostData = { plantData: {}, viewModel: {} }

enum Mode {
auto = 1,
manual = 2,
}

const ENERGY_REFRESH_INTERVAL: number = Duration.fromObject({ hour: 2 }).as(
'hour',
)
const INITIAL_DATA: PostData = { plantData: {}, viewModel: {} }
const K_MULTIPLIER = 1000

const convertToMode = (value: boolean): Mode =>
value ? Mode.auto : Mode.manual

Expand All @@ -38,6 +43,21 @@ const convertToVacationDate = (value: string): string | null => {
return days ? DateTime.now().plus({ days }).toISODate() : null
}

const getEnergy = (energyData: HistogramData | undefined): number =>
energyData ? energyData.items.reduce<number>((acc, { y }) => acc + y, 0) : 0

const getPower = (energyData: HistogramData | undefined): number => {
const hour: number = DateTime.now().hour
return (
((energyData?.items.find(
({ x }) =>
Number(x) <= hour && hour < Number(x) + ENERGY_REFRESH_INTERVAL,
)?.y ?? 0) *
K_MULTIPLIER) /
ENERGY_REFRESH_INTERVAL
)
}

@addToLogs('getName()')
class NuosDevice extends withAPI(Device) {
public declare driver: NuosDriver
Expand All @@ -46,7 +66,7 @@ class NuosDevice extends withAPI(Device) {

protected app!: AristonApp

#data: PostData = initialData
#data: PostData = INITIAL_DATA

#settings: PostSettings = {}

Expand All @@ -72,11 +92,18 @@ class NuosDevice extends withAPI(Device) {
async (): Promise<void> => {
await this.plantMetering()
},
Duration.fromObject({ hours: 2 }).as('milliseconds'),
Duration.fromObject({ hours: ENERGY_REFRESH_INTERVAL }).as(
'milliseconds',
),
)
},
now
.plus({ hours: now.hour % 2 ? 1 : 2 })
.plus({
hours:
now.hour % ENERGY_REFRESH_INTERVAL
? ENERGY_REFRESH_INTERVAL - 1
: ENERGY_REFRESH_INTERVAL,
})
.set({ minute: 1, second: 0, millisecond: 0 })
.diffNow()
.as('milliseconds'),
Expand Down Expand Up @@ -211,14 +238,14 @@ class NuosDevice extends withAPI(Device) {
break
case 'onoff.legionella':
this.#settings.SlpAntilegionellaOnOff = {
old: Number(oldValue) as 0 | 1,
new: Number(value) as 0 | 1,
old: Number(oldValue) as Switch,
new: Number(value) as Switch,
}
break
case 'onoff.preheating':
this.#settings.SlpPreHeatingOnOff = {
old: Number(oldValue) as 0 | 1,
new: Number(value) as 0 | 1,
old: Number(oldValue) as Switch,
new: Number(value) as Switch,
}
break
case 'operation_mode':
Expand Down Expand Up @@ -346,7 +373,7 @@ class NuosDevice extends withAPI(Device) {
: { fetchSettings: 'true', fetchTimeProg: 'false' },
data: post ? this.#data : undefined,
})
this.#data = initialData
this.#data = INITIAL_DATA
return data.data
} catch (error: unknown) {
return null
Expand All @@ -367,13 +394,13 @@ class NuosDevice extends withAPI(Device) {
if (this.#settings.SlpAntilegionellaOnOff) {
await this.setCapabilityValue(
'onoff.legionella',
!!this.#settings.SlpAntilegionellaOnOff.new,
Boolean(this.#settings.SlpAntilegionellaOnOff.new),
)
}
if (this.#settings.SlpPreHeatingOnOff) {
await this.setCapabilityValue(
'onoff.preheating',
!!this.#settings.SlpPreHeatingOnOff.new,
Boolean(this.#settings.SlpPreHeatingOnOff.new),
)
}
this.#settings = {}
Expand Down Expand Up @@ -442,23 +469,12 @@ class NuosDevice extends withAPI(Device) {
const energyHpData = getEnergyData('DhwHp')
const energyResistorData = getEnergyData('DhwResistor')

const getEnergy = (energyData: HistogramData | undefined): number =>
energyData
? energyData.items.reduce<number>((acc, { y }) => acc + y, 0)
: 0
const energyHp = getEnergy(energyHpData)
const energyResistor = getEnergy(energyResistorData)
await this.setCapabilityValue('meter_power', energyHp + energyResistor)
await this.setCapabilityValue('meter_power.hp', energyHp)
await this.setCapabilityValue('meter_power.resistor', energyResistor)

const hour: number = DateTime.now().hour
const getPower = (energyData: HistogramData | undefined): number =>
((energyData?.items.find(
({ x }) => Number(x) <= hour && hour < Number(x) + 2,
)?.y ?? 0) *
1000) /
2
const powerHp = getPower(energyHpData)
const powerResistor = getPower(energyResistorData)
await this.setCapabilityValue('measure_power', powerHp + powerResistor)
Expand Down
9 changes: 8 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ const customRules = {
}
const tsCustomRules = {
'@typescript-eslint/naming-convention': 'off',
'@typescript-eslint/no-magic-numbers': 'off',
'@typescript-eslint/no-magic-numbers': [
'error',
{
ignore: [0, 1, 2, 31],
ignoreEnums: true,
ignoreReadonlyClassProperties: true,
},
],
'@typescript-eslint/no-unused-vars': [
'error',
{ varsIgnorePattern: 'onHomeyReady' },
Expand Down
3 changes: 2 additions & 1 deletion mixins/withAPI.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable
@typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
@typescript-eslint/no-explicit-any,
@typescript-eslint/no-unsafe-argument
*/
import axios, {
type AxiosError,
Expand Down
9 changes: 7 additions & 2 deletions types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export enum OperationMode {
auto = 3,
}

export enum Switch {
off = 0,
on = 1,
}

export type CapabilityOptions = object & {
readonly min: number
readonly max: number
Expand Down Expand Up @@ -111,8 +116,8 @@ interface BasePostSettingsWithOld<T> extends BasePostSettings<T> {
}

export interface PostSettings {
SlpAntilegionellaOnOff?: BasePostSettingsWithOld<0 | 1>
SlpPreHeatingOnOff?: BasePostSettingsWithOld<0 | 1>
SlpAntilegionellaOnOff?: BasePostSettingsWithOld<Switch>
SlpPreHeatingOnOff?: BasePostSettingsWithOld<Switch>
SlpMinSetpointTemperature?: BasePostSettings<number>
SlpMaxSetpointTemperature?: BasePostSettings<number>
}
Expand Down

0 comments on commit ebcfb89

Please sign in to comment.