From d79e8b7a029d268ac9dcd0808fcc2e42b6f892b8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 25 Aug 2023 06:35:31 -0500 Subject: [PATCH] Avoid fetching state and charging state multiple time for hkc icon (#98995) --- .../components/homekit_controller/sensor.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/homekit_controller/sensor.py b/homeassistant/components/homekit_controller/sensor.py index d7230de0832..5803b8aa839 100644 --- a/homeassistant/components/homekit_controller/sensor.py +++ b/homeassistant/components/homekit_controller/sensor.py @@ -466,21 +466,23 @@ class HomeKitBatterySensor(HomeKitSensor): @property def icon(self) -> str: """Return the sensor icon.""" - if not self.available or self.state is None: + native_value = self.native_value + if not self.available or native_value is None: return "mdi:battery-unknown" # This is similar to the logic in helpers.icon, but we have delegated the # decision about what mdi:battery-alert is to the device. icon = "mdi:battery" - if self.is_charging and self.state > 10: - percentage = int(round(self.state / 20 - 0.01)) * 20 + is_charging = self.is_charging + if is_charging and native_value > 10: + percentage = int(round(native_value / 20 - 0.01)) * 20 icon += f"-charging-{percentage}" - elif self.is_charging: + elif is_charging: icon += "-outline" elif self.is_low_battery: icon += "-alert" - elif self.state < 95: - percentage = max(int(round(self.state / 10 - 0.01)) * 10, 10) + elif native_value < 95: + percentage = max(int(round(native_value / 10 - 0.01)) * 10, 10) icon += f"-{percentage}" return icon