From 56f7f3e1bdb2f4f6266d712884cee026bc32c98d Mon Sep 17 00:00:00 2001 From: Miguel Cabral Date: Mon, 19 Dec 2022 19:26:36 +0000 Subject: [PATCH] Expose cover `moving` state to Home Assistant (#15555) --- lib/extension/homeassistant.ts | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/lib/extension/homeassistant.ts b/lib/extension/homeassistant.ts index 2e53ef98f..74a7c1523 100644 --- a/lib/extension/homeassistant.ts +++ b/lib/extension/homeassistant.ts @@ -393,6 +393,7 @@ export default class HomeAssistant extends Extension { const motorState = allExposes?.find((e) => e.type === 'enum' && e.name === 'motor_state' && e.access === ACCESS_STATE); const running = allExposes?.find((e) => e.type === 'binary' && e.name === 'running'); + const moving = allExposes?.find((e) => e.type === 'enum' && e.name === 'moving'); const discoveryEntry: DiscoveryEntry = { type: 'cover', @@ -406,24 +407,38 @@ export default class HomeAssistant extends Extension { }, }; - // For curtains that have `motor_state` lookup a possible state names and make this + // For curtains that have `motor_state` or `moving` lookup a possible state names and make this // available for discovery. If the curtains only support the `running` value, // then we use it anyway. The movement direction is calculated (assumed) in this case. - if (motorState) { + if (motorState || moving) { const openingLookup = ['opening', 'open', 'forward', 'up', 'rising']; const closingLookup = ['closing', 'close', 'backward', 'back', 'reverse', 'down', 'declining']; const stoppedLookup = ['stopped', 'stop', 'pause', 'paused']; - const openingState = motorState.values.find((s) => openingLookup.includes(s.toLowerCase())); - const closingState = motorState.values.find((s) => closingLookup.includes(s.toLowerCase())); - const stoppedState = motorState.values.find((s) => stoppedLookup.includes(s.toLowerCase())); + const movingState = motorState ? motorState : moving; + + const openState = state.values.find((s) => openingLookup.includes(s.toLowerCase())); + const closeState = state.values.find((s) => closingLookup.includes(s.toLowerCase())); + const openingState = movingState.values.find((s) => openingLookup.includes(s.toLowerCase())); + const closingState = movingState.values.find((s) => closingLookup.includes(s.toLowerCase())); + const stoppedState = movingState.values.find((s) => stoppedLookup.includes(s.toLowerCase())); if (openingState && closingState && stoppedState) { discoveryEntry.discovery_payload.state_opening = openingState; discoveryEntry.discovery_payload.state_closing = closingState; - discoveryEntry.discovery_payload.state_stopped = stoppedState; - discoveryEntry.discovery_payload.value_template = `{% if not value_json.${motorState.property} %}` + - ` ${stoppedState} {% else %} {{ value_json.${motorState.property} }} {% endif %}`; + if (openState && closeState) { + discoveryEntry.discovery_payload.state_open = openState; + discoveryEntry.discovery_payload.state_closed = closeState; + discoveryEntry.discovery_payload.value_template = + `{{ value_json.${movingState.property} if value_json.${movingState.property} and` + + ` value_json.${movingState.property} != '${stoppedState}' else` + + ` value_json.${featurePropertyWithoutEndpoint(state)} }}`; + } else { + discoveryEntry.discovery_payload.state_stopped = stoppedState; + discoveryEntry.discovery_payload.value_template = + `{{ '${stoppedState}' if not value_json.${movingState.property}` + + ` else value_json.${movingState.property} }}`; + } } } else if (running) { discoveryEntry.discovery_payload.value_template = `{% if not value_json.${running.property} %} ` +