2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
const settings = require('../util/settings');
|
|
|
|
const zigbeeHerdsmanConverters = require('zigbee-herdsman-converters');
|
|
|
|
const logger = require('../util/logger');
|
|
|
|
const utils = require('../util/utils');
|
|
|
|
const assert = require('assert');
|
2020-04-11 09:10:56 -07:00
|
|
|
const Extension = require('./extension');
|
2020-08-13 11:00:35 -07:00
|
|
|
const stringify = require('json-stable-stringify');
|
2019-09-09 10:48:09 -07:00
|
|
|
|
2020-04-19 09:10:53 -07:00
|
|
|
const topicRegex = new RegExp(`^(.+?)(?:/(${utils.getEndpointNames().join('|')}))?/(get|set)(?:/(.+))?`);
|
2020-08-18 12:27:36 -07:00
|
|
|
const stateValues = ['on', 'off', 'toggle', 'open', 'close', 'stop', 'lock', 'unlock'];
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
const groupConverters = [
|
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.light_onoff_brightness,
|
2020-08-05 12:50:27 -07:00
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.light_color_colortemp,
|
2019-09-09 10:48:09 -07:00
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.light_alert,
|
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.ignore_transition,
|
2019-12-13 10:26:03 -07:00
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.cover_position_tilt,
|
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.thermostat_occupied_heating_setpoint,
|
2019-12-16 12:17:58 -07:00
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.tint_scene,
|
2019-12-31 05:52:23 -07:00
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.light_brightness_move,
|
2020-08-10 11:44:44 -07:00
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.light_brightness_step,
|
2020-08-29 07:14:53 -07:00
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.light_colortemp_step,
|
2020-06-09 10:37:45 -07:00
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.light_colortemp_move,
|
2020-08-29 07:14:53 -07:00
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.light_hue_saturation_move,
|
|
|
|
zigbeeHerdsmanConverters.toZigbeeConverters.light_hue_saturation_step,
|
2019-09-09 10:48:09 -07:00
|
|
|
];
|
|
|
|
|
2020-04-11 09:10:56 -07:00
|
|
|
class EntityPublish extends Extension {
|
2019-09-09 10:48:09 -07:00
|
|
|
onMQTTConnected() {
|
|
|
|
// Subscribe to topics.
|
|
|
|
const baseTopic = settings.get().mqtt.base_topic;
|
2020-03-11 13:24:01 -07:00
|
|
|
for (let step = 1; step < 20; step++) {
|
|
|
|
const topic = `${baseTopic}/${'+/'.repeat(step)}`;
|
|
|
|
this.mqtt.subscribe(`${topic}set`);
|
|
|
|
this.mqtt.subscribe(`${topic}set/+`);
|
|
|
|
this.mqtt.subscribe(`${topic}get`);
|
|
|
|
this.mqtt.subscribe(`${topic}get/+`);
|
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
parseTopic(topic) {
|
|
|
|
const match = topic.match(topicRegex);
|
|
|
|
if (!match) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ID = match[1].replace(`${settings.get().mqtt.base_topic}/`, '');
|
2019-11-11 09:27:26 -07:00
|
|
|
// If we didn't replace base_topic we received something we don't care about
|
2019-09-09 10:48:09 -07:00
|
|
|
if (ID === match[1] || ID.match(/bridge/)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-19 09:10:53 -07:00
|
|
|
return {ID: ID, endpointName: match[2] || '', type: match[3], attribute: match[4]};
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
async onMQTTMessage(topic, message) {
|
|
|
|
topic = this.parseTopic(topic);
|
|
|
|
if (!topic) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-19 09:10:53 -07:00
|
|
|
const entityKey = `${topic.ID}` + (topic.endpointName ? `/${topic.endpointName}` : '');
|
|
|
|
const resolvedEntity = this.zigbee.resolveEntity(entityKey);
|
2019-09-09 10:48:09 -07:00
|
|
|
|
2020-04-19 09:10:53 -07:00
|
|
|
if (!resolvedEntity) {
|
2020-04-05 09:36:08 -07:00
|
|
|
/* istanbul ignore else */
|
|
|
|
if (settings.get().advanced.legacy_api) {
|
2020-04-19 09:10:53 -07:00
|
|
|
const message = {friendly_name: entityKey};
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({type: `entity_not_found`, message}),
|
2020-04-05 09:36:08 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-19 09:10:53 -07:00
|
|
|
logger.error(`Entity '${entityKey}' is unknown`);
|
2019-09-09 10:48:09 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get entity details
|
|
|
|
let converters = null;
|
|
|
|
let target = null;
|
|
|
|
let options = {};
|
2019-09-20 12:25:56 -07:00
|
|
|
let device = null;
|
2020-04-19 09:10:53 -07:00
|
|
|
let definition = null;
|
2020-09-19 02:07:54 -07:00
|
|
|
let membersState = null;
|
2019-09-09 10:48:09 -07:00
|
|
|
|
2020-04-19 09:10:53 -07:00
|
|
|
assert(resolvedEntity.type === 'device' || resolvedEntity.type === 'group');
|
|
|
|
if (resolvedEntity.type === 'device') {
|
|
|
|
if (!resolvedEntity.definition) {
|
|
|
|
logger.warn(`Device with modelID '${resolvedEntity.device.modelID}' is not supported.`);
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.warn(`Please see: https://www.zigbee2mqtt.io/how_tos/how_to_support_new_devices.html`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-19 09:10:53 -07:00
|
|
|
device = resolvedEntity.device;
|
|
|
|
definition = resolvedEntity.definition;
|
|
|
|
target = resolvedEntity.endpoint;
|
|
|
|
converters = resolvedEntity.definition.toZigbee;
|
2020-08-17 11:57:12 -07:00
|
|
|
options = resolvedEntity.settings;
|
2019-09-09 10:48:09 -07:00
|
|
|
} else {
|
2020-04-19 09:10:53 -07:00
|
|
|
target = resolvedEntity.group;
|
2020-08-17 11:57:12 -07:00
|
|
|
options = resolvedEntity.settings;
|
2020-08-30 02:30:38 -07:00
|
|
|
definition = resolvedEntity.group.members
|
|
|
|
.map((e) => zigbeeHerdsmanConverters.findByDevice(e.getDevice())).filter((d) => d);
|
|
|
|
converters = new Set(groupConverters);
|
|
|
|
for (const d of definition) {
|
|
|
|
d.toZigbee.forEach(converters.add, converters);
|
|
|
|
}
|
|
|
|
converters = [...converters];
|
2020-09-19 02:07:54 -07:00
|
|
|
membersState = {};
|
|
|
|
for (const member of resolvedEntity.group.members) {
|
|
|
|
const ieeeAddr = member.getDevice().ieeeAddr;
|
|
|
|
membersState[ieeeAddr] = this.state.get(ieeeAddr);
|
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert the MQTT message to a Zigbee message.
|
|
|
|
let json = {};
|
|
|
|
if (topic.hasOwnProperty('attribute') && topic.attribute) {
|
2020-07-27 14:04:05 -07:00
|
|
|
try {
|
|
|
|
json[topic.attribute] = JSON.parse(message);
|
|
|
|
} catch (e) {
|
|
|
|
json[topic.attribute] = message;
|
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
json = JSON.parse(message);
|
|
|
|
} catch (e) {
|
2020-08-18 12:27:36 -07:00
|
|
|
if (stateValues.includes(message.toLowerCase())) {
|
2020-08-17 12:29:11 -07:00
|
|
|
json = {state: message};
|
|
|
|
} else {
|
|
|
|
logger.error(`Invalid JSON '${message}', skipping...`);
|
2020-09-13 06:38:10 -07:00
|
|
|
return false;
|
2020-08-17 12:29:11 -07:00
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
}
|
2020-09-13 06:38:10 -07:00
|
|
|
if (!json) {
|
|
|
|
logger.error(`Invalid JSON '${message}', skipping...`);
|
|
|
|
return;
|
|
|
|
}
|
2020-04-16 12:42:47 -07:00
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
/**
|
|
|
|
* Home Assistant always publishes 'state', even when e.g. only setting
|
|
|
|
* the color temperature. This would lead to 2 zigbee publishes, where the first one
|
|
|
|
* (state) is probably unecessary.
|
|
|
|
*/
|
2020-09-19 02:07:54 -07:00
|
|
|
const entityState = this.state.get(resolvedEntity.settings.ID) || {};
|
2019-09-09 10:48:09 -07:00
|
|
|
if (settings.get().homeassistant) {
|
|
|
|
const hasColorTemp = json.hasOwnProperty('color_temp');
|
|
|
|
const hasColor = json.hasOwnProperty('color');
|
|
|
|
const hasBrightness = json.hasOwnProperty('brightness');
|
2020-09-19 02:07:54 -07:00
|
|
|
const isOn = entityState && entityState.state === 'ON' ? true : false;
|
2019-09-09 10:48:09 -07:00
|
|
|
if (isOn && (hasColorTemp || hasColor) && !hasBrightness) {
|
|
|
|
delete json.state;
|
|
|
|
logger.debug('Skipping state because of Home Assistant');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 12:42:47 -07:00
|
|
|
/**
|
|
|
|
* Order state & brightness based on current bulb state
|
|
|
|
*
|
|
|
|
* Not all bulbs support setting the color/color_temp while it is off
|
|
|
|
* this results in inconsistant behavior between different vendors.
|
|
|
|
*
|
|
|
|
* bulb on => move state & brightness to the back
|
|
|
|
* bulb off => move state & brightness to the front
|
|
|
|
*/
|
2019-09-09 10:48:09 -07:00
|
|
|
const entries = Object.entries(json);
|
2020-04-26 09:18:20 -07:00
|
|
|
const sorter = typeof json.state === 'string' && json.state.toLowerCase() === 'off' ? 1 : -1;
|
|
|
|
entries.sort((a, b) => (['state', 'brightness', 'brightness_percent'].includes(a[0]) ? sorter : sorter * -1));
|
2019-09-09 10:48:09 -07:00
|
|
|
|
|
|
|
// For each attribute call the corresponding converter
|
2020-05-22 09:16:53 -07:00
|
|
|
const usedConverters = {};
|
2019-11-20 09:36:36 -07:00
|
|
|
for (let [key, value] of entries) {
|
2020-04-19 09:10:53 -07:00
|
|
|
let endpointName = topic.endpointName;
|
2019-11-20 09:36:36 -07:00
|
|
|
let actualTarget = target;
|
|
|
|
|
2020-04-19 09:10:53 -07:00
|
|
|
// When the key has a endpointName included (e.g. state_right), this will override the target.
|
|
|
|
if (resolvedEntity.type === 'device' && key.includes('_')) {
|
2019-11-20 09:36:36 -07:00
|
|
|
const underscoreIndex = key.lastIndexOf('_');
|
2020-04-19 09:10:53 -07:00
|
|
|
const possibleEndpointName = key.substring(underscoreIndex + 1, key.length);
|
|
|
|
if (utils.getEndpointNames().includes(possibleEndpointName)) {
|
|
|
|
endpointName = possibleEndpointName;
|
2019-11-20 09:36:36 -07:00
|
|
|
key = key.substring(0, underscoreIndex);
|
|
|
|
const device = target.getDevice();
|
2020-04-19 09:10:53 -07:00
|
|
|
actualTarget = device.getEndpoint(definition.endpoint(device)[endpointName]);
|
2020-05-23 11:48:07 -07:00
|
|
|
|
|
|
|
if (!actualTarget) {
|
|
|
|
logger.error(`Device '${resolvedEntity.name}' has no endpoint '${endpointName}'`);
|
|
|
|
continue;
|
|
|
|
}
|
2019-11-20 09:36:36 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-22 09:16:53 -07:00
|
|
|
const endpointOrGroupID = actualTarget.constructor.name == 'Group' ? actualTarget.groupID : actualTarget.ID;
|
|
|
|
if (!usedConverters.hasOwnProperty(endpointOrGroupID)) usedConverters[endpointOrGroupID] = [];
|
2019-09-09 10:48:09 -07:00
|
|
|
const converter = converters.find((c) => c.key.includes(key));
|
|
|
|
|
2020-05-22 09:16:53 -07:00
|
|
|
if (usedConverters[endpointOrGroupID].includes(converter)) {
|
2019-09-09 10:48:09 -07:00
|
|
|
// Use a converter only once (e.g. light_onoff_brightness converters can convert state and brightness)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!converter) {
|
|
|
|
logger.error(`No converter available for '${key}' (${json[key]})`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converter didn't return a result, skip
|
|
|
|
const meta = {
|
2020-04-19 09:10:53 -07:00
|
|
|
endpoint_name: endpointName,
|
2019-09-09 10:48:09 -07:00
|
|
|
options,
|
|
|
|
message: json,
|
2019-09-17 12:46:16 -07:00
|
|
|
logger,
|
2019-09-20 12:25:56 -07:00
|
|
|
device,
|
2020-09-19 02:07:54 -07:00
|
|
|
state: entityState,
|
|
|
|
membersState,
|
2020-04-19 09:10:53 -07:00
|
|
|
mapped: definition,
|
2019-09-09 10:48:09 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
2020-01-29 13:38:37 -07:00
|
|
|
if (topic.type === 'set' && converter.convertSet) {
|
2020-04-19 09:10:53 -07:00
|
|
|
logger.debug(`Publishing '${topic.type}' '${key}' to '${resolvedEntity.name}'`);
|
2019-11-20 09:36:36 -07:00
|
|
|
const result = await converter.convertSet(actualTarget, key, value, meta);
|
2019-12-07 09:45:00 -07:00
|
|
|
if (result && result.state) {
|
2019-09-09 10:48:09 -07:00
|
|
|
const msg = result.state;
|
|
|
|
|
2020-04-19 09:10:53 -07:00
|
|
|
if (endpointName) {
|
2020-05-09 07:10:31 -07:00
|
|
|
for (const key of ['state', 'brightness', 'color', 'color_temp']) {
|
2020-03-15 01:38:39 -07:00
|
|
|
if (msg.hasOwnProperty(key)) {
|
2020-04-19 09:10:53 -07:00
|
|
|
msg[`${key}_${endpointName}`] = msg[key];
|
2020-03-15 01:38:39 -07:00
|
|
|
delete msg[key];
|
|
|
|
}
|
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
|
2020-04-19 09:10:53 -07:00
|
|
|
this.publishEntityState(resolvedEntity.settings.ID, msg);
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
|
2020-09-19 02:07:54 -07:00
|
|
|
if (result && result.membersState) {
|
|
|
|
for (const [ieeeAddr, state] of Object.entries(result.membersState)) {
|
|
|
|
this.publishEntityState(ieeeAddr, state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-09 10:48:09 -07:00
|
|
|
// It's possible for devices to get out of sync when writing an attribute that's not reportable.
|
|
|
|
// So here we re-read the value after a specified timeout, this timeout could for example be the
|
|
|
|
// transition time of a color change or for forcing a state read for devices that don't
|
|
|
|
// automatically report a new state when set.
|
|
|
|
// When reporting is requested for a device (report: true in device-specific settings) we won't
|
|
|
|
// ever issue a read here, as we assume the device will properly report changes.
|
|
|
|
// Only do this when the retrieve_state option is enabled for this device.
|
|
|
|
if (
|
2020-04-19 09:10:53 -07:00
|
|
|
resolvedEntity.type === 'device' && result && result.hasOwnProperty('readAfterWriteTime') &&
|
|
|
|
resolvedEntity.settings.retrieve_state
|
2019-09-09 10:48:09 -07:00
|
|
|
) {
|
2019-11-20 09:36:36 -07:00
|
|
|
setTimeout(() => converter.convertGet(actualTarget, key, meta), result.readAfterWriteTime);
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
2020-01-29 13:38:37 -07:00
|
|
|
} else if (topic.type === 'get' && converter.convertGet) {
|
2020-04-19 09:10:53 -07:00
|
|
|
logger.debug(`Publishing get '${topic.type}' '${key}' to '${resolvedEntity.name}'`);
|
2019-11-20 09:36:36 -07:00
|
|
|
await converter.convertGet(actualTarget, key, meta);
|
2019-09-09 10:48:09 -07:00
|
|
|
} else {
|
|
|
|
logger.error(`No converter available for '${topic.type}' '${key}' (${json[key]})`);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
const message =
|
2020-04-19 09:10:53 -07:00
|
|
|
`Publish '${topic.type}' '${key}' to '${resolvedEntity.name}' failed: '${error}'`;
|
2019-09-09 10:48:09 -07:00
|
|
|
logger.error(message);
|
2019-12-04 12:46:19 -07:00
|
|
|
logger.debug(error.stack);
|
2020-04-05 09:36:08 -07:00
|
|
|
|
|
|
|
/* istanbul ignore else */
|
|
|
|
if (settings.get().advanced.legacy_api) {
|
2020-04-19 09:10:53 -07:00
|
|
|
const meta = {friendly_name: resolvedEntity.name};
|
2020-04-05 09:36:08 -07:00
|
|
|
this.mqtt.publish(
|
|
|
|
'bridge/log',
|
2020-08-13 11:00:35 -07:00
|
|
|
stringify({type: `zigbee_publish_error`, message, meta}),
|
2020-04-05 09:36:08 -07:00
|
|
|
);
|
|
|
|
}
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
|
2020-05-22 09:16:53 -07:00
|
|
|
usedConverters[endpointOrGroupID].push(converter);
|
2019-09-09 10:48:09 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = EntityPublish;
|