mirror of
https://github.com/adrianjagielak/home-assistant-futurehome.git
synced 2025-09-13 07:37:09 +00:00
Update demo mode fake state handling
This commit is contained in:
parent
60b116fd97
commit
d68aa6adca
@ -5,6 +5,7 @@
|
|||||||
|
|
||||||
- Added support for 'media_player' service.
|
- Added support for 'media_player' service.
|
||||||
- Removed demo mode 'optimistic' override causing switches to look weird.
|
- Removed demo mode 'optimistic' override causing switches to look weird.
|
||||||
|
- Updated demo mode fake state handling.
|
||||||
|
|
||||||
## 0.1.3 (25.07.2025)
|
## 0.1.3 (25.07.2025)
|
||||||
|
|
||||||
|
@ -164,7 +164,7 @@ address: "${address}",
|
|||||||
service: "${service}",
|
service: "${service}",
|
||||||
uid: "${uid}",
|
uid: "${uid}",
|
||||||
cmd: "${cmd}",
|
cmd: "${cmd}",
|
||||||
val: "${JSON.stringify(val)}",
|
val: ${JSON.stringify(val)},
|
||||||
val_t: "${val_t}"
|
val_t: "${val_t}"
|
||||||
`);
|
`);
|
||||||
|
|
||||||
|
@ -273,10 +273,10 @@ export function haUpdateStateValueReport(parameters: {
|
|||||||
attrName: string;
|
attrName: string;
|
||||||
}) {
|
}) {
|
||||||
// Strip the FIMP envelope so we end up with "/rt:dev/…/ad:x_y"
|
// Strip the FIMP envelope so we end up with "/rt:dev/…/ad:x_y"
|
||||||
const sensorAddr = parameters.topic.replace(/^pt:j1\/mt:evt/, '');
|
const addr = parameters.topic.replace(/^pt:j1\/mt:evt/, '');
|
||||||
|
|
||||||
for (const [stateTopic, payload] of Object.entries(haStateCache)) {
|
for (const [stateTopic, payload] of Object.entries(haStateCache)) {
|
||||||
if (!payload[sensorAddr]) continue;
|
if (!payload[addr]) continue;
|
||||||
|
|
||||||
// Check if the new value has a type property
|
// Check if the new value has a type property
|
||||||
if (
|
if (
|
||||||
@ -289,7 +289,7 @@ export function haUpdateStateValueReport(parameters: {
|
|||||||
const { type: _, ...valueWithoutType } = parameters.value;
|
const { type: _, ...valueWithoutType } = parameters.value;
|
||||||
|
|
||||||
// Get current attribute value
|
// Get current attribute value
|
||||||
const currentAttrValue = payload[sensorAddr][parameters.attrName];
|
const currentAttrValue = payload[addr][parameters.attrName];
|
||||||
|
|
||||||
if (
|
if (
|
||||||
currentAttrValue &&
|
currentAttrValue &&
|
||||||
@ -297,23 +297,23 @@ export function haUpdateStateValueReport(parameters: {
|
|||||||
!Array.isArray(currentAttrValue)
|
!Array.isArray(currentAttrValue)
|
||||||
) {
|
) {
|
||||||
// Current value is already a type map, update the specific type
|
// Current value is already a type map, update the specific type
|
||||||
payload[sensorAddr][parameters.attrName] = {
|
payload[addr][parameters.attrName] = {
|
||||||
...currentAttrValue,
|
...currentAttrValue,
|
||||||
[type]: valueWithoutType,
|
[type]: valueWithoutType,
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
// Current value is not a type map, convert it to one
|
// Current value is not a type map, convert it to one
|
||||||
payload[sensorAddr][parameters.attrName] = {
|
payload[addr][parameters.attrName] = {
|
||||||
[type]: valueWithoutType,
|
[type]: valueWithoutType,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Handle regular value update (non-typed)
|
// Handle regular value update (non-typed)
|
||||||
payload[sensorAddr][parameters.attrName] = parameters.value;
|
payload[addr][parameters.attrName] = parameters.value;
|
||||||
}
|
}
|
||||||
|
|
||||||
log.debug(
|
log.debug(
|
||||||
`Publishing updated sensor value for "${sensorAddr}" to "${stateTopic}"`,
|
`Publishing updated state value for "${addr}" to "${stateTopic}"`,
|
||||||
);
|
);
|
||||||
ha?.publish(stateTopic, JSON.stringify(payload), { retain: true, qos: 2 });
|
ha?.publish(stateTopic, JSON.stringify(payload), { retain: true, qos: 2 });
|
||||||
|
|
||||||
|
@ -40,14 +40,17 @@ export class DemoFimpMqttClient implements IMqttClient {
|
|||||||
qos: 0 | 1 | 2;
|
qos: 0 | 1 | 2;
|
||||||
},
|
},
|
||||||
): void {
|
): void {
|
||||||
|
const responseTopic = topic.replace(/^pt:j1\/mt:cmd/, 'pt:j1/mt:evt');
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const msg = JSON.parse(value);
|
const msg = JSON.parse(value);
|
||||||
|
|
||||||
const sendResponse = (response: FimpResponse) => {
|
const sendResponse = (response: FimpResponse) => {
|
||||||
response.corid = response.corid ?? msg.uid;
|
response.corid = response.corid ?? msg.uid;
|
||||||
|
response.serv = response.serv ?? msg.serv;
|
||||||
const buffer = Buffer.from(JSON.stringify(response));
|
const buffer = Buffer.from(JSON.stringify(response));
|
||||||
for (const handler of this.messageHandlers) {
|
for (const handler of this.messageHandlers) {
|
||||||
handler(topic, buffer, { retain: false } as any);
|
handler(responseTopic, buffer, { retain: false } as any);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -78,10 +81,21 @@ export class DemoFimpMqttClient implements IMqttClient {
|
|||||||
type: 'evt.pd7.response',
|
type: 'evt.pd7.response',
|
||||||
val: { param: { state: { devices: demo_data__state } } },
|
val: { param: { state: { devices: demo_data__state } } },
|
||||||
});
|
});
|
||||||
|
} else if (
|
||||||
|
msg.type.split('.').length === 3 &&
|
||||||
|
msg.type.split('.')[0] === 'cmd' &&
|
||||||
|
msg.type.split('.')[2] === 'set'
|
||||||
|
) {
|
||||||
|
sendResponse({
|
||||||
|
type: `evt.${msg.type.split('.')[1]}.report`,
|
||||||
|
val: msg.val,
|
||||||
|
val_t: msg.val_t,
|
||||||
|
props: msg.props,
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
sendResponse({});
|
sendResponse({});
|
||||||
}
|
}
|
||||||
}, 100);
|
}, 300);
|
||||||
}
|
}
|
||||||
|
|
||||||
on(event: 'message', handler: OnMessageCallback): void;
|
on(event: 'message', handler: OnMessageCallback): void;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user