describe('mqttHandler.init', () => { it('should init', async () => { const gladys = { variable: { getValue: fake.resolves('value'), }, system: { isDocker: fake.resolves(false), }, }; const mqttHandler = new MqttHandler(gladys, MockedMqttClient, 'faea9c35-759a-44d5-bcc9-2af1de37b8b4'); await mqttHandler.init(); assert.callCount(gladys.variable.getValue, 3); expect(Object.keys(mqttHandler.topicBinds)).is.deep.eq(DEFAULT.TOPICS); }); });
describe('MqttHandler', () => { let mqttHandler; beforeEach(() => { mqttHandler = new MqttHandler(gladys, MockedMqttClient, 'faea9c35-759a-44d5-bcc9-2af1de37b8b4'); sinon.reset(); }); it('should have falsy status', () => { expect(mqttHandler.configured).to.eq(false); expect(mqttHandler.connected).to.eq(false); }); it('should have binded topics', async () => { await mqttHandler.init(); expect(Object.keys(mqttHandler.topicBinds)).deep.eq(['gladys/master/#']); assert.callCount(gladys.variable.getValue, 3); assert.calledOnce(MockedMqttClient.internalConnect); expect(mqttHandler.configured).to.eq(true); expect(mqttHandler.connected).to.eq(false); }); });
it('decode STATUS11 message', () => { tasmotaHandler.handleMqttMessage('stat/tasmota-device-topic/STATUS11', JSON.stringify(messages.STATUS11)); expect(tasmotaHandler.mqttDevices).to.deep.eq({}); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({ 'tasmota-device-topic': { name: 'Tasmota', model: 13, external_id: 'tasmota:tasmota-device-topic', selector: 'tasmota-tasmota-device-topic', service_id: serviceId, should_poll: false, features: [], }, }); assert.notCalled(gladys.event.emit); assert.notCalled(gladys.stateManager.get); assert.calledWith(mqttService.device.publish, 'cmnd/tasmota-device-topic/STATUS', '8'); });
describe('Mqtt handle message', () => { const mqttHandler = new MqttHandler(gladys, MockedMqttClient, 'faea9c35-759a-44d5-bcc9-2af1de37b8b4'); beforeEach(async () => { sinon.reset(); mqttHandler.subscribe('UNKNOWN_TOPIC', () => {}); }); it('should not unsubscribe, as not connected', () => { mqttHandler.unsubscribe('UNKNOWN_TOPIC'); assert.notCalled(mqttApi.unsubscribe); expect(Object.keys(mqttHandler.topicBinds)).deep.eq([]); }); it('should unsubscribe', async () => { await mqttHandler.connect({ mqttUrl: 'url' }); mqttHandler.unsubscribe('UNKNOWN_TOPIC'); assert.calledWith(mqttApi.unsubscribe, 'UNKNOWN_TOPIC'); expect(Object.keys(mqttHandler.topicBinds)).deep.eq([]); }); });
networkModeValid: true, }; expect(config).to.deep.eq(expectedConfig);
networkModeValid: false, }; expect(config).to.deep.eq(expectedConfig);
tasmotaHandler.handleMqttMessage('stat/tasmota-device-topic/STATUS', JSON.stringify(messages.STATUS)); expect(tasmotaHandler.mqttDevices).to.deep.eq({}); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({ 'tasmota-device-topic': { name: 'Tasmota', tasmotaHandler.handleMqttMessage('stat/tasmota-device-topic/STATUS11', JSON.stringify(messages.STATUS11)); expect(tasmotaHandler.mqttDevices).to.deep.eq({}); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({ 'tasmota-device-topic': { name: 'Tasmota', ], }; expect(tasmotaHandler.mqttDevices).to.deep.eq({ 'tasmota-device-topic': expectedDevice, }); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({});
tasmotaHandler.handleMqttMessage('stat/tasmota-device-topic/STATUS', JSON.stringify(messages.STATUS)); expect(tasmotaHandler.mqttDevices).to.deep.eq({}); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({ 'tasmota-device-topic': { name: 'Tasmota', tasmotaHandler.handleMqttMessage('stat/tasmota-device-topic/STATUS11', JSON.stringify(messages.STATUS11)); expect(tasmotaHandler.mqttDevices).to.deep.eq({}); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({ 'tasmota-device-topic': { name: 'Tasmota', features: [], }; expect(tasmotaHandler.mqttDevices).to.deep.eq({ 'tasmota-device-topic': expectedDevice, }); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({});
networkModeValid: false, }; expect(config).to.deep.eq(expectedConfig);
it('decode STATUS message', () => { tasmotaHandler.handleMqttMessage('stat/tasmota-device-topic/STATUS', JSON.stringify(messages.STATUS)); expect(tasmotaHandler.mqttDevices).to.deep.eq({}); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({ 'tasmota-device-topic': { name: 'Tasmota', model: 13, external_id: 'tasmota:tasmota-device-topic', selector: 'tasmota-tasmota-device-topic', service_id: serviceId, should_poll: false, features: [], }, }); assert.notCalled(gladys.event.emit); assert.notCalled(gladys.stateManager.get); assert.calledWith(mqttService.device.publish, 'cmnd/tasmota-device-topic/STATUS', '11'); });
tasmotaHandler.handleMqttMessage('stat/tasmota-device-topic/STATUS', JSON.stringify(messages.STATUS)); expect(tasmotaHandler.mqttDevices).to.deep.eq({}); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({ 'tasmota-device-topic': { name: 'Tasmota', tasmotaHandler.handleMqttMessage('stat/tasmota-device-topic/STATUS11', JSON.stringify(messages.STATUS11)); expect(tasmotaHandler.mqttDevices).to.deep.eq({}); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({ 'tasmota-device-topic': { name: 'Tasmota', features: [], }; expect(tasmotaHandler.mqttDevices).to.deep.eq({ 'tasmota-device-topic': expectedDevice, }); expect(tasmotaHandler.pendingMqttDevices).to.deep.eq({});
networkModeValid: false, }; expect(config).to.deep.eq(expectedConfig);
networkModeValid: false, }; expect(config).to.deep.eq(expectedConfig);
networkModeValid: false, }; expect(config).to.deep.eq(expectedConfig);