Use EntityDescription - homekit_controller (#56945)
This commit is contained in:
parent
9d671eff27
commit
3976443292
2 changed files with 136 additions and 149 deletions
|
@ -4,22 +4,26 @@ Support for Homekit number ranges.
|
|||
These are mostly used where a HomeKit accessory exposes additional non-standard
|
||||
characteristics that don't map to a Home Assistant feature.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
from aiohomekit.model.characteristics import Characteristic, CharacteristicsTypes
|
||||
|
||||
from homeassistant.components.number import NumberEntity
|
||||
from homeassistant.components.number import NumberEntity, NumberEntityDescription
|
||||
from homeassistant.core import callback
|
||||
|
||||
from . import KNOWN_DEVICES, CharacteristicEntity
|
||||
|
||||
NUMBER_ENTITIES = {
|
||||
CharacteristicsTypes.Vendor.VOCOLINC_HUMIDIFIER_SPRAY_LEVEL: {
|
||||
"name": "Spray Quantity",
|
||||
"icon": "mdi:water",
|
||||
},
|
||||
CharacteristicsTypes.Vendor.EVE_DEGREE_ELEVATION: {
|
||||
"name": "Elevation",
|
||||
"icon": "mdi:elevation-rise",
|
||||
},
|
||||
NUMBER_ENTITIES: dict[str, NumberEntityDescription] = {
|
||||
CharacteristicsTypes.Vendor.VOCOLINC_HUMIDIFIER_SPRAY_LEVEL: NumberEntityDescription(
|
||||
key=CharacteristicsTypes.Vendor.VOCOLINC_HUMIDIFIER_SPRAY_LEVEL,
|
||||
name="Spray Quantity",
|
||||
icon="mdi:water",
|
||||
),
|
||||
CharacteristicsTypes.Vendor.EVE_DEGREE_ELEVATION: NumberEntityDescription(
|
||||
key=CharacteristicsTypes.Vendor.EVE_DEGREE_ELEVATION,
|
||||
name="Elevation",
|
||||
icon="mdi:elevation-rise",
|
||||
),
|
||||
}
|
||||
|
||||
|
||||
|
@ -30,11 +34,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||
|
||||
@callback
|
||||
def async_add_characteristic(char: Characteristic):
|
||||
kwargs = NUMBER_ENTITIES.get(char.type)
|
||||
if not kwargs:
|
||||
if not (description := NUMBER_ENTITIES.get(char.type)):
|
||||
return False
|
||||
info = {"aid": char.service.accessory.aid, "iid": char.service.iid}
|
||||
async_add_entities([HomeKitNumber(conn, info, char, **kwargs)], True)
|
||||
async_add_entities([HomeKitNumber(conn, info, char, description)], True)
|
||||
return True
|
||||
|
||||
conn.add_char_factory(async_add_characteristic)
|
||||
|
@ -48,32 +51,16 @@ class HomeKitNumber(CharacteristicEntity, NumberEntity):
|
|||
conn,
|
||||
info,
|
||||
char,
|
||||
device_class=None,
|
||||
icon=None,
|
||||
name=None,
|
||||
**kwargs,
|
||||
description: NumberEntityDescription,
|
||||
):
|
||||
"""Initialise a HomeKit number control."""
|
||||
self._device_class = device_class
|
||||
self._icon = icon
|
||||
self._name = name
|
||||
|
||||
self.entity_description = description
|
||||
super().__init__(conn, info, char)
|
||||
|
||||
def get_characteristic_types(self):
|
||||
"""Define the homekit characteristics the entity is tracking."""
|
||||
return [self._char.type]
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return type of sensor."""
|
||||
return self._device_class
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the sensor icon."""
|
||||
return self._icon
|
||||
|
||||
@property
|
||||
def min_value(self) -> float:
|
||||
"""Return the minimum value."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue