mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-25 18:26:35 +00:00
Fix fan more info state display (#15979)
This commit is contained in:
parent
dcac853b71
commit
e6772e8b89
@ -9,6 +9,8 @@ import {
|
|||||||
HassEntityAttributeBase,
|
HassEntityAttributeBase,
|
||||||
HassEntityBase,
|
HassEntityBase,
|
||||||
} from "home-assistant-js-websocket";
|
} from "home-assistant-js-websocket";
|
||||||
|
import { blankBeforePercent } from "../common/translations/blank_before_percent";
|
||||||
|
import { FrontendLocaleData } from "./translation";
|
||||||
|
|
||||||
export const enum FanEntityFeature {
|
export const enum FanEntityFeature {
|
||||||
SET_SPEED = 1,
|
SET_SPEED = 1,
|
||||||
@ -91,3 +93,15 @@ export function computeFanSpeedIcon(
|
|||||||
: [mdiFanSpeed1, mdiFanSpeed2, mdiFanSpeed3][index - 1];
|
: [mdiFanSpeed1, mdiFanSpeed2, mdiFanSpeed3][index - 1];
|
||||||
}
|
}
|
||||||
export const FAN_SPEED_COUNT_MAX_FOR_BUTTONS = 4;
|
export const FAN_SPEED_COUNT_MAX_FOR_BUTTONS = 4;
|
||||||
|
|
||||||
|
export function computeFanSpeedStateDisplay(
|
||||||
|
stateObj: FanEntity,
|
||||||
|
locale: FrontendLocaleData,
|
||||||
|
speed?: number
|
||||||
|
) {
|
||||||
|
const currentSpeed = speed ?? stateObj.attributes.percentage;
|
||||||
|
|
||||||
|
return currentSpeed
|
||||||
|
? `${Math.round(currentSpeed)}${blankBeforePercent(locale)}%`
|
||||||
|
: "";
|
||||||
|
}
|
||||||
|
@ -22,11 +22,12 @@ import {
|
|||||||
computeAttributeNameDisplay,
|
computeAttributeNameDisplay,
|
||||||
computeAttributeValueDisplay,
|
computeAttributeValueDisplay,
|
||||||
} from "../../../common/entity/compute_attribute_display";
|
} from "../../../common/entity/compute_attribute_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 { UNAVAILABLE } from "../../../data/entity";
|
import { UNAVAILABLE } from "../../../data/entity";
|
||||||
import {
|
import {
|
||||||
|
computeFanSpeedStateDisplay,
|
||||||
computeFanSpeedCount,
|
computeFanSpeedCount,
|
||||||
FanEntity,
|
FanEntity,
|
||||||
FanEntityFeature,
|
FanEntityFeature,
|
||||||
@ -49,12 +50,16 @@ class MoreInfoFan extends LitElement {
|
|||||||
|
|
||||||
@state() public _presetMode?: string;
|
@state() public _presetMode?: string;
|
||||||
|
|
||||||
@state() private _selectedPercentage?: number;
|
@state() private _liveSpeed?: number;
|
||||||
|
|
||||||
private _percentageChanged(ev) {
|
private _speedSliderMoved(ev) {
|
||||||
const value = (ev.detail as any).value;
|
const value = (ev.detail as any).value;
|
||||||
if (isNaN(value)) return;
|
if (isNaN(value)) return;
|
||||||
this._selectedPercentage = value;
|
this._liveSpeed = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
private _speedValueChanged() {
|
||||||
|
this._liveSpeed = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
private _toggle = () => {
|
private _toggle = () => {
|
||||||
@ -107,12 +112,35 @@ class MoreInfoFan extends LitElement {
|
|||||||
protected updated(changedProps: PropertyValues): void {
|
protected updated(changedProps: PropertyValues): void {
|
||||||
if (changedProps.has("stateObj")) {
|
if (changedProps.has("stateObj")) {
|
||||||
this._presetMode = this.stateObj?.attributes.preset_mode;
|
this._presetMode = this.stateObj?.attributes.preset_mode;
|
||||||
this._selectedPercentage = this.stateObj?.attributes.percentage
|
|
||||||
? Math.round(this.stateObj.attributes.percentage)
|
|
||||||
: undefined;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private get _stateOverride() {
|
||||||
|
const liveValue = this._liveSpeed;
|
||||||
|
|
||||||
|
const forcedState =
|
||||||
|
this._liveSpeed != null ? (this._liveSpeed ? "on" : "off") : undefined;
|
||||||
|
|
||||||
|
const stateDisplay = computeStateDisplay(
|
||||||
|
this.hass.localize,
|
||||||
|
this.stateObj!,
|
||||||
|
this.hass.locale,
|
||||||
|
this.hass.entities,
|
||||||
|
forcedState
|
||||||
|
);
|
||||||
|
|
||||||
|
const positionStateDisplay = computeFanSpeedStateDisplay(
|
||||||
|
this.stateObj!,
|
||||||
|
this.hass.locale,
|
||||||
|
liveValue
|
||||||
|
);
|
||||||
|
|
||||||
|
if (positionStateDisplay) {
|
||||||
|
return positionStateDisplay;
|
||||||
|
}
|
||||||
|
return stateDisplay;
|
||||||
|
}
|
||||||
|
|
||||||
protected render() {
|
protected render() {
|
||||||
if (!this.hass || !this.stateObj) {
|
if (!this.hass || !this.stateObj) {
|
||||||
return nothing;
|
return nothing;
|
||||||
@ -140,17 +168,11 @@ class MoreInfoFan extends LitElement {
|
|||||||
supportsSpeed &&
|
supportsSpeed &&
|
||||||
computeFanSpeedCount(this.stateObj) > FAN_SPEED_COUNT_MAX_FOR_BUTTONS;
|
computeFanSpeedCount(this.stateObj) > FAN_SPEED_COUNT_MAX_FOR_BUTTONS;
|
||||||
|
|
||||||
const stateOverride = this._selectedPercentage
|
|
||||||
? `${Math.round(this._selectedPercentage)}${blankBeforePercent(
|
|
||||||
this.hass!.locale
|
|
||||||
)}%`
|
|
||||||
: undefined;
|
|
||||||
|
|
||||||
return html`
|
return html`
|
||||||
<ha-more-info-state-header
|
<ha-more-info-state-header
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
.stateObj=${this.stateObj}
|
.stateObj=${this.stateObj}
|
||||||
.stateOverride=${stateOverride}
|
.stateOverride=${this._stateOverride}
|
||||||
></ha-more-info-state-header>
|
></ha-more-info-state-header>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
${
|
${
|
||||||
@ -159,7 +181,8 @@ class MoreInfoFan extends LitElement {
|
|||||||
<ha-more-info-fan-speed
|
<ha-more-info-fan-speed
|
||||||
.stateObj=${this.stateObj}
|
.stateObj=${this.stateObj}
|
||||||
.hass=${this.hass}
|
.hass=${this.hass}
|
||||||
@slider-moved=${this._percentageChanged}
|
@slider-moved=${this._speedSliderMoved}
|
||||||
|
@value-changed=${this._speedValueChanged}
|
||||||
>
|
>
|
||||||
</ha-more-info-fan-speed>
|
</ha-more-info-fan-speed>
|
||||||
`
|
`
|
||||||
|
@ -41,7 +41,7 @@ import {
|
|||||||
CoverEntity,
|
CoverEntity,
|
||||||
} from "../../../data/cover";
|
} from "../../../data/cover";
|
||||||
import { isUnavailableState } from "../../../data/entity";
|
import { isUnavailableState } from "../../../data/entity";
|
||||||
import { FanEntity } from "../../../data/fan";
|
import { computeFanSpeedStateDisplay, FanEntity } from "../../../data/fan";
|
||||||
import { LightEntity } from "../../../data/light";
|
import { LightEntity } from "../../../data/light";
|
||||||
import { ActionHandlerEvent } from "../../../data/lovelace";
|
import { ActionHandlerEvent } from "../../../data/lovelace";
|
||||||
import { SENSOR_DEVICE_CLASS_TIMESTAMP } from "../../../data/sensor";
|
import { SENSOR_DEVICE_CLASS_TIMESTAMP } from "../../../data/sensor";
|
||||||
@ -215,9 +215,12 @@ export class HuiTileCard extends LitElement implements LovelaceCard {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (domain === "fan" && stateActive(stateObj)) {
|
if (domain === "fan" && stateActive(stateObj)) {
|
||||||
const speed = (stateObj as FanEntity).attributes.percentage;
|
const speedStateDisplay = computeFanSpeedStateDisplay(
|
||||||
if (speed) {
|
stateObj as FanEntity,
|
||||||
return `${Math.round(speed)}${blankBeforePercent(this.hass!.locale)}%`;
|
this.hass!.locale
|
||||||
|
);
|
||||||
|
if (speedStateDisplay) {
|
||||||
|
return speedStateDisplay;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user