Fix cover more info state display (#15974)

This commit is contained in:
Paul Bottein 2023-03-30 11:55:13 +02:00 committed by GitHub
parent 2b38a1ce33
commit 78cc75c57c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 76 additions and 40 deletions

View File

@ -3,7 +3,9 @@ import {
HassEntityBase, HassEntityBase,
} from "home-assistant-js-websocket"; } from "home-assistant-js-websocket";
import { supportsFeature } from "../common/entity/supports-feature"; import { supportsFeature } from "../common/entity/supports-feature";
import { blankBeforePercent } from "../common/translations/blank_before_percent";
import { UNAVAILABLE } from "./entity"; import { UNAVAILABLE } from "./entity";
import { FrontendLocaleData } from "./translation";
export const enum CoverEntityFeature { export const enum CoverEntityFeature {
OPEN = 1, OPEN = 1,
@ -106,3 +108,18 @@ interface CoverEntityAttributes extends HassEntityAttributeBase {
export interface CoverEntity extends HassEntityBase { export interface CoverEntity extends HassEntityBase {
attributes: CoverEntityAttributes; attributes: CoverEntityAttributes;
} }
export function computeCoverPositionStateDisplay(
stateObj: CoverEntity,
locale: FrontendLocaleData,
position?: number
) {
const currentPosition =
position ??
stateObj.attributes.current_position ??
stateObj.attributes.current_tilt_position;
return currentPosition && currentPosition !== 100
? `${Math.round(currentPosition)}${blankBeforePercent(locale)}%`
: "";
}

View File

@ -10,9 +10,12 @@ import {
import { customElement, property, state } from "lit/decorators"; import { customElement, property, state } from "lit/decorators";
import { computeStateDisplay } from "../../../common/entity/compute_state_display"; import { computeStateDisplay } from "../../../common/entity/compute_state_display";
import { supportsFeature } from "../../../common/entity/supports-feature"; import { supportsFeature } from "../../../common/entity/supports-feature";
import { blankBeforePercent } from "../../../common/translations/blank_before_percent";
import "../../../components/ha-attributes"; import "../../../components/ha-attributes";
import { CoverEntity, CoverEntityFeature } from "../../../data/cover"; import {
computeCoverPositionStateDisplay,
CoverEntity,
CoverEntityFeature,
} from "../../../data/cover";
import type { HomeAssistant } from "../../../types"; import type { HomeAssistant } from "../../../types";
import "../components/cover/ha-more-info-cover-buttons"; import "../components/cover/ha-more-info-cover-buttons";
import "../components/cover/ha-more-info-cover-position"; import "../components/cover/ha-more-info-cover-position";
@ -27,7 +30,9 @@ class MoreInfoCover extends LitElement {
@property({ attribute: false }) public stateObj?: CoverEntity; @property({ attribute: false }) public stateObj?: CoverEntity;
@state() private _displayedPosition?: number; @state() private _livePosition?: number;
@state() private _liveTilt?: number;
@state() private _mode?: "position" | "button"; @state() private _mode?: "position" | "button";
@ -35,20 +40,29 @@ class MoreInfoCover extends LitElement {
this._mode = this._mode === "position" ? "button" : "position"; this._mode = this._mode === "position" ? "button" : "position";
} }
private _positionChanged(ev) { private _positionSliderMoved(ev) {
const value = (ev.detail as any).value; const value = (ev.detail as any).value;
if (isNaN(value)) return; if (isNaN(value)) return;
this._displayedPosition = value; this._livePosition = value;
}
private _positionValueChanged() {
this._livePosition = undefined;
}
private _tiltSliderMoved(ev) {
const value = (ev.detail as any).value;
if (isNaN(value)) return;
this._liveTilt = value;
}
private _tiltValueChanged() {
this._liveTilt = undefined;
} }
protected willUpdate(changedProps: PropertyValues): void { protected willUpdate(changedProps: PropertyValues): void {
super.willUpdate(changedProps); super.willUpdate(changedProps);
if (changedProps.has("stateObj") && this.stateObj) { if (changedProps.has("stateObj") && this.stateObj) {
if (supportsFeature(this.stateObj, CoverEntityFeature.SET_POSITION)) {
const currentPosition = this.stateObj?.attributes.current_position;
this._displayedPosition =
currentPosition != null ? Math.round(currentPosition) : undefined;
}
if (!this._mode) { if (!this._mode) {
this._mode = this._mode =
supportsFeature(this.stateObj, CoverEntityFeature.SET_POSITION) || supportsFeature(this.stateObj, CoverEntityFeature.SET_POSITION) ||
@ -60,29 +74,29 @@ class MoreInfoCover extends LitElement {
} }
private get _stateOverride() { private get _stateOverride() {
if (this._displayedPosition == null) return undefined; const liveValue = this._livePosition ?? this._liveTilt;
const tempState = { const forcedState =
...this.stateObj, liveValue != null ? (liveValue ? "open" : "closed") : undefined;
state: this._displayedPosition ? "open" : "closed",
attributes: {
...this.stateObj!.attributes,
current_position: this._displayedPosition,
},
} as CoverEntity;
const stateDisplay = computeStateDisplay( const stateDisplay = computeStateDisplay(
this.hass.localize, this.hass.localize,
tempState!, this.stateObj!,
this.hass.locale, this.hass.locale,
this.hass.entities this.hass.entities,
forcedState
); );
return this._displayedPosition && this._displayedPosition !== 100 const positionStateDisplay = computeCoverPositionStateDisplay(
? `${stateDisplay} - ${Math.round( this.stateObj!,
this._displayedPosition this.hass.locale,
)}${blankBeforePercent(this.hass!.locale)}%` liveValue
: stateDisplay; );
if (positionStateDisplay) {
return `${stateDisplay}${positionStateDisplay}`;
}
return stateDisplay;
} }
protected render() { protected render() {
@ -133,7 +147,8 @@ class MoreInfoCover extends LitElement {
<ha-more-info-cover-position <ha-more-info-cover-position
.stateObj=${this.stateObj} .stateObj=${this.stateObj}
.hass=${this.hass} .hass=${this.hass}
@slider-moved=${this._positionChanged} @slider-moved=${this._positionSliderMoved}
@value-changed=${this._positionValueChanged}
></ha-more-info-cover-position> ></ha-more-info-cover-position>
` `
: nothing} : nothing}
@ -142,6 +157,8 @@ class MoreInfoCover extends LitElement {
<ha-more-info-cover-tilt-position <ha-more-info-cover-tilt-position
.stateObj=${this.stateObj} .stateObj=${this.stateObj}
.hass=${this.hass} .hass=${this.hass}
@slider-moved=${this._tiltSliderMoved}
@value-changed=${this._tiltValueChanged}
></ha-more-info-cover-tilt-position> ></ha-more-info-cover-tilt-position>
` `
: nothing} : nothing}

View File

@ -36,8 +36,11 @@ import "../../../components/tile/ha-tile-icon";
import "../../../components/tile/ha-tile-image"; import "../../../components/tile/ha-tile-image";
import "../../../components/tile/ha-tile-info"; import "../../../components/tile/ha-tile-info";
import { cameraUrlWithWidthHeight } from "../../../data/camera"; import { cameraUrlWithWidthHeight } from "../../../data/camera";
import { CoverEntity } from "../../../data/cover"; import {
import { isUnavailableState, ON } from "../../../data/entity"; computeCoverPositionStateDisplay,
CoverEntity,
} from "../../../data/cover";
import { isUnavailableState } from "../../../data/entity";
import { FanEntity } from "../../../data/fan"; import { FanEntity } from "../../../data/fan";
import { LightEntity } from "../../../data/light"; import { LightEntity } from "../../../data/light";
import { ActionHandlerEvent } from "../../../data/lovelace"; import { ActionHandlerEvent } from "../../../data/lovelace";
@ -202,7 +205,7 @@ export class HuiTileCard extends LitElement implements LovelaceCard {
`; `;
} }
if (domain === "light" && stateObj.state === ON) { if (domain === "light" && stateActive(stateObj)) {
const brightness = (stateObj as LightEntity).attributes.brightness; const brightness = (stateObj as LightEntity).attributes.brightness;
if (brightness) { if (brightness) {
return `${Math.round((brightness * 100) / 255)}${blankBeforePercent( return `${Math.round((brightness * 100) / 255)}${blankBeforePercent(
@ -211,7 +214,7 @@ export class HuiTileCard extends LitElement implements LovelaceCard {
} }
} }
if (domain === "fan" && stateObj.state === ON) { if (domain === "fan" && stateActive(stateObj)) {
const speed = (stateObj as FanEntity).attributes.percentage; const speed = (stateObj as FanEntity).attributes.percentage;
if (speed) { if (speed) {
return `${Math.round(speed)}${blankBeforePercent(this.hass!.locale)}%`; return `${Math.round(speed)}${blankBeforePercent(this.hass!.locale)}%`;
@ -225,15 +228,14 @@ export class HuiTileCard extends LitElement implements LovelaceCard {
this.hass!.entities this.hass!.entities
); );
if ( if (domain === "cover" && stateActive(stateObj)) {
domain === "cover" && const positionStateDisplay = computeCoverPositionStateDisplay(
["open", "opening", "closing"].includes(stateObj.state) stateObj as CoverEntity,
) { this.hass!.locale
const position = (stateObj as CoverEntity).attributes.current_position; );
if (position && position !== 100) {
return `${stateDisplay} - ${Math.round(position)}${blankBeforePercent( if (positionStateDisplay) {
this.hass!.locale return `${stateDisplay}${positionStateDisplay}`;
)}%`;
} }
} }
return stateDisplay; return stateDisplay;