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

MQTT Auto Discover prefix customization #40

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion winet-extractor/.env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
WINET_HOST=
MQTT_URL=
MQTT_PREFIX=homeassistant
WINET_USER=
WINET_PASS=
POLL_INTERVAL=10
ANALYTICS=true
ANALYTICS=true
2 changes: 2 additions & 0 deletions winet-extractor/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ services:
options:
winet_host: ''
mqtt_url: ''
mqtt_prefix: 'homeassistant'
winet_user: ''
winet_pass: ''
poll_interval: 10
Expand All @@ -33,6 +34,7 @@ options:
schema:
winet_host: str
mqtt_url: str
mqtt_prefix: str
winet_user: str?
winet_pass: str?
poll_interval: int(1,3600)
Expand Down
15 changes: 8 additions & 7 deletions winet-extractor/src/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ import Winston from 'winston';
export class MqttPublisher {
private logger: Winston.Logger;
private client: MqttClient;

private prefix: string;
private connected = false;

constructor(logger: Winston.Logger, url: string) {
constructor(logger: Winston.Logger, url: string, prefix: string) {
this.logger = logger;
this.client = mqtt.connect(url);
this.prefix = prefix;
this.client.on('connect', () => {
this.logger.info('Connected to MQTT broker');
this.connected = true;
Expand Down Expand Up @@ -52,7 +53,7 @@ export class MqttPublisher {
unit = 'VA';
value = value * 1000;
}
const topic = `homeassistant/sensor/${deviceSlug}/${slug}/state`;
const topic = `${this.prefix}/sensor/${deviceSlug}/${slug}/state`;

const isTextSensor = TextSensors.includes(slug);
let payload = '';
Expand Down Expand Up @@ -83,9 +84,9 @@ export class MqttPublisher {
}

const identifier = `${device.dev_model}_${device.dev_sn}`;
const configTopic = `homeassistant/sensor/${slug}/config`;
const configTopic = `${this.prefix}/sensor/${slug}/config`;
const configPayload = JSON.stringify({
'~': `homeassistant/sensor/${slug}`,
'~': `${this.prefix}/sensor/${slug}`,
name: `${device.dev_model} ${device.dev_sn}`,
unique_id: slug.toLowerCase(),
state_topic: '~/state',
Expand Down Expand Up @@ -122,7 +123,7 @@ export class MqttPublisher {

const slug = deviceStatus.slug;

const configTopic = `homeassistant/sensor/${deviceSlug}/${slug}/config`;
const configTopic = `${this.prefix}/sensor/${deviceSlug}/${slug}/config`;
const isTextSensor = TextSensors.includes(slug);
const isNumeric = (n: number) => !isNaN(n) && isFinite(n);
const valueTemplate = isNumeric(
Expand All @@ -133,7 +134,7 @@ export class MqttPublisher {
const identifier = `${device.dev_model}_${device.dev_sn}`;
const configPayload: ConfigPayload = {
name: deviceStatus.name.trim(),
state_topic: `homeassistant/sensor/${deviceSlug}/${slug}/state`,
state_topic: `${this.prefix}/sensor/${deviceSlug}/${slug}/state`,
unique_id: `${deviceSlug}_${slug}`.toLowerCase(),
value_template: valueTemplate,
device: {
Expand Down
4 changes: 3 additions & 1 deletion winet-extractor/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const logger = Winston.createLogger({
let options = {
winet_host: '',
mqtt_url: '',
mqtt_prefix: '',
winet_user: '',
winet_pass: '',
poll_interval: '10',
Expand All @@ -43,6 +44,7 @@ if (fs.existsSync('/data/options.json')) {

options.winet_host = process.env.WINET_HOST || '';
options.mqtt_url = process.env.MQTT_URL || '';
options.mqtt_prefix = process.env.MQTT_PREFIX || 'homeassistant';
options.winet_user = process.env.WINET_USER || '';
options.winet_pass = process.env.WINET_PASS || '';
options.poll_interval = process.env.POLL_INTERVAL || '10';
Expand All @@ -62,7 +64,7 @@ if (!options.mqtt_url) {
const lang = 'en_US';
const frequency = parseInt(options.poll_interval) || 10;

const mqtt = new MqttPublisher(logger, options.mqtt_url);
const mqtt = new MqttPublisher(logger, options.mqtt_url, options.mqtt_prefix);
const winet = new winetHandler(
logger,
options.winet_host,
Expand Down