-
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
Sort related automations, scripts and scenes on device info page #23740
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import type { HassEntity } from "home-assistant-js-websocket"; | ||
import type { HomeAssistant } from "../types"; | ||
import { caseInsensitiveStringCompare } from "../common/string/compare"; | ||
|
||
export interface RelatedResult { | ||
area?: string[]; | ||
|
@@ -35,13 +37,30 @@ export type ItemType = | |
| "automation_blueprint" | ||
| "script_blueprint"; | ||
|
||
export const findRelated = ( | ||
export const findRelated = async ( | ||
hass: HomeAssistant, | ||
itemType: ItemType, | ||
itemId: string | ||
): Promise<RelatedResult> => | ||
hass.callWS({ | ||
): Promise<RelatedResult> => { | ||
const related = await hass.callWS<RelatedResult>({ | ||
type: "search/related", | ||
item_type: itemType, | ||
item_id: itemId, | ||
}); | ||
|
||
Object.keys(related).forEach((key) => { | ||
related[key] = related[key] | ||
.map((id: string) => hass.states[id]) | ||
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. Not all related items are entities, so this does not makes sense. Only for scenes, scripts, automations and entity it makes sense. |
||
.filter(Boolean) | ||
.sort((a: HassEntity, b: HassEntity) => | ||
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. If an entity name is changed after we fetched related, it will not update the sorting, that is kinda weird. |
||
caseInsensitiveStringCompare( | ||
a.attributes.friendly_name ?? a.entity_id, | ||
b.attributes.friendly_name ?? b.entity_id, | ||
hass.language | ||
) | ||
) | ||
.map((entity: HassEntity) => entity.entity_id); | ||
}); | ||
|
||
return related; | ||
}; |
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 change will defeat the purpose of MemoizeOne here. You can read about MemoizeOne here: https://www.npmjs.com/package/memoize-one#usage
Previously, we had individual MemoizeOne on entities, groups, automations, scripts, ...
So, if the related scripts don't change, memoize will return the cache result fast upon each rendering.
With these changes, only one MemoizeOne remains and since we fetch related entities, groups, automations, scripts and so on in sequence, it will never be able to return the cache.
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 would make this 1 memoized function, that returns all the sorted and filtered entities: