mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +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
|
@property
|
||||||
def icon(self) -> str:
|
def icon(self) -> str:
|
||||||
"""Return the sensor icon."""
|
"""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"
|
return "mdi:battery-unknown"
|
||||||
|
|
||||||
# This is similar to the logic in helpers.icon, but we have delegated the
|
# This is similar to the logic in helpers.icon, but we have delegated the
|
||||||
# decision about what mdi:battery-alert is to the device.
|
# decision about what mdi:battery-alert is to the device.
|
||||||
icon = "mdi:battery"
|
icon = "mdi:battery"
|
||||||
if self.is_charging and self.state > 10:
|
is_charging = self.is_charging
|
||||||
percentage = int(round(self.state / 20 - 0.01)) * 20
|
if is_charging and native_value > 10:
|
||||||
|
percentage = int(round(native_value / 20 - 0.01)) * 20
|
||||||
icon += f"-charging-{percentage}"
|
icon += f"-charging-{percentage}"
|
||||||
elif self.is_charging:
|
elif is_charging:
|
||||||
icon += "-outline"
|
icon += "-outline"
|
||||||
elif self.is_low_battery:
|
elif self.is_low_battery:
|
||||||
icon += "-alert"
|
icon += "-alert"
|
||||||
elif self.state < 95:
|
elif native_value < 95:
|
||||||
percentage = max(int(round(self.state / 10 - 0.01)) * 10, 10)
|
percentage = max(int(round(native_value / 10 - 0.01)) * 10, 10)
|
||||||
icon += f"-{percentage}"
|
icon += f"-{percentage}"
|
||||||
|
|
||||||
return icon
|
return icon
|
||||||
|
Loading…
x
Reference in New Issue
Block a user