Skip to content

Commit

Permalink
Avoid unecessary subscription while fetching user avatar (#3281)
Browse files Browse the repository at this point in the history
<!-- 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 -->

## 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. -->
  • Loading branch information
bgoncal authored Dec 17, 2024
1 parent 469838a commit afbb0a4
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions Sources/App/Settings/Eureka/AccountRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ final class HomeAssistantAccountRow: Row<AccountCell>, RowType {

enum FetchAvatarError: Error, CancellableError {
case missingPerson
case missingURL
case missingURLForUserEntityPicture
case alreadySet
case couldntDecode

Expand All @@ -189,7 +189,7 @@ final class HomeAssistantAccountRow: Row<AccountCell>, RowType {
return
}

accountSubscription = api.connection.caches.user.subscribe { [weak self] _, user in
accountSubscription = api.connection.caches.user.once { [weak self] user in
guard let self else { return }
Current.Log.verbose("got user from user \(user)")
cachedUserName = user.name
Expand All @@ -202,7 +202,7 @@ final class HomeAssistantAccountRow: Row<AccountCell>, RowType {
}
}

avatarSubscription = api.connection.caches.states().subscribe { [weak self] _, states in
avatarSubscription = api.connection.caches.states().once { [weak self] states in
firstly { () -> Guarantee<Set<HAEntity>> in
Guarantee.value(states.all)
}.map { states throws -> HAEntity in
Expand All @@ -215,7 +215,7 @@ final class HomeAssistantAccountRow: Row<AccountCell>, RowType {
if let urlString = entity.attributes["entity_picture"] as? String {
return urlString
} else {
throw FetchAvatarError.missingURL
throw FetchAvatarError.missingURLForUserEntityPicture
}
}.map { path throws -> URL in
guard let url = server.info.connection.activeURL()?.appendingPathComponent(path) else {
Expand Down

0 comments on commit afbb0a4

Please sign in to comment.