Skip to content

Commit

Permalink
Added the option to define mqtt-topic prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yusijs committed Aug 9, 2023
1 parent 861ab3f commit a21ab30
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions config/local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ mqtt:
qos: 1
host: mqtt://192.168.86.38:1883
user: homely
topicPrefixes:
# The topic prefix for discovery messages in home assistant. Defaults to homeassistant if not defined
config: homeassistant
# The topic prefix for state messages. Defaults to homely if not defined
state: homely

homely:
# Api can also be test-sdk.iotiliti.cloud
Expand Down
11 changes: 9 additions & 2 deletions entities/discover.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { logger } from '../utils/logger';
import { HomelyFeature } from '../db';
import { SensorWithName } from '../sensors/model';
import { InferCreationAttributes } from 'sequelize';
import config from 'config';
import { Config } from '../models/config';

const configTopic =
config.get<Config['mqtt']>('mqtt').topicPrefixes?.config ?? 'homeassistant';
const stateTopic =
config.get<Config['mqtt']>('mqtt').topicPrefixes?.config ?? 'homely';

// Capitalizes the first letter of a string to make device-names more human-readable
export const capitalize = (s: string) =>
Expand All @@ -28,7 +35,7 @@ export const discover = (
const device_class =
s.deviceClass ??
(s.getDeviceClass ? s.getDeviceClass(m.device) : undefined);
const topicBase = `homely/${m.device.id}`;
const topicBase = `${stateTopic}/${m.device.id}`;
return {
id: s.id,
device_id: m.device.id,
Expand All @@ -43,7 +50,7 @@ export const discover = (
state_class: s.stateClass,
entity_category: s.entityCategory,
name: capitalize(s.name).replace(/_/g, ' '),
config_topic: `homeassistant/${s.type}/${m.device.id}/${s.name}/config`,
config_topic: `${configTopic}/${s.type}/${m.device.id}/${s.name}/config`,
availability_topic: `${topicBase}/online`,
state_topic: `${topicBase}/${s.name}/state`,
} as InferCreationAttributes<HomelyFeature> & { online: boolean };
Expand Down
15 changes: 11 additions & 4 deletions entities/gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { Device, Home } from '../models/home';
import * as crypto from 'crypto';
import { HomelyFeature } from '../db';
import { logger } from '../utils/logger';
import { Config } from '../models/config';
import config from 'config';

const configTopic =
config.get<Config['mqtt']>('mqtt').topicPrefixes?.config ?? 'homeassistant';
const stateTopic =
config.get<Config['mqtt']>('mqtt').topicPrefixes?.config ?? 'homely';

/**
* Create the gateway (alarm-panel) device
Expand Down Expand Up @@ -30,10 +37,10 @@ export const gateway = (homeData: Home) => {
type: 'alarm_control_panel',
device_class: 'alarm_control_panel',
name: 'Gateway',
command_topic: `homely/${homeData.gatewayserial}/armed/command`,
availability_topic: `homely/${homeData.gatewayserial}/availability`,
config_topic: `homeassistant/alarm_control_panel/${homeData.gatewayserial}/config`,
state_topic: `homely/${homeData.gatewayserial}/armed/state`,
command_topic: `${stateTopic}/${homeData.gatewayserial}/armed/command`,
availability_topic: `${stateTopic}/${homeData.gatewayserial}/availability`,
config_topic: `${configTopic}/alarm_control_panel/${homeData.gatewayserial}/config`,
state_topic: `${stateTopic}/${homeData.gatewayserial}/armed/state`,
} as HomelyFeature;
logger.debug({ gatewayDevice: device, gatewaySensor: feature });
return { device, feature };
Expand Down
11 changes: 11 additions & 0 deletions models/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ export type Config = {
enabled?: boolean;
host: string;
user: string;
qos?: number;
topicPrefixes?: {
/**
* The topic prefix for discovery messages in home assistant. Defaults to homeassistant if not defined
*/
config?: string;
/**
* The topic prefix for state messages. Defaults to homely if not defined
*/
state?: string;
};
};
homely: {
host: string;
Expand Down
2 changes: 1 addition & 1 deletion utils/mqtt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ if (!process.env.MQTT_HOST) {
const mqttOptions: IClientOptions = {
username: process.env.MQTT_USER,
password: process.env.MQTT_PASSWORD,
log: logger.debug,
log: (args) => logger.debug(args),
will: {
topic: 'homely/notice',
payload: new Buffer('Homely is offline'),
Expand Down

0 comments on commit a21ab30

Please sign in to comment.