iOS/Sources/Extensions/Widgets/Scene/Control
Penait1 8dd9117cbe
Preserve order getEntities function (#3108)
<!-- Thank you for submitting a Pull Request and helping to improve Home
Assistant. Please complete the following sections to help the processing
and review of your changes. Please do not delete anything from this
template. -->

## Summary
Changed the implementation of `getEntities(matching string: String? =
nil)` to return an array of key values pairs instead of a dictioniary.
The intent of the function seems to return a sorted list of entities
grouped by server name since the servers are sorted
`Current.servers.all.sorted(by: { $0.info.name < $1.info.name })`. A
dictioniary does not guarantee order, resulting in unpredicting results
in my `Sensor` widget configure window. Sometimes one server is first,
the next time the other.

Apple also recommends using a KeyValuePair when order is important:

https://developer.apple.com/documentation/swift/keyvaluepairs
`Use a KeyValuePairs instance when you need an ordered collection of
key-value pairs and don’t require the fast key lookup that
the Dictionary type provides.`

Because there are typically (I think?) not many servers configured the
loss of fast lookup seems acceptable to me.

## Screenshots
Here an overview of the `getEntities` function while debugging showing
the problem. I adjusted the function slightly to temp store the values,
`servers` contains the value of `Current.servers.all.sorted(by: {
$0.info.name < $1.info.name })` The entitiesPerSever does not have the
same order as the `servers` variable, while they should have

![Screenshot 2024-10-29 at 17 16
04](https://github.com/user-attachments/assets/3ff0620b-2a26-45aa-800f-7fb3093727c6)

## Any other notes
This is also an issue for other widgets like the script widget. I only
fixed the function in `IntentSensorsAppEntity` though
, they have the same problem in the calling function because they return
an dictionary not preserving order;
`private func getScriptEntities(matching string: String? = nil) ->
[Server: [IntentScriptEntity]] {`
2024-10-31 14:22:52 +01:00
..
ControlScene.swift Fix control scene id when calling to activate scene (#3007) 2024-09-16 23:43:31 +00:00
ControlScenesValueProvider.swift Update 'confirmation notification' copy (#3028) 2024-09-19 14:38:47 +02:00
IntentSceneEntity.swift Preserve order getEntities function (#3108) 2024-10-31 14:22:52 +01:00