-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Allow customization of time format #22339
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,10 @@ import type { UpdateEntity } from "../data/update"; | |
import { computeUpdateStateDisplay } from "../data/update"; | ||
import "../panels/lovelace/components/hui-timestamp-display"; | ||
import type { HomeAssistant } from "../types"; | ||
import { | ||
TIMESTAMP_RENDERING_FORMATS, | ||
type TimestampRenderingFormat, | ||
} from "../panels/lovelace/components/types"; | ||
|
||
const TIMESTAMP_STATE_DOMAINS = ["button", "input_button", "scene"]; | ||
|
||
|
@@ -59,6 +63,8 @@ class StateDisplay extends LitElement { | |
|
||
@property({ attribute: false }) public name?: string; | ||
|
||
@property({ attribute: false }) public format?: TimestampRenderingFormat; | ||
|
||
@property({ type: Boolean, attribute: "dash-unavailable" }) | ||
public dashUnavailable?: boolean; | ||
|
||
|
@@ -77,7 +83,10 @@ class StateDisplay extends LitElement { | |
const stateObj = this.stateObj; | ||
const domain = computeStateDomain(stateObj); | ||
|
||
if (content === "state") { | ||
if ( | ||
content === "state" || | ||
(this.format && TIMESTAMP_RENDERING_FORMATS.includes(this.format)) | ||
) { | ||
if (this.dashUnavailable && isUnavailableState(stateObj.state)) { | ||
return "—"; | ||
} | ||
|
@@ -90,7 +99,7 @@ class StateDisplay extends LitElement { | |
<hui-timestamp-display | ||
.hass=${this.hass} | ||
.ts=${new Date(stateObj.state)} | ||
format="relative" | ||
.format=${this.format} | ||
capitalize | ||
></hui-timestamp-display> | ||
`; | ||
|
@@ -133,6 +142,7 @@ class StateDisplay extends LitElement { | |
<ha-relative-time | ||
.hass=${this.hass} | ||
.datetime=${relativeDateTime} | ||
.format=${this.format} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would not be used ever, as we already would have returned the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have to check this, but I'm pretty sure I saw this being reached at some point during my testing. I'll investigate a bit more and provide any pertinent data. |
||
capitalize | ||
></ha-relative-time> | ||
`; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we can do this, as relative time is also used for things like
last_triggered
, etc.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it was something I threw in somewhat last minute when I saw the alternative
state_content
timestamps weren't respecting the format. Would adding more specific guards for the other state content types be an acceptable alternative?