mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 01:38:02 +00:00
Avoid fetching state and charging state multiple time for hkc icon (#98995)
This commit is contained in:
parent
3f2d2a85b7
commit
d79e8b7a02
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user