Skip to content

Commit

Permalink
fix: Remove oneOf bool/obj from settings schema (#25195)
Browse files Browse the repository at this point in the history
Co-authored-by: Nerivec <[email protected]>
  • Loading branch information
Koenkk and Nerivec authored Dec 17, 2024
1 parent e7f8de1 commit c3b4628
Show file tree
Hide file tree
Showing 20 changed files with 734 additions and 560 deletions.
12 changes: 10 additions & 2 deletions data/configuration.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@
version: 2

# Home Assistant integration (MQTT discovery)
homeassistant: false
homeassistant:
enabled: false

# Enable the frontend, runs on port 8080 by default
frontend: true
frontend:
enabled: true
# port: 8080

# MQTT settings
mqtt:
Expand All @@ -28,8 +31,13 @@ mqtt:
# # Adapter type, allowed values: `zstack`, `ember`, `deconz`, `zigate` or `zboss`
# adapter: zstack

# Periodically check whether devices are online/offline
# availability:
# enabled: false

# Advanced settings
advanced:
# channel: 11
# Let Zigbee2MQTT generate a network key on first start
network_key: GENERATE
# Let Zigbee2MQTT generate a pan_id on first start
Expand Down
4 changes: 2 additions & 2 deletions lib/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ export class Controller {
new ExtensionAvailability(...this.extensionArgs),
];

if (settings.get().frontend) {
if (settings.get().frontend.enabled) {
this.extensions.push(new ExtensionFrontend(...this.extensionArgs));
}

if (settings.get().homeassistant) {
if (settings.get().homeassistant.enabled) {
this.extensions.push(new ExtensionHomeAssistant(...this.extensionArgs));
}
}
Expand Down
9 changes: 2 additions & 7 deletions lib/extension/availability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,9 @@ export default class Availability extends Extension {
return utils.minutes(device.options.availability.timeout);
}

const key = this.isActiveDevice(device) ? 'active' : 'passive';
let value = settings.get().availability?.[key]?.timeout;
const type = this.isActiveDevice(device) ? 'active' : 'passive';

if (value == null) {
value = key == 'active' ? 10 : 1500;
}

return utils.minutes(value);
return utils.minutes(settings.get().availability[type].timeout);
}

private isActiveDevice(device: Device): boolean {
Expand Down
14 changes: 5 additions & 9 deletions lib/extension/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,12 @@ export default class Bridge extends Extension {
throw new Error(`Invalid payload`);
}

const newSettings = message.options;
const restartRequired = settings.apply(newSettings);
if (restartRequired) this.restartRequired = true;
const newSettings = message.options as Partial<Settings>;
this.restartRequired = settings.apply(newSettings);

// Apply some settings on-the-fly.
if (newSettings.homeassistant != undefined) {
await this.enableDisableExtension(!!settings.get().homeassistant, 'HomeAssistant');
if (newSettings.homeassistant) {
await this.enableDisableExtension(settings.get().homeassistant.enabled, 'HomeAssistant');
}

if (newSettings.advanced?.log_level != undefined) {
Expand Down Expand Up @@ -671,10 +670,7 @@ export default class Bridge extends Extension {
// @ts-expect-error hidden from publish
delete config.advanced.network_key;
delete config.mqtt.password;

if (config.frontend) {
delete config.frontend.auth_token;
}
delete config.frontend.auth_token;

const networkParams = await this.zigbee.getNetworkParameters();
const payload: Zigbee2MQTTAPI['bridge/info'] = {
Expand Down
2 changes: 1 addition & 1 deletion lib/extension/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class Frontend extends Extension {
super(zigbee, mqtt, state, publishEntityState, eventBus, enableDisableExtension, restartCallback, addExtension);

const frontendSettings = settings.get().frontend;
assert(frontendSettings, 'Frontend extension created without having frontend settings');
assert(frontendSettings.enabled, `Frontend extension created with setting 'enabled: false'`);
this.host = frontendSettings.host;
this.port = frontendSettings.port;
this.sslCert = frontendSettings.ssl_cert;
Expand Down
2 changes: 1 addition & 1 deletion lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export default class HomeAssistant extends Extension {
}

const haSettings = settings.get().homeassistant;
assert(haSettings, 'Home Assistant extension used without settings');
assert(haSettings.enabled, `Home Assistant extension created with setting 'enabled: false'`);
this.discoveryTopic = haSettings.discovery_topic;
this.discoveryRegex = new RegExp(`${haSettings.discovery_topic}/(.*)/(.*)/(.*)/config`);
this.statusTopic = haSettings.status_topic;
Expand Down
2 changes: 1 addition & 1 deletion lib/extension/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export default class Publish extends Extension {
* the color temperature. This would lead to 2 zigbee publishes, where the first one
* (state) is probably unnecessary.
*/
if (settings.get().homeassistant) {
if (settings.get().homeassistant.enabled) {
const hasColorTemp = message.color_temp !== undefined;
const hasColor = message.color !== undefined;
const hasBrightness = message.brightness !== undefined;
Expand Down
9 changes: 6 additions & 3 deletions lib/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,15 @@ declare global {
// Settings
interface Settings {
version?: number;
homeassistant?: {
homeassistant: {
enabled: boolean;
discovery_topic: string;
status_topic: string;
experimental_event_entities: boolean;
legacy_action_sensor: boolean;
};
availability?: {
availability: {
enabled: boolean;
active: {timeout: number};
passive: {timeout: number};
};
Expand Down Expand Up @@ -158,7 +160,8 @@ declare global {
image_block_response_delay?: number;
default_maximum_data_size?: number;
};
frontend?: {
frontend: {
enabled: boolean;
auth_token?: string;
host?: string;
port: number;
Expand Down
Loading

0 comments on commit c3b4628

Please sign in to comment.