5 Commits

Author SHA1 Message Date
Bruno Pantaleão Gonçalves
94fe0c568f
Add more domains for sensors widget (#3270)
<!-- 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
<!-- Provide a brief summary of the changes you have made and most
importantly what they aim to achieve -->
CC: @Penait1 
## Screenshots
<!-- If this is a user-facing change not in the frontend, please include
screenshots in light and dark mode. -->

## Link to pull request in Documentation repository
<!-- Pull requests that add, change or remove functionality must have a
corresponding pull request in the Companion App Documentation repository
(https://github.com/home-assistant/companion.home-assistant). Please add
the number of this pull request after the "#" -->
Documentation: home-assistant/companion.home-assistant#

## Any other notes
<!-- If there is any other information of note, like if this Pull
Request is part of a bigger change, please include it here. -->
2024-12-12 18:08:11 +01:00
Penait1
c2cd22b884
Change sensor widget view (#3126) 2024-11-06 23:10:22 +01:00
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
Penait1
8159d3ff6c
Group by server, widget selection order (#3102)
## Summary
Fixed two issues mentioned in #3067;

Sensors in the suggested sensors view should be grouped by server now
The order of the selected sensors is now respected if they come from two
different servers

## Screenshots
![Screenshot 2024-10-27 at 08 06
20](https://github.com/user-attachments/assets/fe4ae23c-013d-49b1-a3dc-6fd12ae46bfb)

## Any other notes
The `for` loop bothers me, Swift feels a lot like Kotlin and a `map`
seems more appriopiate.
Because an async call happens in it I could not get `map` working that
is nice to read, let me know if there is a better option chatgpt gave me
this

```
let sensorValues = try await withThrowingTaskGroup(of: WidgetSensorsEntry.SensorData?.self) { group in
    for sensor in configuration.sensors ?? [] {
        group.addTask {
            guard let server = Current.servers.all.first(where: { $0.identifier.rawValue == sensor.serverId }) else {
                throw WidgetSensorsDataError.noServers
            }
            return try await fetchSensorData(for: sensor, server: server)
        }
    }

    return try await group.compactMap { $0 }
}
```
2024-10-28 09:49:49 +01:00
Penait1
23d49d81e2
Sensor State Widget (#3067) 2024-10-25 16:09:24 +02:00