Add secondary temperature sensors to homekit_controller (#52194)

This commit is contained in:
Jc2k 2021-06-28 22:48:29 +01:00 committed by GitHub
parent c6efdedd3c
commit 42c944ce56
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View file

@ -44,5 +44,7 @@ HOMEKIT_ACCESSORY_DISPATCH = {
}
CHARACTERISTIC_PLATFORMS = {
CharacteristicsTypes.Vendor.EVE_ENERGY_WATT: "sensor",
CharacteristicsTypes.Vendor.KOOGEEK_REALTIME_ENERGY: "sensor",
CharacteristicsTypes.get_uuid(CharacteristicsTypes.TEMPERATURE_CURRENT): "sensor",
}

View file

@ -1,8 +1,8 @@
"""Support for Homekit sensors."""
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.characteristics import Characteristic, CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import STATE_CLASS_MEASUREMENT, SensorEntity
from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION,
DEVICE_CLASS_BATTERY,
@ -28,14 +28,23 @@ SIMPLE_SENSOR = {
CharacteristicsTypes.Vendor.EVE_ENERGY_WATT: {
"name": "Real Time Energy",
"device_class": DEVICE_CLASS_POWER,
"state_class": STATE_CLASS_MEASUREMENT,
"unit": "watts",
"icon": "mdi:chart-line",
},
CharacteristicsTypes.Vendor.KOOGEEK_REALTIME_ENERGY: {
"name": "Real Time Energy",
"device_class": DEVICE_CLASS_POWER,
"state_class": STATE_CLASS_MEASUREMENT,
"unit": "watts",
"icon": "mdi:chart-line",
},
CharacteristicsTypes.get_uuid(CharacteristicsTypes.TEMPERATURE_CURRENT): {
"name": "Current Temperature",
"device_class": DEVICE_CLASS_TEMPERATURE,
"state_class": STATE_CLASS_MEASUREMENT,
"unit": TEMP_CELSIUS,
# This sensor is only for temperature characteristics that are not part
# of a temperature sensor service.
"probe": lambda char: char.service.type != ServicesTypes.TEMPERATURE_SENSOR,
},
}
@ -216,12 +225,15 @@ class SimpleSensor(CharacteristicEntity, SensorEntity):
info,
char,
device_class=None,
state_class=None,
unit=None,
icon=None,
name=None,
**kwargs,
):
"""Initialise a secondary HomeKit characteristic sensor."""
self._device_class = device_class
self._state_class = state_class
self._unit = unit
self._icon = icon
self._name = name
@ -235,9 +247,14 @@ class SimpleSensor(CharacteristicEntity, SensorEntity):
@property
def device_class(self):
"""Return units for the sensor."""
"""Return type of sensor."""
return self._device_class
@property
def state_class(self):
"""Return type of state."""
return self._state_class
@property
def unit_of_measurement(self):
"""Return units for the sensor."""
@ -285,10 +302,12 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
conn.add_listener(async_add_service)
@callback
def async_add_characteristic(char):
def async_add_characteristic(char: Characteristic):
kwargs = SIMPLE_SENSOR.get(char.type)
if not kwargs:
return False
if "probe" in kwargs and not kwargs["probe"](char):
return False
info = {"aid": char.service.accessory.aid, "iid": char.service.iid}
async_add_entities([SimpleSensor(conn, info, char, **kwargs)], True)

View file

@ -59,6 +59,9 @@ async def test_ecobee3_setup(hass):
assert climate_state.attributes["min_humidity"] == 20
assert climate_state.attributes["max_humidity"] == 50
climate_sensor = entity_registry.async_get("sensor.homew_current_temperature")
assert climate_sensor.unique_id == "homekit-123456789012-aid:1-sid:16-cid:16"
occ1 = entity_registry.async_get("binary_sensor.kitchen")
assert occ1.unique_id == "homekit-AB1C-56"