2019-09-09 10:48:09 -07:00
|
|
|
const data = require('./stub/data');
|
2019-01-08 11:00:47 -07:00
|
|
|
const settings = require('../lib/util/settings');
|
2020-09-24 09:06:43 -07:00
|
|
|
const stringify = require('json-stable-stringify-without-jsonify');
|
2019-09-09 10:48:09 -07:00
|
|
|
const logger = require('./stub/logger');
|
|
|
|
const zigbeeHerdsman = require('./stub/zigbeeHerdsman');
|
|
|
|
const flushPromises = () => new Promise(setImmediate);
|
|
|
|
const MQTT = require('./stub/mqtt');
|
|
|
|
const Controller = require('../lib/controller');
|
2020-07-03 12:20:22 -07:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const HomeAssistant = require('../lib/extension/homeassistant');
|
2018-11-16 12:23:11 -07:00
|
|
|
|
|
|
|
describe('HomeAssistant extension', () => {
|
2019-09-09 10:48:09 -07:00
|
|
|
beforeEach(async () => {
|
|
|
|
this.version = await require('../lib/util/utils').getZigbee2mqttVersion();
|
2020-08-01 01:36:20 -07:00
|
|
|
this.version = `Zigbee2MQTT ${this.version.version}`;
|
2019-09-09 10:48:09 -07:00
|
|
|
jest.useRealTimers();
|
|
|
|
data.writeDefaultConfiguration();
|
|
|
|
settings._reRead();
|
|
|
|
data.writeEmptyState();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
settings.set(['homeassistant'], true);
|
2019-01-08 11:00:47 -07:00
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should have mapping for all devices supported by zigbee-herdsman-converters', () => {
|
2018-11-16 12:23:11 -07:00
|
|
|
const missing = [];
|
2020-01-09 13:47:19 -07:00
|
|
|
const ha = new HomeAssistant(null, null, null, null, {on: () => {}});
|
2018-11-16 12:23:11 -07:00
|
|
|
|
2020-10-01 09:33:59 -07:00
|
|
|
require('zigbee-herdsman-converters').definitions.forEach((d) => {
|
|
|
|
if (!d.hasOwnProperty('exposes') && !ha._getMapping()[d.model]) {
|
2018-11-16 12:23:11 -07:00
|
|
|
missing.push(d.model);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-09 14:12:10 -07:00
|
|
|
expect(missing).toHaveLength(0);
|
2019-09-09 10:48:09 -07:00
|
|
|
});
|
2019-01-08 11:00:47 -07:00
|
|
|
|
2020-04-03 02:12:58 -07:00
|
|
|
it('Should not have duplicate type/object_ids in a mapping', () => {
|
|
|
|
const duplicated = [];
|
|
|
|
const ha = new HomeAssistant(null, null, null, null, {on: () => {}});
|
|
|
|
|
|
|
|
require('zigbee-herdsman-converters').devices.forEach((d) => {
|
|
|
|
const mapping = ha._getMapping()[d.model];
|
|
|
|
const cfg_type_object_ids = [];
|
|
|
|
|
|
|
|
mapping.forEach((c) => {
|
|
|
|
if (cfg_type_object_ids.includes(c['type'] + '/' + c['object_id'])) {
|
|
|
|
duplicated.push(d.model);
|
|
|
|
} else {
|
|
|
|
cfg_type_object_ids.push(c['type'] + '/' + c['object_id']);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(duplicated).toHaveLength(0);
|
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should discover devices', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
2019-01-08 11:00:47 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
let payload;
|
|
|
|
await flushPromises();
|
2019-01-08 11:00:47 -07:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': '°C',
|
|
|
|
'device_class': 'temperature',
|
|
|
|
'value_template': '{{ value_json.temperature }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_temperature',
|
|
|
|
'unique_id': '0x0017880104e45522_temperature_zigbee2mqtt',
|
2019-01-08 11:00:47 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
2019-01-08 11:00:47 -07:00
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/temperature/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-01-08 11:00:47 -07:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': '%',
|
|
|
|
'device_class': 'humidity',
|
|
|
|
'value_template': '{{ value_json.humidity }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_humidity',
|
|
|
|
'unique_id': '0x0017880104e45522_humidity_zigbee2mqtt',
|
2019-01-08 11:00:47 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
2019-01-08 11:00:47 -07:00
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/humidity/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-01-08 11:00:47 -07:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': 'hPa',
|
|
|
|
'device_class': 'pressure',
|
|
|
|
'value_template': '{{ value_json.pressure }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_pressure',
|
|
|
|
'unique_id': '0x0017880104e45522_pressure_zigbee2mqtt',
|
2019-01-08 11:00:47 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
2019-01-08 11:00:47 -07:00
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/pressure/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-01-08 11:00:47 -07:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': '%',
|
|
|
|
'device_class': 'battery',
|
|
|
|
'value_template': '{{ value_json.battery }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_battery',
|
|
|
|
'unique_id': '0x0017880104e45522_battery_zigbee2mqtt',
|
2019-01-08 11:00:47 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
2019-01-08 11:00:47 -07:00
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/battery/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-02-20 12:10:38 -07:00
|
|
|
|
|
|
|
payload = {
|
2020-01-26 11:57:15 -07:00
|
|
|
'icon': 'mdi:signal',
|
|
|
|
'unit_of_measurement': 'lqi',
|
2019-02-20 12:10:38 -07:00
|
|
|
'value_template': '{{ value_json.linkquality }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_linkquality',
|
|
|
|
'unique_id': '0x0017880104e45522_linkquality_zigbee2mqtt',
|
2019-02-20 12:10:38 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
2019-02-20 12:10:38 -07:00
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/linkquality/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-01-08 11:00:47 -07:00
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should discover devices with precision', async () => {
|
|
|
|
settings.set(['devices', '0x0017880104e45522'], {
|
2019-03-09 20:11:50 -07:00
|
|
|
humidity_precision: 0,
|
|
|
|
temperature_precision: 1,
|
|
|
|
pressure_precision: 2,
|
2019-09-09 10:48:09 -07:00
|
|
|
friendly_name: 'weather_sensor',
|
2019-09-25 03:08:39 -07:00
|
|
|
retain: false,
|
2019-09-09 10:48:09 -07:00
|
|
|
})
|
2019-01-08 11:00:47 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
|
|
|
|
let payload;
|
|
|
|
await flushPromises();
|
2019-01-08 11:00:47 -07:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': '°C',
|
|
|
|
'device_class': 'temperature',
|
2020-05-29 14:13:09 -07:00
|
|
|
'value_template': "{{ value_json.temperature }}",
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_temperature',
|
|
|
|
'unique_id': '0x0017880104e45522_temperature_zigbee2mqtt',
|
2019-01-08 11:00:47 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
2019-01-08 11:00:47 -07:00
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/temperature/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-01-08 11:00:47 -07:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': '%',
|
|
|
|
'device_class': 'humidity',
|
2020-05-29 14:13:09 -07:00
|
|
|
'value_template': '{{ value_json.humidity }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_humidity',
|
|
|
|
'unique_id': '0x0017880104e45522_humidity_zigbee2mqtt',
|
2019-01-08 11:00:47 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
2019-01-08 11:00:47 -07:00
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/humidity/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-01-08 11:00:47 -07:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': 'hPa',
|
|
|
|
'device_class': 'pressure',
|
2020-05-29 14:13:09 -07:00
|
|
|
'value_template': '{{ value_json.pressure }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_pressure',
|
|
|
|
'unique_id': '0x0017880104e45522_pressure_zigbee2mqtt',
|
2019-02-20 12:10:38 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
2019-02-20 12:10:38 -07:00
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/pressure/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-01-08 11:00:47 -07:00
|
|
|
});
|
2019-02-09 11:42:31 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should discover devices with overriden user configuration', async () => {
|
|
|
|
settings.set(['devices', '0x0017880104e45522'], {
|
2019-03-09 20:11:50 -07:00
|
|
|
homeassistant: {
|
|
|
|
expire_after: 30,
|
|
|
|
icon: 'mdi:test',
|
|
|
|
temperature: {
|
|
|
|
expire_after: 90,
|
2019-09-09 10:48:09 -07:00
|
|
|
device: {
|
|
|
|
manufacturer: 'From Xiaomi',
|
|
|
|
sw_version: 'test'
|
|
|
|
}
|
2019-02-09 11:42:31 -07:00
|
|
|
},
|
2019-09-09 10:48:09 -07:00
|
|
|
humidity: {
|
|
|
|
unique_id: null,
|
|
|
|
},
|
|
|
|
device: {
|
|
|
|
manufacturer: 'Not from Xiaomi',
|
|
|
|
model: 'custom model',
|
|
|
|
}
|
2019-03-09 20:11:50 -07:00
|
|
|
},
|
2019-09-09 10:48:09 -07:00
|
|
|
friendly_name: 'weather_sensor',
|
2019-09-25 03:08:39 -07:00
|
|
|
retain: false,
|
2019-09-09 10:48:09 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
2019-02-09 11:42:31 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
let payload;
|
|
|
|
await flushPromises();
|
2019-02-09 11:42:31 -07:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': '°C',
|
|
|
|
'device_class': 'temperature',
|
|
|
|
'value_template': '{{ value_json.temperature }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_temperature',
|
|
|
|
'unique_id': '0x0017880104e45522_temperature_zigbee2mqtt',
|
2019-02-09 11:42:31 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': 'test',
|
|
|
|
'model': 'custom model',
|
|
|
|
'manufacturer': 'From Xiaomi',
|
2019-02-09 11:42:31 -07:00
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
2019-09-09 10:48:09 -07:00
|
|
|
'expire_after': 90,
|
|
|
|
'icon': 'mdi:test',
|
2019-02-09 11:42:31 -07:00
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/temperature/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-02-09 11:42:31 -07:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': '%',
|
|
|
|
'device_class': 'humidity',
|
|
|
|
'value_template': '{{ value_json.humidity }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_humidity',
|
2019-02-09 11:42:31 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
|
|
|
'model': 'custom model',
|
|
|
|
'manufacturer': 'Not from Xiaomi',
|
2019-02-09 11:42:31 -07:00
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
'expire_after': 30,
|
|
|
|
'icon': 'mdi:test',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/humidity/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-02-09 11:42:31 -07:00
|
|
|
});
|
2019-02-23 08:02:45 -07:00
|
|
|
|
2020-10-13 11:33:48 -07:00
|
|
|
it('Should discover devices with overriden user configuration affecting type and object_id', async () => {
|
|
|
|
settings.set(['devices', '0x0017880104e45541'], {
|
|
|
|
friendly_name: 'my_switch',
|
|
|
|
homeassistant: {
|
|
|
|
switch: {
|
|
|
|
type: 'light',
|
|
|
|
object_id: 'light'
|
|
|
|
},
|
|
|
|
light: {
|
|
|
|
type: 'this should be ignored',
|
|
|
|
name: 'my_light_name_override'
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
|
|
|
|
let payload;
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
"availability_topic": "zigbee2mqtt/bridge/state",
|
|
|
|
"command_topic": "zigbee2mqtt/my_switch/set",
|
|
|
|
"device": {
|
|
|
|
"identifiers": [
|
|
|
|
"zigbee2mqtt_0x0017880104e45541"
|
|
|
|
],
|
|
|
|
"manufacturer": "Xiaomi",
|
|
|
|
"model": "Aqara single key wired wall switch without neutral wire. Doesn't work as a router and doesn't support power meter (QBKG04LM)",
|
|
|
|
"name": "my_switch",
|
|
|
|
"sw_version": this.version
|
|
|
|
},
|
|
|
|
"json_attributes_topic": "zigbee2mqtt/my_switch",
|
|
|
|
"name": "my_light_name_override",
|
|
|
|
"payload_off": "OFF",
|
|
|
|
"payload_on": "ON",
|
|
|
|
"state_topic": "zigbee2mqtt/my_switch",
|
|
|
|
"unique_id": "0x0017880104e45541_light_zigbee2mqtt",
|
|
|
|
"value_template": "{{ value_json.state }}"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/light/0x0017880104e45541/light/config',
|
|
|
|
stringify(payload),
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2019-10-07 12:58:35 -07:00
|
|
|
it('Shouldnt discover devices when homeassistant null is set in device options', async () => {
|
|
|
|
settings.set(['devices', '0x0017880104e45522'], {
|
|
|
|
homeassistant: null,
|
|
|
|
friendly_name: 'weather_sensor',
|
|
|
|
retain: false,
|
|
|
|
})
|
|
|
|
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
const topics = MQTT.publish.mock.calls.map((c) => c[0]);
|
|
|
|
expect(topics).not.toContain('homeassistant/sensor/0x0017880104e45522/humidity/config')
|
|
|
|
expect(topics).not.toContain('homeassistant/sensor/0x0017880104e45522/temperature/config')
|
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should discover devices with fan', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
2019-04-26 12:57:38 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
let payload;
|
|
|
|
await flushPromises();
|
2019-04-26 12:57:38 -07:00
|
|
|
|
|
|
|
payload = {
|
2019-09-09 10:48:09 -07:00
|
|
|
"state_topic":"zigbee2mqtt/fan",
|
|
|
|
"state_value_template":"{{ value_json.fan_state }}",
|
|
|
|
"command_topic":"zigbee2mqtt/fan/set/fan_state",
|
|
|
|
"speed_state_topic":"zigbee2mqtt/fan",
|
|
|
|
"speed_command_topic":"zigbee2mqtt/fan/set/fan_mode",
|
|
|
|
"speed_value_template":"{{ value_json.fan_mode }}",
|
|
|
|
"speeds":[
|
|
|
|
"off",
|
|
|
|
"low",
|
|
|
|
"medium",
|
|
|
|
"high",
|
|
|
|
"on",
|
|
|
|
"auto",
|
|
|
|
"smart"
|
|
|
|
],
|
|
|
|
"json_attributes_topic":"zigbee2mqtt/fan",
|
|
|
|
"name":"fan_fan",
|
|
|
|
"unique_id":"0x0017880104e45548_fan_zigbee2mqtt",
|
|
|
|
"device":{
|
|
|
|
"identifiers":[
|
|
|
|
"zigbee2mqtt_0x0017880104e45548"
|
|
|
|
],
|
|
|
|
"name":"fan",
|
|
|
|
"sw_version":this.version,
|
|
|
|
"model":"Universal wink enabled white ceiling fan premier remote control (99432)",
|
|
|
|
"manufacturer":"Hampton Bay"
|
2019-04-26 12:57:38 -07:00
|
|
|
},
|
2019-09-09 10:48:09 -07:00
|
|
|
"availability_topic":"zigbee2mqtt/bridge/state"
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/fan/0x0017880104e45548/fan/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-04-26 12:57:38 -07:00
|
|
|
});
|
|
|
|
|
2020-08-23 13:36:30 -07:00
|
|
|
it('Should discover thermostat devices', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
|
|
|
|
let payload;
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
payload = {
|
|
|
|
"action_template":"{% set values = {'idle':'off','heat':'heating','cool':'cooling','fan only':'fan'} %}{{ values[value_json.running_state] }}",
|
|
|
|
"action_topic":"zigbee2mqtt/TS0601_thermostat",
|
|
|
|
"availability_topic":"zigbee2mqtt/bridge/state",
|
2020-10-06 12:42:05 -07:00
|
|
|
"away_mode_command_topic":"zigbee2mqtt/TS0601_thermostat/set/away_mode",
|
|
|
|
"away_mode_state_template":"{{ value_json.away_mode }}",
|
|
|
|
"away_mode_state_topic":"zigbee2mqtt/TS0601_thermostat",
|
2020-08-23 13:36:30 -07:00
|
|
|
"current_temperature_template":"{{ value_json.local_temperature }}",
|
|
|
|
"current_temperature_topic":"zigbee2mqtt/TS0601_thermostat",
|
|
|
|
"device":{
|
|
|
|
"identifiers":[
|
|
|
|
"zigbee2mqtt_0x0017882104a44559"
|
|
|
|
],
|
|
|
|
"manufacturer":"TuYa",
|
|
|
|
"model":"Radiator valve with thermostat (TS0601_thermostat)",
|
|
|
|
"name":"TS0601_thermostat",
|
|
|
|
"sw_version": this.version,
|
|
|
|
},
|
|
|
|
"hold_command_topic":"zigbee2mqtt/TS0601_thermostat/set/preset",
|
|
|
|
"hold_modes":[
|
|
|
|
"schedule",
|
|
|
|
"manual",
|
|
|
|
"boost",
|
|
|
|
"complex",
|
|
|
|
"comfort",
|
|
|
|
"eco"
|
|
|
|
],
|
|
|
|
"hold_state_template":"{{ value_json.preset }}",
|
|
|
|
"hold_state_topic":"zigbee2mqtt/TS0601_thermostat",
|
|
|
|
"json_attributes_topic":"zigbee2mqtt/TS0601_thermostat",
|
|
|
|
"max_temp":"30",
|
|
|
|
"min_temp":"5",
|
|
|
|
"mode_command_topic":"zigbee2mqtt/TS0601_thermostat/set/system_mode",
|
|
|
|
"mode_state_template":"{{ value_json.system_mode }}",
|
|
|
|
"mode_state_topic":"zigbee2mqtt/TS0601_thermostat",
|
|
|
|
"modes":[
|
|
|
|
"auto"
|
|
|
|
],
|
|
|
|
"name":"TS0601_thermostat_climate",
|
|
|
|
"temp_step":0.5,
|
|
|
|
"temperature_command_topic":"zigbee2mqtt/TS0601_thermostat/set/current_heating_setpoint",
|
|
|
|
"temperature_state_template":"{{ value_json.current_heating_setpoint }}",
|
|
|
|
"temperature_state_topic":"zigbee2mqtt/TS0601_thermostat",
|
|
|
|
"temperature_unit":"C",
|
|
|
|
"unique_id":"0x0017882104a44559_climate_zigbee2mqtt"
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/climate/0x0017882104a44559/climate/config',
|
|
|
|
stringify(payload),
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should discover devices with cover_position', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
2019-02-23 08:02:45 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
let payload;
|
|
|
|
await flushPromises();
|
2019-02-23 08:02:45 -07:00
|
|
|
|
|
|
|
payload = {
|
2020-08-08 04:54:39 -07:00
|
|
|
command_topic: 'zigbee2mqtt/smart vent/set',
|
|
|
|
position_topic: 'zigbee2mqtt/smart vent',
|
|
|
|
set_position_topic: 'zigbee2mqtt/smart vent/set',
|
2019-02-23 08:02:45 -07:00
|
|
|
set_position_template: '{ "position": {{ position }} }',
|
|
|
|
value_template: '{{ value_json.position }}',
|
2020-08-08 04:54:39 -07:00
|
|
|
json_attributes_topic: 'zigbee2mqtt/smart vent',
|
|
|
|
name: 'smart vent cover',
|
2019-09-09 10:48:09 -07:00
|
|
|
unique_id: '0x0017880104e45551_cover_zigbee2mqtt',
|
|
|
|
device:
|
|
|
|
{
|
|
|
|
identifiers: [ 'zigbee2mqtt_0x0017880104e45551' ],
|
2020-08-08 04:54:39 -07:00
|
|
|
name: 'smart vent',
|
2019-09-09 10:48:09 -07:00
|
|
|
sw_version: this.version,
|
|
|
|
model: 'Smart vent (SV01)',
|
|
|
|
manufacturer: 'Keen Home'
|
2019-02-23 08:02:45 -07:00
|
|
|
},
|
2019-09-09 10:48:09 -07:00
|
|
|
availability_topic: 'zigbee2mqtt/bridge/state'
|
2019-02-23 08:02:45 -07:00
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/cover/0x0017880104e45551/cover/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-02-23 08:02:45 -07:00
|
|
|
});
|
2019-03-10 13:48:40 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should discover devices with custom homeassistant_discovery_topic', async () => {
|
|
|
|
settings.set(['advanced', 'homeassistant_discovery_topic'], 'my_custom_discovery_topic')
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
2019-03-10 13:48:40 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
let payload;
|
|
|
|
await flushPromises();
|
2019-03-10 13:48:40 -07:00
|
|
|
|
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': '°C',
|
|
|
|
'device_class': 'temperature',
|
|
|
|
'value_template': '{{ value_json.temperature }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_temperature',
|
|
|
|
'unique_id': '0x0017880104e45522_temperature_zigbee2mqtt',
|
2019-03-10 13:48:40 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
2019-03-10 13:48:40 -07:00
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'my_custom_discovery_topic/sensor/0x0017880104e45522/temperature/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-03-10 13:48:40 -07:00
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should throw error when starting with attributes output', async () => {
|
|
|
|
settings.set(['experimental', 'output'], 'attribute')
|
|
|
|
expect(() => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
}).toThrowError('Home Assitant integration is not possible with attribute output!');
|
|
|
|
});
|
2019-03-10 13:48:40 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should warn when starting with cache_state false', async () => {
|
|
|
|
settings.set(['advanced', 'cache_state'], false);
|
|
|
|
logger.warn.mockClear();
|
|
|
|
controller = new Controller(false);
|
|
|
|
expect(logger.warn).toHaveBeenCalledWith("In order for HomeAssistant integration to work properly set `cache_state: true");
|
|
|
|
});
|
2019-03-10 13:48:40 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Shouldt discover when already discovered', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
const device = zigbeeHerdsman.devices.WSDCGQ11LM;
|
|
|
|
const data = {measuredValue: -85}
|
|
|
|
const payload = {data, cluster: 'msTemperatureMeasurement', device, endpoint: device.getEndpoint(1), type: 'attributeReport', linkquality: 10};
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await zigbeeHerdsman.events.message(payload);
|
|
|
|
await flushPromises();
|
2020-04-15 13:34:59 -07:00
|
|
|
// 1 publish is the publish from receive
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should discover when not discovered yet', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
controller.extensions.find((e) => e.constructor.name === 'HomeAssistant').discovered = {};
|
|
|
|
const device = zigbeeHerdsman.devices.WSDCGQ11LM;
|
|
|
|
const data = {measuredValue: -85}
|
|
|
|
const payload = {data, cluster: 'msTemperatureMeasurement', device, endpoint: device.getEndpoint(1), type: 'attributeReport', linkquality: 10};
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await zigbeeHerdsman.events.message(payload);
|
|
|
|
await flushPromises();
|
|
|
|
const payloadHA = {
|
2019-03-10 13:48:40 -07:00
|
|
|
'unit_of_measurement': '°C',
|
|
|
|
'device_class': 'temperature',
|
|
|
|
'value_template': '{{ value_json.temperature }}',
|
2019-09-09 10:48:09 -07:00
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_temperature',
|
|
|
|
'unique_id': '0x0017880104e45522_temperature_zigbee2mqtt',
|
2019-03-10 13:48:40 -07:00
|
|
|
'device': {
|
2019-09-09 10:48:09 -07:00
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
2019-03-10 13:48:40 -07:00
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/temperature/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payloadHA),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-03-10 13:48:40 -07:00
|
|
|
});
|
2019-03-15 14:41:39 -07:00
|
|
|
|
2020-04-11 11:34:50 -07:00
|
|
|
it('Shouldnt discover when device leaves', async () => {
|
2019-09-09 10:48:09 -07:00
|
|
|
controller = new Controller();
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
controller.extensions.find((e) => e.constructor.name === 'HomeAssistant').discovered = {};
|
|
|
|
const device = zigbeeHerdsman.devices.bulb;
|
|
|
|
const payload = {ieeeAddr: device.ieeeAddr};
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await zigbeeHerdsman.events.deviceLeave(payload);
|
|
|
|
await flushPromises();
|
|
|
|
// 1 publish is from device_removed
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2019-03-15 14:41:39 -07:00
|
|
|
|
2020-07-16 14:16:59 -07:00
|
|
|
it('Should send all status when home assistant comes online (default topic)', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
data.writeDefaultState();
|
|
|
|
controller = new Controller();
|
|
|
|
await controller.start();
|
|
|
|
expect(MQTT.subscribe).toHaveBeenCalledWith('homeassistant/status');
|
|
|
|
await flushPromises();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('homeassistant/status', 'online');
|
|
|
|
await flushPromises();
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'zigbee2mqtt/bulb',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({"state":"ON","brightness":50,"color_temp":370,"linkquality":99,"update_available":false}),
|
2020-07-16 14:16:59 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function)
|
|
|
|
);
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'zigbee2mqtt/remote',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({"brightness":255,"update_available":false}),
|
2020-07-16 14:16:59 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function)
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should send all status when home assistant comes online', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
data.writeDefaultState();
|
|
|
|
controller = new Controller();
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
2020-07-16 14:16:59 -07:00
|
|
|
expect(MQTT.subscribe).toHaveBeenCalledWith('hass/status');
|
2019-09-09 10:48:09 -07:00
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('hass/status', 'online');
|
|
|
|
await flushPromises();
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'zigbee2mqtt/bulb',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({"state":"ON","brightness":50,"color_temp":370,"linkquality":99,"update_available":false}),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function)
|
|
|
|
);
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'zigbee2mqtt/remote',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({"brightness":255,"update_available":false}),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function)
|
|
|
|
);
|
|
|
|
});
|
2019-03-15 14:41:39 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Shouldnt send all status when home assistant comes offline', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
data.writeDefaultState();
|
|
|
|
controller = new Controller();
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('hass/status', 'offline');
|
|
|
|
await flushPromises();
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
2019-03-15 14:41:39 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Shouldnt send all status when home assistant comes online with different topic', async () => {
|
|
|
|
jest.useFakeTimers();
|
|
|
|
data.writeDefaultState();
|
|
|
|
controller = new Controller();
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('hass/status_different', 'offline');
|
|
|
|
await flushPromises();
|
|
|
|
jest.runOnlyPendingTimers();
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(0);
|
2019-06-24 11:52:47 -07:00
|
|
|
});
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
it('Should discover devices with availability', async () => {
|
|
|
|
settings.set(['advanced', 'availability_timeout'], 1)
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
2019-06-24 11:52:47 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
let payload;
|
|
|
|
await flushPromises();
|
2019-06-24 11:52:47 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
payload = {
|
|
|
|
'unit_of_measurement': '°C',
|
|
|
|
'device_class': 'temperature',
|
|
|
|
'value_template': '{{ value_json.temperature }}',
|
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor',
|
|
|
|
'name': 'weather_sensor_temperature',
|
|
|
|
'unique_id': '0x0017880104e45522_temperature_zigbee2mqtt',
|
|
|
|
'device': {
|
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor',
|
|
|
|
'sw_version': this.version,
|
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/weather_sensor/availability',
|
2019-06-24 11:52:47 -07:00
|
|
|
};
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/temperature/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2019-09-09 10:48:09 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2019-03-15 14:41:39 -07:00
|
|
|
});
|
2020-01-09 13:47:19 -07:00
|
|
|
|
|
|
|
it('Should clear discovery when device is removed', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
MQTT.events.message('zigbee2mqtt/bridge/config/remove', 'weather_sensor');
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/temperature/config',
|
|
|
|
null,
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/humidity/config',
|
|
|
|
null,
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/pressure/config',
|
|
|
|
null,
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/battery/config',
|
|
|
|
null,
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/linkquality/config',
|
|
|
|
null,
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('Should not clear discovery when unsupported device is removed', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
MQTT.events.message('zigbee2mqtt/bridge/config/remove', 'unsupported2');
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(1);
|
|
|
|
});
|
2020-02-16 08:00:15 -07:00
|
|
|
|
2020-03-04 04:55:08 -07:00
|
|
|
it('Should refresh discovery when device is renamed', async () => {
|
2020-09-04 12:08:20 -07:00
|
|
|
settings.set(['experimental', 'new_api'], true);
|
2020-03-04 04:55:08 -07:00
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
2020-09-25 10:48:30 -07:00
|
|
|
await MQTT.events.message('homeassistant/device_automation/0x0017880104e45522/action_double/config', stringify({topic: 'zigbee2mqtt/weather_sensor/action'}));
|
|
|
|
await flushPromises();
|
2020-03-04 04:55:08 -07:00
|
|
|
MQTT.publish.mockClear();
|
2020-09-04 12:08:20 -07:00
|
|
|
MQTT.events.message('zigbee2mqtt/bridge/request/device/rename', stringify({"from": "weather_sensor", "to": "weather_sensor_renamed","homeassistant_rename":true}));
|
2020-03-04 04:55:08 -07:00
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
const payload = {
|
|
|
|
'unit_of_measurement': '°C',
|
|
|
|
'device_class': 'temperature',
|
|
|
|
'value_template': '{{ value_json.temperature }}',
|
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor_renamed',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor_renamed',
|
|
|
|
'name': 'weather_sensor_renamed_temperature',
|
|
|
|
'unique_id': '0x0017880104e45522_temperature_zigbee2mqtt',
|
|
|
|
'device': {
|
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor_renamed',
|
|
|
|
'sw_version': this.version,
|
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/temperature/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2020-03-04 04:55:08 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2020-08-17 11:19:44 -07:00
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/temperature/config',
|
2020-09-04 12:08:20 -07:00
|
|
|
null,
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2020-09-25 10:48:30 -07:00
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/device_automation/0x0017880104e45522/action_double/config',
|
|
|
|
stringify({
|
|
|
|
"automation_type":"trigger",
|
|
|
|
"type":"action",
|
|
|
|
"subtype":"double",
|
|
|
|
"payload":"double",
|
|
|
|
"topic":"zigbee2mqtt/weather_sensor_renamed/action",
|
|
|
|
"device":{
|
|
|
|
"identifiers":[
|
|
|
|
"zigbee2mqtt_0x0017880104e45522"
|
|
|
|
],
|
|
|
|
"name":"weather_sensor_renamed",
|
|
|
|
"sw_version": this.version,
|
|
|
|
"model":"Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)",
|
|
|
|
"manufacturer":"Xiaomi"
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2020-09-04 12:08:20 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Shouldnt refresh discovery when device is renamed and homeassistant_rename is false', async () => {
|
|
|
|
settings.set(['experimental', 'new_api'], true);
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
MQTT.events.message('zigbee2mqtt/bridge/request/device/rename', stringify({"from": "weather_sensor", "to": "weather_sensor_renamed","homeassistant_rename":false}));
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
expect(MQTT.publish).not.toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/temperature/config',
|
2020-08-17 11:19:44 -07:00
|
|
|
null,
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2020-09-25 08:44:24 -07:00
|
|
|
|
|
|
|
const payload = {
|
|
|
|
'unit_of_measurement': '°C',
|
|
|
|
'device_class': 'temperature',
|
|
|
|
'value_template': '{{ value_json.temperature }}',
|
|
|
|
'state_topic': 'zigbee2mqtt/weather_sensor_renamed',
|
|
|
|
'json_attributes_topic': 'zigbee2mqtt/weather_sensor_renamed',
|
|
|
|
'name': 'weather_sensor_renamed_temperature',
|
|
|
|
'unique_id': '0x0017880104e45522_temperature_zigbee2mqtt',
|
|
|
|
'device': {
|
|
|
|
'identifiers': ['zigbee2mqtt_0x0017880104e45522'],
|
|
|
|
'name': 'weather_sensor_renamed',
|
|
|
|
'sw_version': this.version,
|
|
|
|
'model': 'Aqara temperature, humidity and pressure sensor (WSDCGQ11LM)',
|
|
|
|
'manufacturer': 'Xiaomi',
|
|
|
|
},
|
|
|
|
'availability_topic': 'zigbee2mqtt/bridge/state',
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/sensor/0x0017880104e45522/temperature/config',
|
|
|
|
stringify(payload),
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2020-03-04 04:55:08 -07:00
|
|
|
});
|
|
|
|
|
2020-02-16 08:00:15 -07:00
|
|
|
it('Should discover update_available sensor when device supports it', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
const payload = {
|
|
|
|
"payload_on":true,
|
|
|
|
"payload_off":false,
|
|
|
|
"value_template":"{{ value_json.update_available}}",
|
|
|
|
"state_topic":"zigbee2mqtt/bulb",
|
|
|
|
"json_attributes_topic":"zigbee2mqtt/bulb",
|
|
|
|
"name":"bulb_update_available",
|
|
|
|
"unique_id":"0x000b57fffec6a5b2_update_available_zigbee2mqtt",
|
|
|
|
"device":{
|
|
|
|
"identifiers":[
|
|
|
|
"zigbee2mqtt_0x000b57fffec6a5b2"
|
|
|
|
],
|
|
|
|
"name":"bulb",
|
|
|
|
'sw_version': this.version,
|
|
|
|
"model":"TRADFRI LED bulb E26/E27 980 lumen, dimmable, white spectrum, opal white (LED1545G12)",
|
|
|
|
"manufacturer":"IKEA"
|
|
|
|
},
|
|
|
|
"availability_topic":"zigbee2mqtt/bridge/state"
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/binary_sensor/0x000b57fffec6a5b2/update_available/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(payload),
|
2020-02-16 08:00:15 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
});
|
2020-02-29 10:07:15 -07:00
|
|
|
|
|
|
|
it('Should discover trigger when click is published', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
2020-03-01 07:55:20 -07:00
|
|
|
|
|
|
|
const discovered = MQTT.publish.mock.calls.filter((c) => c[0].includes('0x0017880104e45520')).map((c) => c[0]);
|
|
|
|
expect(discovered.length).toBe(4);
|
|
|
|
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/click/config');
|
|
|
|
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/action/config');
|
|
|
|
|
2020-02-29 10:07:15 -07:00
|
|
|
MQTT.publish.mockClear();
|
|
|
|
|
|
|
|
const device = zigbeeHerdsman.devices.WXKG11LM;
|
2020-09-25 09:47:28 -07:00
|
|
|
const payload1 = {data: {onOff: 1}, cluster: 'genOnOff', device, endpoint: device.getEndpoint(1), type: 'attributeReport', linkquality: 10};
|
|
|
|
await zigbeeHerdsman.events.message(payload1);
|
2020-02-29 10:07:15 -07:00
|
|
|
await flushPromises();
|
|
|
|
|
2020-08-23 13:58:07 -07:00
|
|
|
const discoverPayloadAction = {
|
2020-02-29 10:07:15 -07:00
|
|
|
"automation_type":"trigger",
|
2020-07-26 05:19:33 -07:00
|
|
|
"type":"action",
|
2020-02-29 10:07:15 -07:00
|
|
|
"subtype":"single",
|
|
|
|
"payload":"single",
|
2020-07-26 05:19:33 -07:00
|
|
|
"topic":"zigbee2mqtt/button/action",
|
2020-02-29 10:07:15 -07:00
|
|
|
"device":{
|
|
|
|
"identifiers":[
|
|
|
|
"zigbee2mqtt_0x0017880104e45520"
|
|
|
|
],
|
|
|
|
"name":"button",
|
|
|
|
"sw_version": this.version,
|
|
|
|
"model":"Aqara wireless switch (WXKG11LM)",
|
|
|
|
"manufacturer":"Xiaomi"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
2020-07-26 05:19:33 -07:00
|
|
|
'homeassistant/device_automation/0x0017880104e45520/action_single/config',
|
2020-08-23 13:58:07 -07:00
|
|
|
stringify(discoverPayloadAction),
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
|
|
|
const discoverPayloadClick = {
|
|
|
|
"automation_type":"trigger",
|
|
|
|
"type":"click",
|
|
|
|
"subtype":"single",
|
|
|
|
"payload":"single",
|
|
|
|
"topic":"zigbee2mqtt/button/click",
|
|
|
|
"device":{
|
|
|
|
"identifiers":[
|
|
|
|
"zigbee2mqtt_0x0017880104e45520"
|
|
|
|
],
|
|
|
|
"name":"button",
|
|
|
|
"sw_version": this.version,
|
|
|
|
"model":"Aqara wireless switch (WXKG11LM)",
|
|
|
|
"manufacturer":"Xiaomi"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'homeassistant/device_automation/0x0017880104e45520/click_single/config',
|
|
|
|
stringify(discoverPayloadClick),
|
2020-02-29 10:07:15 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
2020-07-26 05:19:33 -07:00
|
|
|
'zigbee2mqtt/button/action',
|
2020-02-29 10:07:15 -07:00
|
|
|
'single',
|
|
|
|
{ retain: false, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
2020-08-23 13:58:07 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'zigbee2mqtt/button/click',
|
|
|
|
'single',
|
|
|
|
{ retain: false, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
2020-03-01 07:55:20 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'zigbee2mqtt/button',
|
2020-08-23 13:58:07 -07:00
|
|
|
stringify({action: "single", linkquality: 10, click: "single"}),
|
2020-03-01 07:55:20 -07:00
|
|
|
{ retain: false, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'zigbee2mqtt/button',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({linkquality: 10, action: ""}),
|
2020-03-01 07:55:20 -07:00
|
|
|
{ retain: false, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
2020-08-23 13:58:07 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'zigbee2mqtt/button',
|
|
|
|
stringify({linkquality: 10, click: ""}),
|
|
|
|
{ retain: false, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
2020-02-29 10:07:15 -07:00
|
|
|
// Should only discover it once
|
|
|
|
MQTT.publish.mockClear();
|
2020-09-25 09:47:28 -07:00
|
|
|
await zigbeeHerdsman.events.message(payload1);
|
2020-02-29 10:07:15 -07:00
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).not.toHaveBeenCalledWith(
|
2020-07-26 05:19:33 -07:00
|
|
|
'homeassistant/device_automation/0x0017880104e45520/action_single/config',
|
2020-08-23 13:58:07 -07:00
|
|
|
stringify(discoverPayloadAction),
|
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(MQTT.publish).not.toHaveBeenCalledWith(
|
|
|
|
'homeassistant/device_automation/0x0017880104e45520/click_single/config',
|
|
|
|
stringify(discoverPayloadClick),
|
2020-02-29 10:07:15 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
2020-07-26 05:19:33 -07:00
|
|
|
'zigbee2mqtt/button/action',
|
2020-02-29 10:07:15 -07:00
|
|
|
'single',
|
|
|
|
{ retain: false, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2020-08-23 13:58:07 -07:00
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'zigbee2mqtt/button/click',
|
|
|
|
'single',
|
|
|
|
{ retain: false, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
2020-09-25 09:47:28 -07:00
|
|
|
|
|
|
|
// Shouldn't rediscover when already discovered in previous session
|
2020-09-25 10:48:30 -07:00
|
|
|
controller.extensions.find((e) => e.constructor.name === 'HomeAssistant')._clearDiscoveredTrigger();
|
|
|
|
await MQTT.events.message('homeassistant/device_automation/0x0017880104e45520/action_double/config', stringify({topic: 'zigbee2mqtt/button/action'}));
|
|
|
|
await MQTT.events.message('homeassistant/device_automation/0x0017880104e45520/action_double/config', stringify({topic: 'zigbee2mqtt/button/action'}));
|
2020-09-25 09:47:28 -07:00
|
|
|
await flushPromises();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
const payload2 = {data: {'32768': 2}, cluster: 'genOnOff', device, endpoint: device.getEndpoint(1), type: 'attributeReport', linkquality: 10};
|
|
|
|
await zigbeeHerdsman.events.message(payload2);
|
|
|
|
await flushPromises();
|
2020-09-25 10:48:30 -07:00
|
|
|
expect(MQTT.publish).not.toHaveBeenCalledWith('homeassistant/device_automation/0x0017880104e45520/action_double/config', expect.any(String), expect.any(Object), expect.any(Function));
|
|
|
|
|
|
|
|
// Should rediscover when already discovered in previous session but with diferent name
|
|
|
|
controller.extensions.find((e) => e.constructor.name === 'HomeAssistant')._clearDiscoveredTrigger();
|
|
|
|
await MQTT.events.message('homeassistant/device_automation/0x0017880104e45520/action_double/config', stringify({topic: 'zigbee2mqtt/button_other_name/action'}));
|
|
|
|
await flushPromises();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await zigbeeHerdsman.events.message(payload2);
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith('homeassistant/device_automation/0x0017880104e45520/action_double/config', expect.any(String), expect.any(Object), expect.any(Function));
|
2020-02-29 10:07:15 -07:00
|
|
|
});
|
2020-03-01 07:55:20 -07:00
|
|
|
|
2020-07-24 13:53:10 -07:00
|
|
|
it('Should not discover sensor_click when legacy: false is set', async () => {
|
|
|
|
settings.set(['devices', '0x0017880104e45520'], {
|
|
|
|
legacy: false,
|
|
|
|
friendly_name: 'weather_sensor',
|
|
|
|
retain: false,
|
|
|
|
})
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
const discovered = MQTT.publish.mock.calls.filter((c) => c[0].includes('0x0017880104e45520')).map((c) => c[0]);
|
|
|
|
expect(discovered.length).toBe(3);
|
|
|
|
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/action/config');
|
|
|
|
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/battery/config');
|
|
|
|
expect(discovered).toContain('homeassistant/sensor/0x0017880104e45520/linkquality/config');
|
|
|
|
});
|
|
|
|
|
2020-03-01 07:55:20 -07:00
|
|
|
it('Should disable Home Assistant legacy triggers', async () => {
|
|
|
|
settings.set(['advanced', 'homeassistant_legacy_triggers'], false);
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
const discovered = MQTT.publish.mock.calls.filter((c) => c[0].includes('0x0017880104e45520')).map((c) => c[0]);
|
|
|
|
expect(discovered.length).toBe(2);
|
|
|
|
expect(discovered).not.toContain('homeassistant/sensor/0x0017880104e45520/click/config');
|
|
|
|
expect(discovered).not.toContain('homeassistant/sensor/0x0017880104e45520/action/config');
|
|
|
|
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
|
|
|
|
const device = zigbeeHerdsman.devices.WXKG11LM;
|
2020-07-26 05:19:33 -07:00
|
|
|
settings.set(['devices', device.ieeeAddr, 'legacy'], false);
|
2020-03-01 07:55:20 -07:00
|
|
|
const payload = {data: {onOff: 1}, cluster: 'genOnOff', device, endpoint: device.getEndpoint(1), type: 'attributeReport', linkquality: 10};
|
|
|
|
await zigbeeHerdsman.events.message(payload);
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
const discoverPayload = {
|
|
|
|
"automation_type":"trigger",
|
2020-07-26 05:19:33 -07:00
|
|
|
"type":"action",
|
2020-03-01 07:55:20 -07:00
|
|
|
"subtype":"single",
|
|
|
|
"payload":"single",
|
2020-07-26 05:19:33 -07:00
|
|
|
"topic":"zigbee2mqtt/button/action",
|
2020-03-01 07:55:20 -07:00
|
|
|
"device":{
|
|
|
|
"identifiers":[
|
|
|
|
"zigbee2mqtt_0x0017880104e45520"
|
|
|
|
],
|
|
|
|
"name":"button",
|
|
|
|
"sw_version": this.version,
|
|
|
|
"model":"Aqara wireless switch (WXKG11LM)",
|
|
|
|
"manufacturer":"Xiaomi"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
2020-07-26 05:19:33 -07:00
|
|
|
'homeassistant/device_automation/0x0017880104e45520/action_single/config',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify(discoverPayload),
|
2020-03-01 07:55:20 -07:00
|
|
|
{ retain: true, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
2020-07-26 05:19:33 -07:00
|
|
|
'zigbee2mqtt/button/action',
|
2020-03-01 07:55:20 -07:00
|
|
|
'single',
|
|
|
|
{ retain: false, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith(
|
|
|
|
'zigbee2mqtt/button',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({action: "single", linkquality: 10}),
|
2020-03-01 07:55:20 -07:00
|
|
|
{ retain: false, qos: 0 },
|
|
|
|
expect.any(Function),
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(3);
|
|
|
|
});
|
2020-03-15 01:38:39 -07:00
|
|
|
|
|
|
|
it('Should republish payload to postfix topic with lightWithPostfix config', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
|
2020-08-13 11:00:35 -07:00
|
|
|
await MQTT.events.message('zigbee2mqtt/U202DST600ZB/l2/set', stringify({state: 'ON', brightness: 20}));
|
2020-03-15 01:38:39 -07:00
|
|
|
await flushPromises();
|
2020-08-13 11:00:35 -07:00
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/U202DST600ZB', stringify({state_l2:"ON", brightness_l2:20}), {"qos": 0, "retain": false}, expect.any(Function));
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/U202DST600ZB/l2', stringify({state:"ON", brightness:20}), {"qos": 0, "retain": false}, expect.any(Function));
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith('zigbee2mqtt/U202DST600ZB/l1', stringify({}), {"qos": 0, "retain": false}, expect.any(Function));
|
2020-03-15 01:38:39 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
it('Shouldnt crash in onPublishEntityState on group publish', async () => {
|
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
logger.error.mockClear();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
|
2020-08-13 11:00:35 -07:00
|
|
|
await MQTT.events.message('zigbee2mqtt/group_1/set', stringify({state: 'ON'}));
|
2020-03-15 01:38:39 -07:00
|
|
|
await flushPromises();
|
|
|
|
expect(logger.error).toHaveBeenCalledTimes(0);
|
|
|
|
});
|
2020-04-15 13:34:59 -07:00
|
|
|
|
2020-07-26 05:19:33 -07:00
|
|
|
it('Should counter an action payload with an empty payload', async () => {
|
2020-04-15 13:34:59 -07:00
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
const device = zigbeeHerdsman.devices.WXKG11LM;
|
2020-07-26 05:19:33 -07:00
|
|
|
settings.set(['devices', device.ieeeAddr, 'legacy'], false);
|
2020-04-15 13:34:59 -07:00
|
|
|
const data = {onOff: 1}
|
|
|
|
const payload = {data, cluster: 'genOnOff', device, endpoint: device.getEndpoint(1), type: 'attributeReport', linkquality: 10};
|
|
|
|
await zigbeeHerdsman.events.message(payload);
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(4);
|
|
|
|
expect(MQTT.publish.mock.calls[0][0]).toStrictEqual('zigbee2mqtt/button');
|
2020-07-26 05:19:33 -07:00
|
|
|
expect(JSON.parse(MQTT.publish.mock.calls[0][1])).toStrictEqual({action: 'single', linkquality: 10});
|
2020-04-15 13:34:59 -07:00
|
|
|
expect(MQTT.publish.mock.calls[0][2]).toStrictEqual({"qos": 0, "retain": false});
|
|
|
|
expect(MQTT.publish.mock.calls[1][0]).toStrictEqual('zigbee2mqtt/button');
|
2020-07-26 05:19:33 -07:00
|
|
|
expect(JSON.parse(MQTT.publish.mock.calls[1][1])).toStrictEqual({action: '', linkquality: 10});
|
2020-04-15 13:34:59 -07:00
|
|
|
expect(MQTT.publish.mock.calls[1][2]).toStrictEqual({"qos": 0, "retain": false});
|
2020-07-26 05:19:33 -07:00
|
|
|
expect(MQTT.publish.mock.calls[2][0]).toStrictEqual('homeassistant/device_automation/0x0017880104e45520/action_single/config');
|
|
|
|
expect(MQTT.publish.mock.calls[3][0]).toStrictEqual('zigbee2mqtt/button/action');
|
2020-04-15 13:34:59 -07:00
|
|
|
});
|
2020-07-03 12:20:22 -07:00
|
|
|
|
|
|
|
it('Load Home Assistant mapping from external converters', async () => {
|
|
|
|
fs.copyFileSync(path.join(__dirname, 'assets', 'mock-external-converter-multiple.js'), path.join(data.mockDir, 'mock-external-converter-multiple.js'));
|
|
|
|
const beforeCount = Object.entries((new HomeAssistant(null, null, null, null, {on: () => {}}))._getMapping()).length;
|
|
|
|
settings.set(['external_converters'], ['mock-external-converter-multiple.js']);
|
|
|
|
controller = new Controller();
|
|
|
|
const ha = controller.extensions.find((e) => e.constructor.name === 'HomeAssistant');
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
const afterCount = Object.entries(ha._getMapping()).length;
|
|
|
|
expect(beforeCount + 1).toStrictEqual(afterCount);
|
|
|
|
|
|
|
|
const homeassistantSwitch = {
|
|
|
|
type: 'switch',
|
|
|
|
object_id: 'switch',
|
|
|
|
discovery_payload: {
|
|
|
|
payload_off: 'OFF',
|
|
|
|
payload_on: 'ON',
|
|
|
|
value_template: '{{ value_json.state }}',
|
|
|
|
command_topic: true,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
expect(ha._getMapping()['external_converters_device']).toStrictEqual([homeassistantSwitch]);
|
|
|
|
});
|
2020-08-17 08:24:57 -07:00
|
|
|
|
2020-08-17 11:19:44 -07:00
|
|
|
it('Should clear outdated configs', async () => {
|
2020-08-17 08:24:57 -07:00
|
|
|
controller = new Controller(false);
|
|
|
|
await controller.start();
|
|
|
|
await flushPromises();
|
|
|
|
|
|
|
|
// Non-existing device -> clear
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('homeassistant/sensor/0x123/temperature/config', stringify({availability_topic: 'zigbee2mqtt/0x123/availability'}));
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(1);
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith('homeassistant/sensor/0x123/temperature/config', null, {qos: 0, retain: true}, expect.any(Function));
|
|
|
|
|
|
|
|
// Existing device -> don't clear
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('homeassistant/binary_sensor/0x000b57fffec6a5b2/update_available/config', stringify({availability_topic: 'zigbee2mqtt/0x000b57fffec6a5b2/availability'}));
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
// Non-existing device of different instance -> don't clear
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('homeassistant/sensor/0x123/temperature/config', stringify({availability_topic: 'zigbee2mqtt_different/0x123/availability'}));
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
// Existing device but non-existing config -> don't clear
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('homeassistant/sensor/0x000b57fffec6a5b2/update_available/config', stringify({availability_topic: 'zigbee2mqtt/0x000b57fffec6a5b2/availability'}));
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(1);
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith('homeassistant/sensor/0x000b57fffec6a5b2/update_available/config', null, {qos: 0, retain: true}, expect.any(Function));
|
|
|
|
|
|
|
|
// Non-existing device but invalid payload -> clear
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('homeassistant/sensor/0x123/temperature/config', '1}3');
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(0);
|
2020-09-25 09:20:27 -07:00
|
|
|
|
|
|
|
// Existing device, device automation -> don't clear
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('homeassistant/device_automation/0x000b57fffec6a5b2/action_button_3_single/config', stringify({topic: 'zigbee2mqtt/0x000b57fffec6a5b2/availability'}));
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(0);
|
|
|
|
|
|
|
|
// Not-existing device, device automation -> don't clear
|
|
|
|
MQTT.publish.mockClear();
|
|
|
|
await MQTT.events.message('homeassistant/device_automation/0x000b57fffec6a5b2_not_existing/action_button_3_single/config', stringify({topic: 'zigbee2mqtt/0x000b57fffec6a5b2_not_existing/availability'}));
|
|
|
|
await flushPromises();
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledTimes(1);
|
|
|
|
expect(MQTT.publish).toHaveBeenCalledWith('homeassistant/device_automation/0x000b57fffec6a5b2_not_existing/action_button_3_single/config', null, {qos: 0, retain: true}, expect.any(Function));
|
2020-08-17 08:24:57 -07:00
|
|
|
});
|
2018-11-16 12:23:11 -07:00
|
|
|
});
|