Skip to content

Commit

Permalink
added moving state to home assistant for TS130F Curtain/blind switch
Browse files Browse the repository at this point in the history
  • Loading branch information
cmiguelcabral committed Dec 16, 2022
1 parent 52e545f commit fa23b5c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions lib/extension/homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -406,24 +407,27 @@ 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 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 %}`;
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} %} ` +
Expand Down

0 comments on commit fa23b5c

Please sign in to comment.