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 = { CHARACTERISTIC_PLATFORMS = {
CharacteristicsTypes.Vendor.EVE_ENERGY_WATT: "sensor",
CharacteristicsTypes.Vendor.KOOGEEK_REALTIME_ENERGY: "sensor", CharacteristicsTypes.Vendor.KOOGEEK_REALTIME_ENERGY: "sensor",
CharacteristicsTypes.get_uuid(CharacteristicsTypes.TEMPERATURE_CURRENT): "sensor",
} }

View File

@ -1,8 +1,8 @@
"""Support for Homekit sensors.""" """Support for Homekit sensors."""
from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.characteristics import Characteristic, CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes 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 ( from homeassistant.const import (
CONCENTRATION_PARTS_PER_MILLION, CONCENTRATION_PARTS_PER_MILLION,
DEVICE_CLASS_BATTERY, DEVICE_CLASS_BATTERY,
@ -28,14 +28,23 @@ SIMPLE_SENSOR = {
CharacteristicsTypes.Vendor.EVE_ENERGY_WATT: { CharacteristicsTypes.Vendor.EVE_ENERGY_WATT: {
"name": "Real Time Energy", "name": "Real Time Energy",
"device_class": DEVICE_CLASS_POWER, "device_class": DEVICE_CLASS_POWER,
"state_class": STATE_CLASS_MEASUREMENT,
"unit": "watts", "unit": "watts",
"icon": "mdi:chart-line",
}, },
CharacteristicsTypes.Vendor.KOOGEEK_REALTIME_ENERGY: { CharacteristicsTypes.Vendor.KOOGEEK_REALTIME_ENERGY: {
"name": "Real Time Energy", "name": "Real Time Energy",
"device_class": DEVICE_CLASS_POWER, "device_class": DEVICE_CLASS_POWER,
"state_class": STATE_CLASS_MEASUREMENT,
"unit": "watts", "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, info,
char, char,
device_class=None, device_class=None,
state_class=None,
unit=None, unit=None,
icon=None, icon=None,
name=None, name=None,
**kwargs,
): ):
"""Initialise a secondary HomeKit characteristic sensor.""" """Initialise a secondary HomeKit characteristic sensor."""
self._device_class = device_class self._device_class = device_class
self._state_class = state_class
self._unit = unit self._unit = unit
self._icon = icon self._icon = icon
self._name = name self._name = name
@ -235,9 +247,14 @@ class SimpleSensor(CharacteristicEntity, SensorEntity):
@property @property
def device_class(self): def device_class(self):
"""Return units for the sensor.""" """Return type of sensor."""
return self._device_class return self._device_class
@property
def state_class(self):
"""Return type of state."""
return self._state_class
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return units for the sensor.""" """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) conn.add_listener(async_add_service)
@callback @callback
def async_add_characteristic(char): def async_add_characteristic(char: Characteristic):
kwargs = SIMPLE_SENSOR.get(char.type) kwargs = SIMPLE_SENSOR.get(char.type)
if not kwargs: if not kwargs:
return False return False
if "probe" in kwargs and not kwargs["probe"](char):
return False
info = {"aid": char.service.accessory.aid, "iid": char.service.iid} info = {"aid": char.service.accessory.aid, "iid": char.service.iid}
async_add_entities([SimpleSensor(conn, info, char, **kwargs)], True) 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["min_humidity"] == 20
assert climate_state.attributes["max_humidity"] == 50 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") occ1 = entity_registry.async_get("binary_sensor.kitchen")
assert occ1.unique_id == "homekit-AB1C-56" assert occ1.unique_id == "homekit-AB1C-56"