diff --git a/src/data/cover.ts b/src/data/cover.ts index 8a47b1528c..af372dd5a9 100644 --- a/src/data/cover.ts +++ b/src/data/cover.ts @@ -2,6 +2,7 @@ import { HassEntityAttributeBase, HassEntityBase, } from "home-assistant-js-websocket"; +import { stateActive } from "../common/entity/state_active"; import { supportsFeature } from "../common/entity/supports-feature"; import { blankBeforePercent } from "../common/translations/blank_before_percent"; import { UNAVAILABLE } from "./entity"; @@ -114,10 +115,12 @@ export function computeCoverPositionStateDisplay( locale: FrontendLocaleData, position?: number ) { - const currentPosition = - position ?? - stateObj.attributes.current_position ?? - stateObj.attributes.current_tilt_position; + const statePosition = stateActive(stateObj) + ? stateObj.attributes.current_position ?? + stateObj.attributes.current_tilt_position + : undefined; + + const currentPosition = position ?? statePosition; return currentPosition && currentPosition !== 100 ? `${Math.round(currentPosition)}${blankBeforePercent(locale)}%` diff --git a/src/data/fan.ts b/src/data/fan.ts index 9dbef3fbb4..c5c11d8e00 100644 --- a/src/data/fan.ts +++ b/src/data/fan.ts @@ -9,6 +9,7 @@ import { HassEntityAttributeBase, HassEntityBase, } from "home-assistant-js-websocket"; +import { stateActive } from "../common/entity/state_active"; import { blankBeforePercent } from "../common/translations/blank_before_percent"; import { FrontendLocaleData } from "./translation"; @@ -69,7 +70,7 @@ export function fanSpeedToPercentage( if (speedValue === -1) { return 0; } - return Math.round(speedValue * step); + return Math.floor(speedValue * step); } export function computeFanSpeedCount(stateObj: FanEntity): number { @@ -99,9 +100,12 @@ export function computeFanSpeedStateDisplay( locale: FrontendLocaleData, speed?: number ) { - const currentSpeed = speed ?? stateObj.attributes.percentage; + const percentage = stateActive(stateObj) + ? stateObj.attributes.percentage + : undefined; + const currentSpeed = speed ?? percentage; return currentSpeed - ? `${Math.round(currentSpeed)}${blankBeforePercent(locale)}%` + ? `${Math.floor(currentSpeed)}${blankBeforePercent(locale)}%` : ""; } diff --git a/src/dialogs/more-info/components/fan/ha-more-info-fan-speed.ts b/src/dialogs/more-info/components/fan/ha-more-info-fan-speed.ts index 4762796e1b..41fead80cf 100644 --- a/src/dialogs/more-info/components/fan/ha-more-info-fan-speed.ts +++ b/src/dialogs/more-info/components/fan/ha-more-info-fan-speed.ts @@ -3,6 +3,7 @@ import { customElement, property, state } from "lit/decorators"; import { styleMap } from "lit/directives/style-map"; import { computeAttributeNameDisplay } from "../../../../common/entity/compute_attribute_display"; import { computeStateDisplay } from "../../../../common/entity/compute_state_display"; +import { stateActive } from "../../../../common/entity/state_active"; import { stateColorCss } from "../../../../common/entity/state_color"; import "../../../../components/ha-control-select"; import type { ControlSelectOption } from "../../../../components/ha-control-select"; @@ -26,20 +27,25 @@ export class HaMoreInfoFanSpeed extends LitElement { @property({ attribute: false }) public stateObj!: FanEntity; - @state() value?: number; + @state() sliderValue?: number; + + @state() speedValue?: FanSpeed; protected updated(changedProp: Map): void { if (changedProp.has("stateObj")) { - this.value = - this.stateObj.attributes.percentage != null - ? Math.max(Math.round(this.stateObj.attributes.percentage), 1) - : undefined; + const percentage = stateActive(this.stateObj) + ? this.stateObj.attributes.percentage ?? 0 + : 0; + this.sliderValue = Math.max(Math.round(percentage), 0); + this.speedValue = fanPercentageToSpeed(this.stateObj, percentage); } } private _speedValueChanged(ev: CustomEvent) { const speed = (ev.detail as any).value as FanSpeed; + this.speedValue = speed; + const percentage = fanSpeedToPercentage(this.stateObj, speed); this.hass.callService("fan", "set_percentage", { @@ -52,6 +58,8 @@ export class HaMoreInfoFanSpeed extends LitElement { const value = (ev.detail as any).value; if (isNaN(value)) return; + this.sliderValue = value; + this.hass.callService("fan", "set_percentage", { entity_id: this.stateObj!.entity_id, percentage: value, @@ -88,16 +96,11 @@ export class HaMoreInfoFanSpeed extends LitElement { }) ).reverse(); - const speed = fanPercentageToSpeed( - this.stateObj, - this.stateObj.attributes.percentage ?? 0 - ); - return html` { class HuiFanSpeedTileFeature extends LitElement implements LovelaceTileFeature { @property({ attribute: false }) public hass?: HomeAssistant; - @property({ attribute: false }) public stateObj?: HassEntity; + @property({ attribute: false }) public stateObj?: FanEntity; @state() private _config?: FanSpeedTileFeatureConfig; @@ -79,6 +81,10 @@ class HuiFanSpeedTileFeature extends LitElement implements LovelaceTileFeature { const speedCount = computeFanSpeedCount(this.stateObj); + const percentage = stateActive(this.stateObj) + ? this.stateObj.attributes.percentage ?? 0 + : 0; + if (speedCount <= FAN_SPEED_COUNT_MAX_FOR_BUTTONS) { const options = FAN_SPEEDS[speedCount]!.map( (speed) => ({ @@ -88,10 +94,7 @@ class HuiFanSpeedTileFeature extends LitElement implements LovelaceTileFeature { }) ); - const speed = fanPercentageToSpeed( - this.stateObj, - this.stateObj.attributes.percentage ?? 0 - ); + const speed = fanPercentageToSpeed(this.stateObj, percentage); return html`
@@ -113,15 +116,12 @@ class HuiFanSpeedTileFeature extends LitElement implements LovelaceTileFeature { `; } - const percentage = - this.stateObj.attributes.percentage != null - ? Math.max(Math.round(this.stateObj.attributes.percentage), 0) - : undefined; + const value = Math.max(Math.round(percentage), 0); return html`