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:
Jc2k 2019-08-31 13:18:18 +01:00 committed by GitHub
parent 7b05ede297
commit 944b544b2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 2430 additions and 5 deletions

View File

@ -25,4 +25,5 @@ HOMEKIT_ACCESSORY_DISPATCH = {
"humidity": "sensor",
"light": "sensor",
"temperature": "sensor",
"battery": "sensor",
}

View File

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

View File

@ -0,0 +1,36 @@
"""Tests for handling accessories on a Hue bridge via HomeKit."""
from tests.components.homekit_controller.common import (
setup_accessories_from_file,
setup_test_accessories,
Helper,
)
async def test_hue_bridge_setup(hass):
"""Test that a Hue hub can be correctly setup in HA via HomeKit."""
accessories = await setup_accessories_from_file(hass, "hue_bridge.json")
config_entry, pairing = await setup_test_accessories(hass, accessories)
entity_registry = await hass.helpers.entity_registry.async_get_registry()
# Check that the battery is correctly found and set up
battery_id = "sensor.hue_dimmer_switch_battery"
battery = entity_registry.async_get(battery_id)
assert battery.unique_id == "homekit-6623462389072572-644245094400"
battery_helper = Helper(
hass, "sensor.hue_dimmer_switch_battery", pairing, accessories[0], config_entry
)
battery_state = await battery_helper.poll_and_get_state()
assert battery_state.attributes["friendly_name"] == "Hue dimmer switch Battery"
assert battery_state.attributes["icon"] == "mdi:battery"
assert battery_state.state == "100"
device_registry = await hass.helpers.device_registry.async_get_registry()
device = device_registry.async_get(battery.device_id)
assert device.manufacturer == "Philips"
assert device.name == "Hue dimmer switch"
assert device.model == "RWL021"
assert device.sw_version == "45.1.17846"

View File

@ -5,6 +5,9 @@ TEMPERATURE = ("temperature", "temperature.current")
HUMIDITY = ("humidity", "relative-humidity.current")
LIGHT_LEVEL = ("light", "light-level.current")
CARBON_DIOXIDE_LEVEL = ("carbon-dioxide", "carbon-dioxide.level")
BATTERY_LEVEL = ("battery", "battery-level")
CHARGING_STATE = ("battery", "charging-state")
LO_BATT = ("battery", "status-lo-batt")
def create_temperature_sensor_service():
@ -47,6 +50,22 @@ def create_carbon_dioxide_level_sensor_service():
return service
def create_battery_level_sensor():
"""Define battery level characteristics."""
service = FakeService("public.hap.service.battery")
cur_state = service.add_characteristic("battery-level")
cur_state.value = 100
low_battery = service.add_characteristic("status-lo-batt")
low_battery.value = 0
charging_state = service.add_characteristic("charging-state")
charging_state.value = 0
return service
async def test_temperature_sensor_read_state(hass, utcnow):
"""Test reading the state of a HomeKit temperature sensor accessory."""
sensor = create_temperature_sensor_service()
@ -101,3 +120,49 @@ async def test_carbon_dioxide_level_sensor_read_state(hass, utcnow):
helper.characteristics[CARBON_DIOXIDE_LEVEL].value = 20
state = await helper.poll_and_get_state()
assert state.state == "20"
async def test_battery_level_sensor(hass, utcnow):
"""Test reading the state of a HomeKit battery level sensor."""
sensor = create_battery_level_sensor()
helper = await setup_test_component(hass, [sensor], suffix="battery")
helper.characteristics[BATTERY_LEVEL].value = 100
state = await helper.poll_and_get_state()
assert state.state == "100"
assert state.attributes["icon"] == "mdi:battery"
helper.characteristics[BATTERY_LEVEL].value = 20
state = await helper.poll_and_get_state()
assert state.state == "20"
assert state.attributes["icon"] == "mdi:battery-20"
async def test_battery_charging(hass, utcnow):
"""Test reading the state of a HomeKit battery's charging state."""
sensor = create_battery_level_sensor()
helper = await setup_test_component(hass, [sensor], suffix="battery")
helper.characteristics[BATTERY_LEVEL].value = 0
helper.characteristics[CHARGING_STATE].value = 1
state = await helper.poll_and_get_state()
assert state.attributes["icon"] == "mdi:battery-outline"
helper.characteristics[BATTERY_LEVEL].value = 20
state = await helper.poll_and_get_state()
assert state.attributes["icon"] == "mdi:battery-charging-20"
async def test_battery_low(hass, utcnow):
"""Test reading the state of a HomeKit battery's low state."""
sensor = create_battery_level_sensor()
helper = await setup_test_component(hass, [sensor], suffix="battery")
helper.characteristics[LO_BATT].value = 0
helper.characteristics[BATTERY_LEVEL].value = 1
state = await helper.poll_and_get_state()
assert state.attributes["icon"] == "mdi:battery-10"
helper.characteristics[LO_BATT].value = 1
state = await helper.poll_and_get_state()
assert state.attributes["icon"] == "mdi:battery-alert"

File diff suppressed because it is too large Load Diff