Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Badges - Show timestamp the same way as timer #21298

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 39 additions & 5 deletions src/components/entity/ha-state-label-badge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { timerTimeRemaining } from "../../data/timer";
import { HomeAssistant } from "../../types";
import "../ha-label-badge";
import "../ha-state-icon";
import { SENSOR_DEVICE_CLASS_TIMESTAMP } from "../../data/sensor";

// Define the domains whose states have special truncated strings
const TRUNCATED_DOMAINS = [
Expand Down Expand Up @@ -180,6 +181,11 @@ export class HaStateLabelBadge extends LitElement {
if (entry?.platform === "moon") {
return null;
}
if (
entityState.attributes.device_class === SENSOR_DEVICE_CLASS_TIMESTAMP
) {
return null;
}
// eslint-disable-next-line: disable=no-fallthrough
default:
return entityState.state === UNKNOWN ||
Expand Down Expand Up @@ -214,7 +220,10 @@ export class HaStateLabelBadge extends LitElement {
case "timer":
return true;
case "sensor":
return entry?.platform === "moon";
return (
entry?.platform === "moon" ||
entityState.attributes.device_class === SENSOR_DEVICE_CLASS_TIMESTAMP
);
default:
return false;
}
Expand All @@ -240,6 +249,12 @@ export class HaStateLabelBadge extends LitElement {
if (domain === "timer") {
return secondsToDuration(_timerTimeRemaining);
}
if (
domain === "sensor" &&
entityState.attributes.device_class === SENSOR_DEVICE_CLASS_TIMESTAMP
) {
return secondsToDuration(_timerTimeRemaining);
}
return entityState.attributes.unit_of_measurement || null;
}

Expand All @@ -252,12 +267,25 @@ export class HaStateLabelBadge extends LitElement {

private startInterval(stateObj) {
this.clearInterval();
if (stateObj && computeStateDomain(stateObj) === "timer") {
this.calculateTimerRemaining(stateObj);
if (stateObj) {
const domain = computeStateDomain(stateObj);
if (domain === "timer") {
this.calculateTimerRemaining(stateObj);

if (stateObj.state === "active") {
this._updateRemaining = window.setInterval(
() => this.calculateTimerRemaining(this.state),
1000
);
}
} else if (
domain === "sensor" &&
stateObj.attributes.device_class === SENSOR_DEVICE_CLASS_TIMESTAMP
) {
this.calculateTimestampRemaining(stateObj);

if (stateObj.state === "active") {
this._updateRemaining = window.setInterval(
() => this.calculateTimerRemaining(this.state),
() => this.calculateTimestampRemaining(this.state),
1000
);
}
Expand All @@ -268,6 +296,12 @@ export class HaStateLabelBadge extends LitElement {
this._timerTimeRemaining = timerTimeRemaining(stateObj);
}

private calculateTimestampRemaining(stateObj) {
const now = new Date().getTime();
const value = new Date(stateObj.state).getTime();
this._timerTimeRemaining = Math.max((value - now) / 1000, 0);
}

static get styles(): CSSResultGroup {
return css`
:host {
Expand Down
Loading