diff --git a/src/publisher/mqtt/util.ts b/src/publisher/mqtt/util.ts index f97c847..b61114f 100644 --- a/src/publisher/mqtt/util.ts +++ b/src/publisher/mqtt/util.ts @@ -5,8 +5,8 @@ import slugify from 'slugify' export const slugifyName = (name: string): string => { return slugify(name, { - // We can't have "+" in MQTT topic names - remove: /[+]/, + // We can't have "+" and "/" in MQTT topic names + remove: /[+/]/, // Since the rest of the topic name is lower-case, just lower-case everything lower: true, }) diff --git a/tests/publisher/mqtt/util.test.ts b/tests/publisher/mqtt/util.test.ts index 459198a..999d56d 100644 --- a/tests/publisher/mqtt/util.test.ts +++ b/tests/publisher/mqtt/util.test.ts @@ -3,6 +3,7 @@ import { createCharacteristicsSensorTopicName, createPowerSensorTopicName } from import { Circuit } from '../../../src/circuit' test('creates correct topic names', () => { + // Strips spaces const characteristics = { name: 'Some characteristic', } as unknown as Characteristics @@ -10,10 +11,19 @@ test('creates correct topic names', () => { let topic = createCharacteristicsSensorTopicName(characteristics, 'voltage') expect(topic).toEqual('eachwatt/characteristic/some-characteristic/voltage') - const circuit = { + // Strips plus signs + let circuit = { name: 'Some circuit + with plus sign', } as unknown as Circuit topic = createPowerSensorTopicName(circuit, 'power') expect(topic).toEqual('eachwatt/circuit/some-circuit-with-plus-sign/power') + + // Strips forward slashes + circuit = { + name: 'Some circuit / with a slash', + } as unknown as Circuit + + topic = createPowerSensorTopicName(circuit, 'power') + expect(topic).toEqual('eachwatt/circuit/some-circuit-with-a-slash/power') })