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

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