-
Notifications
You must be signed in to change notification settings - Fork 53
fix: use compact datetime rows with edit affordance for date range #592
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
Open
tykeal
wants to merge
6
commits into
FutureTense:main
Choose a base branch
from
tykeal:fix-datetime-layout
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3b11cb4
fix: use compact datetime rows with edit affordance for date range
tykeal 523d1f2
fix: use locale-aware datetime formatting and address review feedback
tykeal daa7342
fix: re-render on locale changes and fix stale comments
tykeal 4d3c4f0
fix: add date_format support, action handler, and fix flaky tests
tykeal a3edeb6
fix: conditionally show pencil icon and remove unused action types
tykeal 628a608
fix: use formatToParts for locale-safe date formatting
tykeal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,282 @@ | ||
| import { beforeAll, describe, expect, it, vi } from 'vitest'; | ||
| import { HomeAssistant } from './ha_type_stubs'; | ||
| import { KeymasterDatetimeRow } from './datetime-row'; | ||
|
|
||
| // Mock hui-generic-entity-row since it only exists inside HA's Lovelace runtime. | ||
| class MockGenericEntityRow extends HTMLElement { | ||
| private _hass: unknown; | ||
| private _config: unknown; | ||
|
|
||
| set hass(v: unknown) { | ||
| this._hass = v; | ||
| } | ||
| get hass(): unknown { | ||
| return this._hass; | ||
| } | ||
|
|
||
| set config(v: unknown) { | ||
| this._config = v; | ||
| } | ||
| get config(): unknown { | ||
| return this._config; | ||
| } | ||
|
|
||
| connectedCallback(): void { | ||
| if (!this.shadowRoot) { | ||
| this.attachShadow({ mode: 'open' }); | ||
| this.shadowRoot!.innerHTML = '<slot></slot>'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| beforeAll(() => { | ||
| if (!customElements.get('hui-generic-entity-row')) { | ||
| customElements.define('hui-generic-entity-row', MockGenericEntityRow); | ||
| } | ||
| if (!customElements.get('keymaster-datetime-row')) { | ||
| customElements.define('keymaster-datetime-row', KeymasterDatetimeRow); | ||
| } | ||
| }); | ||
|
|
||
| function createMockHass( | ||
| states: Record<string, { state: string; attributes: Record<string, unknown> }> = {} | ||
| ): HomeAssistant { | ||
| return { | ||
| callWS: vi.fn(), | ||
| config: { state: 'RUNNING' }, | ||
| states, | ||
| } as unknown as HomeAssistant; | ||
| } | ||
|
|
||
| function createElement(): KeymasterDatetimeRow { | ||
| return document.createElement('keymaster-datetime-row') as KeymasterDatetimeRow; | ||
| } | ||
|
|
||
| describe('KeymasterDatetimeRow', () => { | ||
| describe('setConfig', () => { | ||
| it('throws if no entity is provided', () => { | ||
| const el = createElement(); | ||
| expect(() => el.setConfig({} as never)).toThrow('Entity is required'); | ||
| }); | ||
|
|
||
| it('stores the config', () => { | ||
| const el = createElement(); | ||
| const config = { entity: 'datetime.test' }; | ||
| el.setConfig(config); | ||
| // Verify indirectly by ensuring render doesn't throw when hass is set | ||
| el.hass = createMockHass({ | ||
| 'datetime.test': { | ||
| state: '2026-04-03T14:30:00+00:00', | ||
| attributes: { friendly_name: 'Test' }, | ||
| }, | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('render', () => { | ||
| it('renders hui-generic-entity-row with correct hass and config', async () => { | ||
| const el = createElement(); | ||
| const config = { entity: 'datetime.test' }; | ||
| el.setConfig(config); | ||
| const hass = createMockHass({ | ||
| 'datetime.test': { | ||
| state: '2026-04-03T00:00:00+00:00', | ||
| attributes: { friendly_name: 'Date Range Start' }, | ||
| }, | ||
| }); | ||
| el.hass = hass; | ||
|
|
||
| document.body.appendChild(el); | ||
| await el.updateComplete; | ||
|
|
||
| const shadow = el.shadowRoot!; | ||
| const genericRow = shadow.querySelector( | ||
| 'hui-generic-entity-row' | ||
| ) as MockGenericEntityRow; | ||
| expect(genericRow).not.toBeNull(); | ||
| expect(genericRow.hass).toBe(hass); | ||
| expect(genericRow.config).toEqual(config); | ||
|
|
||
| document.body.removeChild(el); | ||
| }); | ||
|
|
||
| it('renders state text and pencil icon in slot', async () => { | ||
| const utcStr = '2026-04-03T00:00:00+00:00'; | ||
| const d = new Date(utcStr); | ||
| const expected = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')} ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`; | ||
|
|
||
| const el = createElement(); | ||
| el.setConfig({ entity: 'datetime.test' }); | ||
| el.hass = createMockHass({ | ||
| 'datetime.test': { | ||
| state: utcStr, | ||
| attributes: { friendly_name: 'Date Range Start' }, | ||
| }, | ||
| }); | ||
|
|
||
| document.body.appendChild(el); | ||
| await el.updateComplete; | ||
|
|
||
| const shadow = el.shadowRoot!; | ||
| expect(shadow.querySelector('.state')?.textContent).toBe(expected); | ||
| expect(shadow.querySelector('ha-icon')).not.toBeNull(); | ||
| expect(shadow.querySelector('ha-icon')?.getAttribute('icon')).toBe('mdi:pencil'); | ||
|
|
||
| document.body.removeChild(el); | ||
| }); | ||
|
|
||
| it('shows hui-warning for missing entity', async () => { | ||
| const el = createElement(); | ||
| el.setConfig({ entity: 'datetime.missing' }); | ||
| el.hass = createMockHass({}); | ||
|
|
||
| document.body.appendChild(el); | ||
| await el.updateComplete; | ||
|
|
||
| const shadow = el.shadowRoot!; | ||
| const warning = shadow.querySelector('hui-warning'); | ||
| expect(warning).not.toBeNull(); | ||
| expect(warning?.textContent).toContain('datetime.missing'); | ||
|
|
||
| document.body.removeChild(el); | ||
| }); | ||
| }); | ||
|
|
||
| describe('_formatState (via render)', () => { | ||
| it('formats ISO datetime state correctly', async () => { | ||
| const utcStr = '2026-04-03T00:00:00+00:00'; | ||
| const d = new Date(utcStr); | ||
| const expected = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')} ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`; | ||
|
|
||
| const el = createElement(); | ||
| el.setConfig({ entity: 'datetime.test' }); | ||
| el.hass = createMockHass({ | ||
| 'datetime.test': { | ||
| state: utcStr, | ||
| attributes: { friendly_name: 'Test' }, | ||
| }, | ||
| }); | ||
|
|
||
| document.body.appendChild(el); | ||
| await el.updateComplete; | ||
|
|
||
| expect(el.shadowRoot!.querySelector('.state')?.textContent).toBe(expected); | ||
|
|
||
| document.body.removeChild(el); | ||
| }); | ||
|
|
||
| it('converts UTC datetime to local timezone', async () => { | ||
| const utcStr = '2026-04-03T23:00:00+00:00'; | ||
| const d = new Date(utcStr); | ||
| const expected = `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')} ${String(d.getHours()).padStart(2, '0')}:${String(d.getMinutes()).padStart(2, '0')}`; | ||
|
|
||
| const el = createElement(); | ||
| el.setConfig({ entity: 'datetime.test' }); | ||
| el.hass = createMockHass({ | ||
| 'datetime.test': { | ||
| state: utcStr, | ||
| attributes: { friendly_name: 'Test' }, | ||
| }, | ||
| }); | ||
|
|
||
| document.body.appendChild(el); | ||
| await el.updateComplete; | ||
|
|
||
| const stateText = el.shadowRoot!.querySelector('.state')?.textContent; | ||
| expect(stateText).toBe(expected); | ||
| // In non-UTC timezones, verify it does NOT just regex-extract the UTC values | ||
| if (d.getTimezoneOffset() !== 0) { | ||
| expect(stateText).not.toBe('2026-04-03 23:00'); | ||
| } | ||
|
|
||
| document.body.removeChild(el); | ||
| }); | ||
|
|
||
| it('shows "unknown" for unknown state', async () => { | ||
| const el = createElement(); | ||
| el.setConfig({ entity: 'datetime.test' }); | ||
| el.hass = createMockHass({ | ||
| 'datetime.test': { | ||
| state: 'unknown', | ||
| attributes: { friendly_name: 'Test' }, | ||
| }, | ||
| }); | ||
|
|
||
| document.body.appendChild(el); | ||
| await el.updateComplete; | ||
|
|
||
| expect(el.shadowRoot!.querySelector('.state')?.textContent).toBe('unknown'); | ||
|
|
||
| document.body.removeChild(el); | ||
| }); | ||
|
|
||
| it('shows "unavailable" for unavailable state', async () => { | ||
| const el = createElement(); | ||
| el.setConfig({ entity: 'datetime.test' }); | ||
| el.hass = createMockHass({ | ||
| 'datetime.test': { | ||
| state: 'unavailable', | ||
| attributes: { friendly_name: 'Test' }, | ||
| }, | ||
| }); | ||
|
|
||
| document.body.appendChild(el); | ||
| await el.updateComplete; | ||
|
|
||
| expect(el.shadowRoot!.querySelector('.state')?.textContent).toBe('unavailable'); | ||
|
|
||
| document.body.removeChild(el); | ||
| }); | ||
| }); | ||
|
|
||
| describe('shouldUpdate', () => { | ||
| it('returns false when entity state is unchanged', async () => { | ||
| const el = createElement(); | ||
| el.setConfig({ entity: 'datetime.test' }); | ||
|
|
||
| const stateObj = { | ||
| state: '2026-04-03T14:30:00+00:00', | ||
| attributes: { friendly_name: 'Test' }, | ||
| }; | ||
|
|
||
| el.hass = createMockHass({ 'datetime.test': stateObj }); | ||
| document.body.appendChild(el); | ||
| await el.updateComplete; | ||
|
|
||
| // Spy on render | ||
| const renderSpy = vi.spyOn(el as never, 'render'); | ||
|
|
||
| // Set same hass with same state object reference | ||
| el.hass = createMockHass({ 'datetime.test': stateObj }); | ||
| await el.updateComplete; | ||
|
|
||
| // shouldUpdate should have prevented the render | ||
| expect(renderSpy).not.toHaveBeenCalled(); | ||
|
|
||
| renderSpy.mockRestore(); | ||
| document.body.removeChild(el); | ||
| }); | ||
| }); | ||
|
|
||
| describe('styles', () => { | ||
| it('pencil icon uses mdi:pencil', async () => { | ||
| const el = createElement(); | ||
| el.setConfig({ entity: 'datetime.test' }); | ||
| el.hass = createMockHass({ | ||
| 'datetime.test': { | ||
| state: '2026-04-03T14:30:00+00:00', | ||
| attributes: { friendly_name: 'Test' }, | ||
| }, | ||
| }); | ||
|
|
||
| document.body.appendChild(el); | ||
| await el.updateComplete; | ||
|
|
||
| const editIcon = el.shadowRoot!.querySelector('.edit-icon'); | ||
| expect(editIcon).not.toBeNull(); | ||
| expect(editIcon?.getAttribute('icon')).toBe('mdi:pencil'); | ||
|
|
||
| document.body.removeChild(el); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.