Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierZal committed Dec 2, 2024
1 parent 93b8bcd commit 881fa56
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 32 deletions.
18 changes: 11 additions & 7 deletions app.mts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
thermostatMode,
vertical,
} from './json-files.mts'
import { getDevices, getZones } from './lib/get-zones.mts'
import { getZones } from './lib/get-zones.mts'
import {
fanSpeedValues,
zoneModel,
Expand Down Expand Up @@ -447,16 +447,20 @@ export default class MELCloudApp extends Homey.App {
this.homey.dashboards
.getWidget('ata-group-setting')
.registerSettingAutocompleteListener('default_zone', (query) =>
getZones({ type: DeviceType.Ata }).filter(({ name }) =>
name.toLowerCase().includes(query.toLowerCase()),
),
getZones({ type: DeviceType.Ata })
.filter(({ model }) => model !== 'devices')
.filter(({ name }) =>
name.toLowerCase().includes(query.toLowerCase()),
),
)
this.homey.dashboards
.getWidget('charts')
.registerSettingAutocompleteListener('default_zone', (query) =>
getDevices().filter(({ name }) =>
name.toLowerCase().includes(query.toLowerCase()),
),
getZones()
.filter(({ model }) => model === 'devices')
.filter(({ name }) =>
name.toLowerCase().includes(query.toLowerCase()),
),
)
}

Expand Down
21 changes: 6 additions & 15 deletions lib/get-zones.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
BuildingZone,
DeviceZone,
FloorZone,
Zone,
} from '../types/common.mts'

const LEVEL_1 = 1
Expand Down Expand Up @@ -55,6 +56,7 @@ const filterAndMapDevices = (
.map(({ area, floor, id, name }) => ({
id: `devices_${String(id)}`,
level: getDeviceLevel({ area, floor }),
model: 'devices' as const,
name,
}))
.toSorted(compareNames)
Expand All @@ -69,6 +71,7 @@ const filterAndMapAreas = (
devices: filterAndMapDevices(devices, { type }),
id: `areas_${String(id)}`,
level: floor ? LEVEL_2 : LEVEL_1,
model: 'areas' as const,
name,
}))
.toSorted(compareNames)
Expand All @@ -87,6 +90,7 @@ const filterAndMapFloors = (
),
id: `floors_${String(id)}`,
level: 1,
model: 'floors' as const,
name,
}))
.toSorted(compareNames)
Expand All @@ -110,15 +114,12 @@ export const getBuildings = ({
floors: filterAndMapFloors(floors, { type }),
id: `buildings_${String(id)}`,
level: 0,
model: 'buildings' as const,
name,
}))
.toSorted(compareNames)

export const getZones = ({ type }: { type?: DeviceType } = {}): (
| AreaZone
| BuildingZone
| FloorZone
)[] =>
export const getZones = ({ type }: { type?: DeviceType } = {}): Zone[] =>
getBuildings({ type })
.flatMap(({ areas, devices, floors, ...building }) => [
building,
Expand All @@ -138,13 +139,3 @@ export const getZones = ({ type }: { type?: DeviceType } = {}): (
) ?? []),
])
.toSorted(compareNames)

export const getDevices = ({
type,
}: { type?: DeviceType } = {}): DeviceZone[] =>
(type === undefined ?
DeviceModel.getAll()
: DeviceModel.getAll().filter(({ type: deviceType }) => deviceType === type)
)
.map(({ id, name }) => ({ id: `devices_${String(id)}`, level: 0, name }))
.toSorted(compareNames)
2 changes: 1 addition & 1 deletion settings/index.mts
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ const generateZones = async (zones: Zone[]): Promise<void> =>
const { id, level, name } = zone
createOptionElement(zoneElement, {
id,
label: `${'···'.repeat(level)} ${name}`,
label: `${'··'.repeat(level)} ${name}`,
})
if ('devices' in zone && zone.devices) {
await generateZones(zone.devices)
Expand Down
22 changes: 15 additions & 7 deletions types/common.mts
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,20 @@ export const fanSpeedValues = [
createVeryObject(slow),
] as const

export interface AreaZone extends DeviceZone {
export interface AreaZone extends Omit<DeviceZone, 'model'> {
readonly model: 'areas'
readonly devices?: DeviceZone[]
}

export interface BuildingZone extends FloorZone {
export interface BaseZone {
readonly id: string
readonly level: number
readonly model: 'areas' | 'buildings' | 'devices' | 'floors'
readonly name: string
}

export interface BuildingZone extends Omit<FloorZone, 'model'> {
readonly model: 'buildings'
readonly floors?: FloorZone[]
}

Expand All @@ -196,10 +205,8 @@ export interface DeviceDetails<T extends DeviceType> {
readonly name: string
}

export interface DeviceZone {
readonly id: string
readonly level: number
readonly name: string
export interface DeviceZone extends BaseZone {
readonly model: 'devices'
}

export interface DriverCapabilitiesOptions {
Expand All @@ -222,7 +229,8 @@ export interface DriverSetting {
readonly values?: readonly { readonly id: string; readonly label: string }[]
}

export interface FloorZone extends AreaZone {
export interface FloorZone extends Omit<AreaZone, 'model'> {
readonly model: 'floors'
readonly areas?: AreaZone[]
}

Expand Down
6 changes: 4 additions & 2 deletions widgets/charts/api.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getDevices } from '../../lib/get-zones.mts'
import { getZones } from '../../lib/get-zones.mts'

import type { ReportChartLineOptions } from '@olivierzal/melcloud-api'
import type { Homey } from 'homey/lib/Homey'
Expand All @@ -7,7 +7,9 @@ import type { DaysQuery, DeviceZone } from '../../types/common.mts'

const api = {
getDevices(): DeviceZone[] {
return getDevices()
return getZones().filter(
(zone): zone is DeviceZone => zone.model === 'devices',
)
},
getLanguage({ homey }: { homey: Homey }): string {
return homey.i18n.getLanguage()
Expand Down

0 comments on commit 881fa56

Please sign in to comment.