-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Fixed Logbook Card/Panel/Dialog Incorrect Entires for input_datetime
Entities
#9399
Fixed Logbook Card/Panel/Dialog Incorrect Entires for input_datetime
Entities
#9399
Conversation
…n explicit input_datetime state.
…input_datetime state display.
// When only date is passed to `Date` constructor, it uses UTC regardless of frontend's timezone, | ||
// so we need to add the frontend's timezone offset. | ||
// Other usages of `Date` constructor have both date and time in the string, frontend's timezone is used w/o problem. | ||
const timezoneOffset = new Date().getTimezoneOffset() * 60 * 1000; | ||
const adjustedDateObj = new Date(dateObj.getTime() + timezoneOffset); |
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.
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.
So the issue here is that we don't have the attributes in the logbook? |
const now = new Date(); | ||
date = new Date( |
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.
Instead of creating a new date, we can just set the hour and minute of now?
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.
This is done.
// If trying to display an explicit state, need to parse the explict state to `Date` then format. | ||
// Attributes aren't available, we have to use `state`. | ||
try { | ||
if (!stateObj.attributes.has_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.
If attributes are not available, how can we do this?
try { | ||
if (!stateObj.attributes.has_time) { | ||
// Only has date. | ||
const dateObj = new Date(state); |
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 think we should make the state
string a valid ISO 8601 string, by adding a date or time, then we don't have to struggle with all these issues, and don't have to rely on the current attributes.
@bramkragten thanks for taking a look at this PR!
Yes, we only added
We are getting
This code exists prior to this PR, I only changed indentation. But yes, I think we can, I can try it.
To the 3 comments above, I think you are making good points. There seems to be too many issues and hacks when we try to create a 2 questions on this direction: For making state a ISO 8601 stringDo we make core store the Or we add logic to core logbook component to process Or do we do it on frontend? We would do something like these?:
For
|
Yes, I think the frontend should create a ISO 8601 string. We don't need to use the About timezone: |
@bramkragten gotcha, thanks for the advice! I will go with this direction see how it works out. For timezone: doesn't the letter Thanks again. |
@bramkragten Finished refactoring to what we discussed. Let me know if I've misunderstood anything or anything can be improved. |
const components = state.split(" "); | ||
if (components.length === 2) { | ||
// Date and time. | ||
const iso8601String = components.join("T") + "Z"; |
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.
If you leave the Z
of, you don't need the timezoneOffset
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 thought Z
is required in ISO8601 spec, even after reading some documents, it's still vague.
But I verified it works without Z
on both Chrome and Safari, I will remove it. Thanks for the reminder!
// Inserting today's Date string. | ||
const now = new Date(); | ||
const dateISO8601String = | ||
now.toISOString().substring(0, 10) + "T" + state + "Z"; |
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.
Same here, no need for timezoneOffset
when Z
is dropped
const dateObj = new Date(state); | ||
const adjustedDateObj = new Date( | ||
dateObj.getTime() + timezoneOffset | ||
); | ||
return formatDate(adjustedDateObj, locale); |
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.
This will work, but
const dateObj = new Date(`${state}T00:00:00`);
will work too.
// Inserting today's Date string. | ||
const now = new Date(); | ||
const dateISO8601String = | ||
now.toISOString().substring(0, 10) + "T" + state + "Z"; |
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.
now.toISOString().substring(0, 10) + "T" + state + "Z"; | |
`${now.toISOString().split("T")[0]}T${state}; |
@bramkragten I resolved the last 4 comments. I'm amazed how simple this ended up with, thank you! |
Co-authored-by: Bram Kragten <[email protected]>
Proposed change
The issue (in the video, although I changed the input_datetime to different dates, logbook dialog's every entry shows the same date):
Kapture.2021-06-09.at.20.12.20.mp4
The issue under the hood is:
Back in #6978 we added a parameter
state
tocomputeStateDisplay
in order to compute the display of an explicit state (instead of the entity's current state). But theinput_datetime
handling wasn't changed properly.The
input_datetime
handling should try to format the passed in explicitstate
if it exists.This PR added the handling for passed in explicit input_datetime
state
.Type of change
Example configuration
N/A
Additional information
input_datetime
Entities #9405Checklist
If user exposed functionality or configuration variables are added/changed: