mirror of
https://github.com/home-assistant/core.git
synced 2025-11-17 23:10:28 +00:00
Add support for Homekit accessory battery sensors (#26210)
* Add simple battery sensor * Add test for battery sensor based on a real device * Vary icon based on battery state * Add test for battery sensory * Read other battery related states from accessory * Add a device class to the battery sensor * Respect the low battery flag from the device
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
"""Support for Homekit sensors."""
|
||||
from homekit.model.characteristics import CharacteristicsTypes
|
||||
|
||||
from homeassistant.const import TEMP_CELSIUS
|
||||
from homeassistant.const import DEVICE_CLASS_BATTERY, TEMP_CELSIUS
|
||||
|
||||
from . import KNOWN_DEVICES, HomeKitEntity
|
||||
|
||||
@@ -30,7 +30,7 @@ class HomeKitHumiditySensor(HomeKitEntity):
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the device."""
|
||||
return "{} {}".format(super().name, "Humidity")
|
||||
return f"{super().name} Humidity"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
@@ -66,7 +66,7 @@ class HomeKitTemperatureSensor(HomeKitEntity):
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the device."""
|
||||
return "{} {}".format(super().name, "Temperature")
|
||||
return f"{super().name} Temperature"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
@@ -102,7 +102,7 @@ class HomeKitLightSensor(HomeKitEntity):
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the device."""
|
||||
return "{} {}".format(super().name, "Light Level")
|
||||
return f"{super().name} Light Level"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
@@ -138,7 +138,7 @@ class HomeKitCarbonDioxideSensor(HomeKitEntity):
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the device."""
|
||||
return "{} {}".format(super().name, "CO2")
|
||||
return f"{super().name} CO2"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
@@ -159,11 +159,85 @@ class HomeKitCarbonDioxideSensor(HomeKitEntity):
|
||||
return self._state
|
||||
|
||||
|
||||
class HomeKitBatterySensor(HomeKitEntity):
|
||||
"""Representation of a Homekit battery sensor."""
|
||||
|
||||
def __init__(self, *args):
|
||||
"""Initialise the entity."""
|
||||
super().__init__(*args)
|
||||
self._state = None
|
||||
self._low_battery = False
|
||||
self._charging = False
|
||||
|
||||
def get_characteristic_types(self):
|
||||
"""Define the homekit characteristics the entity is tracking."""
|
||||
return [
|
||||
CharacteristicsTypes.BATTERY_LEVEL,
|
||||
CharacteristicsTypes.STATUS_LO_BATT,
|
||||
CharacteristicsTypes.CHARGING_STATE,
|
||||
]
|
||||
|
||||
@property
|
||||
def device_class(self) -> str:
|
||||
"""Return the device class of the sensor."""
|
||||
return DEVICE_CLASS_BATTERY
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the device."""
|
||||
return f"{super().name} Battery"
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the sensor icon."""
|
||||
if not self.available or self.state 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._charging and self.state > 10:
|
||||
percentage = int(round(self.state / 20 - 0.01)) * 20
|
||||
icon += f"-charging-{percentage}"
|
||||
elif self._charging:
|
||||
icon += "-outline"
|
||||
elif self._low_battery:
|
||||
icon += "-alert"
|
||||
elif self.state < 95:
|
||||
percentage = max(int(round(self.state / 10 - 0.01)) * 10, 10)
|
||||
icon += f"-{percentage}"
|
||||
|
||||
return icon
|
||||
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return units for the sensor."""
|
||||
return UNIT_PERCENT
|
||||
|
||||
def _update_battery_level(self, value):
|
||||
self._state = value
|
||||
|
||||
def _update_status_lo_batt(self, value):
|
||||
self._low_battery = value == 1
|
||||
|
||||
def _update_charging_state(self, value):
|
||||
# 0 = not charging
|
||||
# 1 = charging
|
||||
# 2 = not chargeable
|
||||
self._charging = value == 1
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the current battery level percentage."""
|
||||
return self._state
|
||||
|
||||
|
||||
ENTITY_TYPES = {
|
||||
"humidity": HomeKitHumiditySensor,
|
||||
"temperature": HomeKitTemperatureSensor,
|
||||
"light": HomeKitLightSensor,
|
||||
"carbon-dioxide": HomeKitCarbonDioxideSensor,
|
||||
"battery": HomeKitBatterySensor,
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user