Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce TS strict type checking. #23601

Merged
merged 27 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ interface DiscoveryEntry {
discovery_payload: KeyValue;
}

const sensorClick: DiscoveryEntry = {
interface Discovered {
mockProperties: Set<MockProperty>;
messages: {[s: string]: {payload: string; published: boolean}};
triggers: Set<string>;
discovered: boolean;
}

const SENSOR_CLICK: Readonly<DiscoveryEntry> = {
type: 'sensor',
object_id: 'click',
mockProperties: [{property: 'click', value: null}],
Expand All @@ -32,22 +39,15 @@ const sensorClick: DiscoveryEntry = {
},
};

interface Discovered {
mockProperties: Set<MockProperty>;
messages: {[s: string]: {payload: string; published: boolean}};
triggers: Set<string>;
discovered: boolean;
}

const ACCESS_STATE = 0b001;
const ACCESS_SET = 0b010;
const GROUP_SUPPORTED_TYPES = ['light', 'switch', 'lock', 'cover'];
const GROUP_SUPPORTED_TYPES: Readonly<string[]> = ['light', 'switch', 'lock', 'cover'];
Koenkk marked this conversation as resolved.
Show resolved Hide resolved
const DEFAULT_STATUS_TOPIC = 'homeassistant/status';
const COVER_OPENING_LOOKUP = ['opening', 'open', 'forward', 'up', 'rising'];
const COVER_CLOSING_LOOKUP = ['closing', 'close', 'backward', 'back', 'reverse', 'down', 'declining'];
const COVER_STOPPED_LOOKUP = ['stopped', 'stop', 'pause', 'paused'];
const SWITCH_DIFFERENT = ['valve_detection', 'window_detection', 'auto_lock', 'away_mode'];
const LEGACY_MAPPING = [
const COVER_OPENING_LOOKUP: Readonly<string[]> = ['opening', 'open', 'forward', 'up', 'rising'];
const COVER_CLOSING_LOOKUP: Readonly<string[]> = ['closing', 'close', 'backward', 'back', 'reverse', 'down', 'declining'];
const COVER_STOPPED_LOOKUP: Readonly<string[]> = ['stopped', 'stop', 'pause', 'paused'];
const SWITCH_DIFFERENT: Readonly<string[]> = ['valve_detection', 'window_detection', 'auto_lock', 'away_mode'];
const LEGACY_MAPPING: Readonly<{models: string[]; discovery: DiscoveryEntry}[]> = [
{
models: [
'WXKG01LM',
Expand Down Expand Up @@ -79,7 +79,7 @@ const LEGACY_MAPPING = [
'QBKG12LM',
'E1743',
],
discovery: sensorClick,
discovery: SENSOR_CLICK,
},
{
models: ['ICTC-G-1'],
Expand Down Expand Up @@ -153,7 +153,7 @@ const BINARY_DISCOVERY_LOOKUP: {[s: string]: KeyValue} = {
window: {device_class: 'window'},
window_detection: {icon: 'mdi:window-open-variant'},
window_open: {device_class: 'window'},
};
} as const;
const NUMERIC_DISCOVERY_LOOKUP: {[s: string]: KeyValue} = {
ac_frequency: {device_class: 'frequency', enabled_by_default: false, entity_category: 'diagnostic', state_class: 'measurement'},
action_duration: {icon: 'mdi:timer', device_class: 'duration'},
Expand Down Expand Up @@ -306,7 +306,7 @@ const NUMERIC_DISCOVERY_LOOKUP: {[s: string]: KeyValue} = {
x_axis: {icon: 'mdi:axis-x-arrow'},
y_axis: {icon: 'mdi:axis-y-arrow'},
z_axis: {icon: 'mdi:axis-z-arrow'},
};
} as const;
const ENUM_DISCOVERY_LOOKUP: {[s: string]: KeyValue} = {
action: {icon: 'mdi:gesture-double-tap'},
alarm_humidity: {entity_category: 'config', icon: 'mdi:water-percent-alert'},
Expand Down Expand Up @@ -348,14 +348,14 @@ const ENUM_DISCOVERY_LOOKUP: {[s: string]: KeyValue} = {
update: {device_class: 'update'},
volume: {entity_category: 'config', icon: 'mdi: volume-high'},
week: {entity_category: 'config', icon: 'mdi:calendar-clock'},
};
} as const;
const LIST_DISCOVERY_LOOKUP: {[s: string]: KeyValue} = {
action: {icon: 'mdi:gesture-double-tap'},
color_options: {icon: 'mdi:palette'},
level_config: {entity_category: 'diagnostic'},
programming_mode: {icon: 'mdi:calendar-clock'},
schedule_settings: {icon: 'mdi:calendar-clock'},
};
} as const;

const featurePropertyWithoutEndpoint = (feature: zhc.Feature): string => {
if (feature.endpoint) {
Expand Down Expand Up @@ -1512,7 +1512,7 @@ export default class HomeAssistant extends Extension {
});

if (isDevice && entity.options.hasOwnProperty('legacy') && !entity.options.legacy) {
configs = configs.filter((c) => c !== sensorClick);
configs = configs.filter((c) => c !== SENSOR_CLICK);
}

if (!this.legacyTrigger) {
Expand Down
24 changes: 14 additions & 10 deletions lib/extension/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export const loadTopicGetSetRegex = (): void => {
};
loadTopicGetSetRegex();

const stateValues = ['on', 'off', 'toggle', 'open', 'close', 'stop', 'lock', 'unlock'];
const sceneConverterKeys = ['scene_store', 'scene_add', 'scene_remove', 'scene_remove_all', 'scene_rename'];
const STATE_VALUES: Readonly<string[]> = ['on', 'off', 'toggle', 'open', 'close', 'stop', 'lock', 'unlock'];
const SCENE_CONVERTER_KEYS: Readonly<string[]> = ['scene_store', 'scene_add', 'scene_remove', 'scene_remove_all', 'scene_rename'];

// Legacy: don't provide default converters anymore, this is required by older z2m installs not saving group members
const defaultGroupConverters = [
const DEFAULT_GROUP_CONVERTERS: Readonly<zhc.Tz.Converter[]> = [
zhc.toZigbee.light_onoff_brightness,
zhc.toZigbee.light_color_colortemp,
philips.tz.effect, // Support Hue effects for groups
Expand Down Expand Up @@ -85,7 +85,7 @@ export default class Publish extends Extension {
try {
return JSON.parse(data.message);
} catch {
if (stateValues.includes(data.message.toLowerCase())) {
if (STATE_VALUES.includes(data.message.toLowerCase())) {
return {state: data.message};
} else {
return undefined;
Expand Down Expand Up @@ -183,11 +183,11 @@ export default class Publish extends Extension {
re.zh.members.map((e) => [e.getDevice().ieeeAddr, this.state.get(this.zigbee.resolveEntity(e.getDevice().ieeeAddr)!)]),
)
: undefined;
let converters: zhc.Tz.Converter[];
let converters: Readonly<zhc.Tz.Converter[]>;

if (Array.isArray(definition)) {
const c = new Set(definition.map((d) => d.toZigbee).flat());
converters = c.size === 0 ? defaultGroupConverters : Array.from(c);
converters = c.size === 0 ? DEFAULT_GROUP_CONVERTERS : Array.from(c);
} else {
converters = definition?.toZigbee ?? [];
}
Expand Down Expand Up @@ -341,10 +341,14 @@ export default class Publish extends Extension {
}
}

const scenesChanged = Object.values(usedConverters).some((cl) => cl.some((c) => c.key?.some((k) => sceneConverterKeys.includes(k))));

if (scenesChanged) {
this.eventBus.emitScenesChanged({entity: re});
outerLoop: for (const converters of Object.values(usedConverters)) {
Koenkk marked this conversation as resolved.
Show resolved Hide resolved
for (const converter of converters) {
const scenesChanged = converter.key?.some((k) => SCENE_CONVERTER_KEYS.includes(k));
if (scenesChanged) {
this.eventBus.emitScenesChanged({entity: re});
break outerLoop;
}
}
}
}
}
Loading