Skip to content

Commit

Permalink
Use switch for valve in entity row (#19342)
Browse files Browse the repository at this point in the history
  • Loading branch information
piitaya authored Jan 9, 2024
1 parent e8c1a34 commit a5630a4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 23 deletions.
3 changes: 3 additions & 0 deletions src/components/entity/ha-entity-toggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ export class HaEntityToggle extends LitElement {
} else if (stateDomain === "cover") {
serviceDomain = "cover";
service = turnOn ? "open_cover" : "close_cover";
} else if (stateDomain === "valve") {
serviceDomain = "valve";
service = turnOn ? "open_valve" : "close_valve";
} else if (stateDomain === "group") {
serviceDomain = "homeassistant";
service = turnOn ? "turn_on" : "turn_off";
Expand Down
48 changes: 25 additions & 23 deletions src/panels/lovelace/entity-rows/hui-valve-entity-row.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import {
css,
CSSResultGroup,
html,
LitElement,
PropertyValues,
nothing,
} from "lit";
import { LitElement, PropertyValues, html, nothing } from "lit";
import { customElement, property, state } from "lit/decorators";
import "../../../components/ha-valve-controls";
import { ValveEntity } from "../../../data/valve";
import { isUnavailableState } from "../../../data/entity";
import { HomeAssistant } from "../../../types";
import { hasConfigOrEntityChanged } from "../common/has-changed";
import "../components/hui-generic-entity-row";
Expand Down Expand Up @@ -37,7 +30,7 @@ class HuiValveEntityRow extends LitElement implements LovelaceRow {
return nothing;
}

const stateObj = this.hass.states[this._config.entity] as ValveEntity;
const stateObj = this.hass.states[this._config.entity];

if (!stateObj) {
return html`
Expand All @@ -47,23 +40,32 @@ class HuiValveEntityRow extends LitElement implements LovelaceRow {
`;
}

const showToggle =
stateObj.state === "open" ||
stateObj.state === "closed" ||
isUnavailableState(stateObj.state);

return html`
<hui-generic-entity-row .hass=${this.hass} .config=${this._config}>
<ha-valve-controls
.hass=${this.hass}
.stateObj=${stateObj}
></ha-valve-controls>
<hui-generic-entity-row
.hass=${this.hass}
.config=${this._config}
.catchInteraction=${!showToggle}
>
${showToggle
? html`
<ha-entity-toggle
.hass=${this.hass}
.stateObj=${stateObj}
></ha-entity-toggle>
`
: html`
<div class="text-content">
${this.hass.formatEntityState(stateObj)}
</div>
`}
</hui-generic-entity-row>
`;
}

static get styles(): CSSResultGroup {
return css`
ha-valve-controls {
margin-right: -0.57em;
}
`;
}
}

declare global {
Expand Down

0 comments on commit a5630a4

Please sign in to comment.