initial impl of transitions
This commit is contained in:
parent
82c4384097
commit
0c8e502b2f
3 changed files with 124 additions and 88 deletions
|
|
@ -27,32 +27,17 @@ const getSubscribePath = () => `${discoveryPrefix}/+/${nodeId}/#`;
|
|||
const getPath = ({ id, type }) =>
|
||||
`${discoveryPrefix}/${type}/${nodeId}/${id}`;
|
||||
const getConfigPath = plug => `${getPath(plug)}/config`;
|
||||
const getAvailabilityTopic = plug => `${getPath(plug)}/availability`;
|
||||
const getStateTopic = plug => `${getPath(plug)}/state`;
|
||||
const getBrightnessCommandTopic = plug => `${getPath(plug)}/setBrightness`;
|
||||
const getBrightnessTopic = plug => `${getPath(plug)}/brightness`;
|
||||
const getCommandTopic = plug => `${getPath(plug)}/set`;
|
||||
|
||||
const getDiscoveryDimmablePayload = device => ({
|
||||
name: device.name,
|
||||
unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`,
|
||||
state_topic: getStateTopic(device),
|
||||
command_topic: getCommandTopic(device),
|
||||
brightness_command_topic: getBrightnessCommandTopic(device),
|
||||
brightness_state_topic: getBrightnessTopic(device),
|
||||
payload_on: 1,
|
||||
payload_off: 0,
|
||||
optimistic: false
|
||||
});
|
||||
|
||||
const getDiscoveryPayload = device => ({
|
||||
schema: 'json',
|
||||
name: device.name,
|
||||
unique_id: `light.plejd.${device.name.toLowerCase().replace(/ /g, '')}`,
|
||||
state_topic: getStateTopic(device),
|
||||
command_topic: getCommandTopic(device),
|
||||
payload_on: 1,
|
||||
payload_off: 0,
|
||||
optimistic: false
|
||||
optimistic: false,
|
||||
brightness: `${device.dimmable}`
|
||||
});
|
||||
|
||||
// #endregion
|
||||
|
|
@ -99,24 +84,17 @@ class MqttClient extends EventEmitter {
|
|||
});
|
||||
|
||||
this.client.on('message', (topic, message) => {
|
||||
const command = message.toString();
|
||||
//const command = message.toString();
|
||||
const command = JSON.parse(message.toString());
|
||||
|
||||
if (topic === startTopic) {
|
||||
logger('home assistant has started. lets do discovery.');
|
||||
self.emit('connected');
|
||||
}
|
||||
|
||||
if (_.includes(topic, 'setBrightness')) {
|
||||
const device = self.devices.find(x => getBrightnessCommandTopic(x) === topic);
|
||||
logger('got brightness update for ' + device.name + ' with brightness: ' + command);
|
||||
|
||||
self.emit('brightnessChanged', device.id, parseInt(command));
|
||||
}
|
||||
else if (_.includes(topic, 'set') && _.includes(['0', '1'], command)) {
|
||||
if (_.includes(topic, 'set')) {
|
||||
const device = self.devices.find(x => getCommandTopic(x) === topic);
|
||||
logger('got state update for ' + device.name + ' with state: ' + command);
|
||||
|
||||
self.emit('stateChanged', device.id, parseInt(command));
|
||||
self.emit('stateChanged', device.id, command);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
@ -134,16 +112,8 @@ class MqttClient extends EventEmitter {
|
|||
devices.forEach((device) => {
|
||||
logger(`sending discovery for ${device.name}`);
|
||||
|
||||
let payload = null;
|
||||
|
||||
if (device.type === 'switch') {
|
||||
payload = getDiscoveryPayload(device);
|
||||
}
|
||||
else {
|
||||
payload = device.dimmable ? getDiscoveryDimmablePayload(device) : getDiscoveryPayload(device);
|
||||
}
|
||||
|
||||
console.log(`discovered ${device.name} with Plejd ID ${device.id}.`);
|
||||
let payload = getDiscoveryPayload(device);
|
||||
console.log(`discovered ${device.type}: ${device.name} with Plejd ID ${device.id}.`);
|
||||
|
||||
self.deviceMap[device.id] = payload.unique_id;
|
||||
|
||||
|
|
@ -154,7 +124,7 @@ class MqttClient extends EventEmitter {
|
|||
});
|
||||
}
|
||||
|
||||
updateState(deviceId, state) {
|
||||
updateState(deviceId, data) {
|
||||
const device = this.devices.find(x => x.id === deviceId);
|
||||
|
||||
if (!device) {
|
||||
|
|
@ -162,27 +132,26 @@ class MqttClient extends EventEmitter {
|
|||
return;
|
||||
}
|
||||
|
||||
logger('updating state for ' + device.name + ': ' + state);
|
||||
logger('updating state for ' + device.name + ': ' + data.state);
|
||||
let payload = null;
|
||||
|
||||
if (device.dimmable) {
|
||||
payload = {
|
||||
state: data.state === 1 ? 'ON' : 'OFF',
|
||||
brightness: data.brightness
|
||||
}
|
||||
}
|
||||
else {
|
||||
payload = {
|
||||
state: data.state === 1 ? 'ON' : 'OFF'
|
||||
}
|
||||
}
|
||||
|
||||
logger(JSON.stringify(payload));
|
||||
|
||||
this.client.publish(
|
||||
getStateTopic(device),
|
||||
state.toString()
|
||||
);
|
||||
}
|
||||
|
||||
updateBrightness(deviceId, brightness) {
|
||||
const device = this.devices.find(x => x.id === deviceId);
|
||||
|
||||
if (!device) {
|
||||
logger('error: ' + deviceId + ' is not handled by us.');
|
||||
return;
|
||||
}
|
||||
|
||||
logger('updating brightness for ' + device.name + ': ' + brightness);
|
||||
|
||||
this.client.publish(
|
||||
getBrightnessTopic(device),
|
||||
brightness.toString()
|
||||
JSON.stringify(payload)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue