Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions Sources/Shared/HAEntity+CarPlay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ public extension HAEntity {
}

func getIcon() -> UIImage? {
var image = MaterialDesignIcons.bookmarkIcon
let image = getMDI()
var tint: UIColor?

let icon = getMDI()

if let state = Domain.State(rawValue: state) {
if [.on, .open, .opening, .unlocked, .unlocking].contains(state) {
tint = AppConstants.lighterTintColor
Expand Down
56 changes: 56 additions & 0 deletions Tests/Shared/HAEntityCarPlay.test.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import HAKit
@testable import Shared
import Testing
import UIKit

@MainActor
struct HAEntityCarPlayTests {
@Test("Given an entity without a custom icon when requesting a CarPlay icon then it uses the domain/state icon")
func usesDomainAndStateBasedIconWhenNoCustomIconExists() throws {
let entity = try HAEntity(
entityId: "light.kitchen",
state: Domain.State.off.rawValue,
lastChanged: Date(),
lastUpdated: Date(),
attributes: [
"friendly_name": "Kitchen light",
],
context: .init(id: "context", userId: "user", parentId: nil)
)

let actualData = try #require(entity.getIcon()?.pngData())
let expectedData = try #require(
MaterialDesignIcons.lightbulbIcon
.carPlayIcon(color: .lightGray)
.pngData()
)

#expect(actualData == expectedData)
}

@Test(
"Given an entity with a custom icon when requesting a CarPlay icon then it preserves that icon before falling back"
)
func usesCustomAttributeIconBeforeFallbacks() throws {
let entity = try HAEntity(
entityId: "sensor.temperature",
state: "23",
lastChanged: Date(),
lastUpdated: Date(),
attributes: [
"friendly_name": "Temperature",
"icon": "mdi:thermometer",
],
context: .init(id: "context", userId: "user", parentId: nil)
)

let actualData = try #require(entity.getIcon()?.pngData())
let expectedData = try #require(
MaterialDesignIcons.thermometerIcon
.carPlayIcon()
.pngData()
)

#expect(actualData == expectedData)
}
}
Loading