-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Order automation devices in pickers based on automation usage #27477
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
victorigualada
wants to merge
11
commits into
home-assistant:dev
Choose a base branch
from
victorigualada:order-automation-devices-in-pickers
base: dev
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.
+200
−2
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1e010a1
show first used automation devices in the device pickers
victorigualada 4381208
move context to automation sections and pass down value
victorigualada 697ccc9
extract duplicated logic to helper function
victorigualada 1c6b2d5
rename helper reorder function and replace assignement
victorigualada be52ab5
merge upstream
victorigualada c686917
update dependency
victorigualada d864a47
remove unneeded search and reorder functions
victorigualada 461a852
Merge branch 'order-automation-devices-in-pickers' of github.com:vict…
victorigualada 394ea46
ensure we don't re-render if devices didn't change in the automation
victorigualada 393be55
Merge branch 'dev' into order-automation-devices-in-pickers
victorigualada 81e89d0
mege upstream and fix conflicts
victorigualada 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
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
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,137 @@ | ||
| import { createContext } from "@lit/context"; | ||
| import type { HomeAssistant } from "../types"; | ||
| import type { AutomationConfig } from "./automation"; | ||
| import { ensureArray } from "../common/array/ensure-array"; | ||
|
|
||
| export type AutomationSection = "triggers" | "conditions" | "actions"; | ||
|
|
||
| export type EntityId = string; | ||
| export type DeviceId = string; | ||
| export type AreaId = string; | ||
| export type Domain = string; | ||
|
|
||
| export interface AutomationLocalContext { | ||
| meta: { | ||
| automationId?: string; | ||
| signature: string; | ||
| }; | ||
|
|
||
| used: { | ||
| entities: EntityId[]; | ||
| devices: DeviceId[]; | ||
| areas: AreaId[]; | ||
| domains: Domain[]; | ||
| }; | ||
|
|
||
| maps: { | ||
| entityArea: Record<EntityId, AreaId | null>; | ||
| entityDevice: Record<EntityId, DeviceId | null>; | ||
| deviceArea: Record<DeviceId, AreaId | null>; | ||
| deviceDomains: Record<DeviceId, Domain[]>; | ||
| }; | ||
|
|
||
| weights?: { | ||
| used: number; | ||
| sameDomain: number; | ||
| sameArea: number; | ||
| }; | ||
|
|
||
| hints?: { | ||
| lastEditedPath?: string[] | number[]; | ||
| labels?: string[]; | ||
| services?: string[]; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Context key for the automation editor locality context. | ||
| */ | ||
| export const automationEditorContext = createContext< | ||
| AutomationLocalContext | undefined | ||
| >("automationEditorContext"); | ||
|
|
||
| const addIds = (ids: unknown, set: Set<string>) => { | ||
| if (ids === undefined || ids === null) return; | ||
| const list = ensureArray(ids) as unknown[]; | ||
| for (const item of list) { | ||
| if (typeof item === "string" && item) { | ||
| set.add(item); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| const getContextSignature = (devices: Set<string>): string => | ||
| Array.from(devices.values()) | ||
| .map((d) => d.slice(0, 6)) | ||
| .sort() | ||
| .join(); | ||
|
|
||
| export const buildAutomationLocalContext = ( | ||
| config: AutomationConfig | undefined, | ||
| _hass: HomeAssistant, | ||
| automationId?: string, | ||
| previous?: AutomationLocalContext | ||
| ): AutomationLocalContext | undefined => { | ||
| if (!config) return undefined; | ||
|
|
||
| const devicesSet = new Set<string>(); | ||
|
|
||
| const scan = (val: any) => { | ||
| if (val === null || val === undefined) return; | ||
| if (Array.isArray(val)) { | ||
| for (const item of val) scan(item); | ||
| return; | ||
| } | ||
| if (typeof val !== "object") return; | ||
|
|
||
| if ("device_id" in val) { | ||
| addIds((val as any).device_id, devicesSet); | ||
| } | ||
| if ("target" in val && val.target && typeof val.target === "object") { | ||
| if ("device_id" in (val.target as any)) { | ||
| addIds((val.target as any).device_id, devicesSet); | ||
| } | ||
| } | ||
|
|
||
| for (const key of Object.keys(val)) { | ||
| const child = (val as any)[key]; | ||
| if (child && typeof child === "object") { | ||
| scan(child); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| scan(config.triggers); | ||
| scan(config.conditions); | ||
| scan(config.actions); | ||
|
|
||
| // Avoid re-rendering if nothing changed | ||
| const signature = getContextSignature(devicesSet); | ||
| if (previous && previous.meta.signature === signature) { | ||
| return previous; | ||
| } | ||
|
|
||
| return { | ||
| meta: { | ||
| automationId, | ||
| signature, | ||
| }, | ||
| used: { | ||
| entities: [], | ||
| devices: Array.from(devicesSet), | ||
| areas: [], | ||
| domains: [], | ||
| }, | ||
| maps: { | ||
| entityArea: {}, | ||
| entityDevice: {}, | ||
| deviceArea: Object.fromEntries( | ||
| Object.entries(_hass.devices || {}).map(([id, dev]) => [ | ||
| id, | ||
| dev.area_id || null, | ||
| ]) | ||
| ), | ||
| deviceDomains: {}, | ||
| }, | ||
| }; | ||
| }; |
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
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
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
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
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
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.
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 like to prevent this from re-creating everything when the automation changes, as it will trigger the other memoized functions to also recalculate. Can we make sure to only update those parts that actually changed? So if no devices changed, we don't recreate
used.devices.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 added a signature to the context that we can compare on change updates to not update the context object.