Avoid fetching state and charging state multiple time for hkc icon (#98995)

This commit is contained in:
J. Nick Koston 2023-08-25 06:35:31 -05:00 committed by GitHub
parent 3f2d2a85b7
commit d79e8b7a02
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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