Refactor homekit_controller to prepare for more typing information (#65329)

This commit is contained in:
Jc2k 2022-01-31 22:48:16 +00:00 committed by GitHub
parent a9af29cbe0
commit 0f88790303
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
28 changed files with 139 additions and 179 deletions

View file

@ -42,85 +42,85 @@ class HomeKitSensorEntityDescription(SensorEntityDescription):
SIMPLE_SENSOR: dict[str, HomeKitSensorEntityDescription] = {
CharacteristicsTypes.Vendor.CONNECTSENSE_ENERGY_WATT: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.CONNECTSENSE_ENERGY_WATT,
CharacteristicsTypes.VENDOR_CONNECTSENSE_ENERGY_WATT: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_CONNECTSENSE_ENERGY_WATT,
name="Power",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=POWER_WATT,
),
CharacteristicsTypes.Vendor.CONNECTSENSE_ENERGY_AMPS: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.CONNECTSENSE_ENERGY_AMPS,
CharacteristicsTypes.VENDOR_CONNECTSENSE_ENERGY_AMPS: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_CONNECTSENSE_ENERGY_AMPS,
name="Current",
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
),
CharacteristicsTypes.Vendor.CONNECTSENSE_ENERGY_AMPS_20: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.CONNECTSENSE_ENERGY_AMPS_20,
CharacteristicsTypes.VENDOR_CONNECTSENSE_ENERGY_AMPS_20: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_CONNECTSENSE_ENERGY_AMPS_20,
name="Current",
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
),
CharacteristicsTypes.Vendor.CONNECTSENSE_ENERGY_KW_HOUR: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.CONNECTSENSE_ENERGY_KW_HOUR,
CharacteristicsTypes.VENDOR_CONNECTSENSE_ENERGY_KW_HOUR: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_CONNECTSENSE_ENERGY_KW_HOUR,
name="Energy kWh",
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
),
CharacteristicsTypes.Vendor.EVE_ENERGY_WATT: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.EVE_ENERGY_WATT,
CharacteristicsTypes.VENDOR_EVE_ENERGY_WATT: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_EVE_ENERGY_WATT,
name="Power",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=POWER_WATT,
),
CharacteristicsTypes.Vendor.EVE_ENERGY_KW_HOUR: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.EVE_ENERGY_KW_HOUR,
CharacteristicsTypes.VENDOR_EVE_ENERGY_KW_HOUR: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_EVE_ENERGY_KW_HOUR,
name="Energy kWh",
device_class=SensorDeviceClass.ENERGY,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ENERGY_KILO_WATT_HOUR,
),
CharacteristicsTypes.Vendor.EVE_ENERGY_VOLTAGE: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.EVE_ENERGY_VOLTAGE,
CharacteristicsTypes.VENDOR_EVE_ENERGY_VOLTAGE: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_EVE_ENERGY_VOLTAGE,
name="Volts",
device_class=SensorDeviceClass.VOLTAGE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_POTENTIAL_VOLT,
),
CharacteristicsTypes.Vendor.EVE_ENERGY_AMPERE: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.EVE_ENERGY_AMPERE,
CharacteristicsTypes.VENDOR_EVE_ENERGY_AMPERE: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_EVE_ENERGY_AMPERE,
name="Amps",
device_class=SensorDeviceClass.CURRENT,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=ELECTRIC_CURRENT_AMPERE,
),
CharacteristicsTypes.Vendor.KOOGEEK_REALTIME_ENERGY: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.KOOGEEK_REALTIME_ENERGY,
CharacteristicsTypes.VENDOR_KOOGEEK_REALTIME_ENERGY: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_KOOGEEK_REALTIME_ENERGY,
name="Power",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=POWER_WATT,
),
CharacteristicsTypes.Vendor.KOOGEEK_REALTIME_ENERGY_2: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.KOOGEEK_REALTIME_ENERGY_2,
CharacteristicsTypes.VENDOR_KOOGEEK_REALTIME_ENERGY_2: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_KOOGEEK_REALTIME_ENERGY_2,
name="Power",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=POWER_WATT,
),
CharacteristicsTypes.Vendor.EVE_DEGREE_AIR_PRESSURE: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.EVE_DEGREE_AIR_PRESSURE,
CharacteristicsTypes.VENDOR_EVE_DEGREE_AIR_PRESSURE: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_EVE_DEGREE_AIR_PRESSURE,
name="Air Pressure",
device_class=SensorDeviceClass.PRESSURE,
state_class=SensorStateClass.MEASUREMENT,
native_unit_of_measurement=PRESSURE_HPA,
),
CharacteristicsTypes.Vendor.VOCOLINC_OUTLET_ENERGY: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.Vendor.VOCOLINC_OUTLET_ENERGY,
CharacteristicsTypes.VENDOR_VOCOLINC_OUTLET_ENERGY: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.VENDOR_VOCOLINC_OUTLET_ENERGY,
name="Power",
device_class=SensorDeviceClass.POWER,
state_class=SensorStateClass.MEASUREMENT,
@ -134,10 +134,7 @@ SIMPLE_SENSOR: dict[str, HomeKitSensorEntityDescription] = {
native_unit_of_measurement=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.get_uuid(ServicesTypes.TEMPERATURE_SENSOR)
),
probe=(lambda char: char.service.type != ServicesTypes.TEMPERATURE_SENSOR),
),
CharacteristicsTypes.RELATIVE_HUMIDITY_CURRENT: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.RELATIVE_HUMIDITY_CURRENT,
@ -147,10 +144,7 @@ SIMPLE_SENSOR: dict[str, HomeKitSensorEntityDescription] = {
native_unit_of_measurement=PERCENTAGE,
# This sensor is only for humidity characteristics that are not part
# of a humidity sensor service.
probe=(
lambda char: char.service.type
!= ServicesTypes.get_uuid(ServicesTypes.HUMIDITY_SENSOR)
),
probe=(lambda char: char.service.type != ServicesTypes.HUMIDITY_SENSOR),
),
CharacteristicsTypes.AIR_QUALITY: HomeKitSensorEntityDescription(
key=CharacteristicsTypes.AIR_QUALITY,
@ -202,15 +196,6 @@ SIMPLE_SENSOR: dict[str, HomeKitSensorEntityDescription] = {
),
}
# For legacy reasons, "built-in" characteristic types are in their short form
# And vendor types don't have a short form
# This means long and short forms get mixed up in this dict, and comparisons
# don't work!
# We call get_uuid on *every* type to normalise them to the long form
# Eventually aiohomekit will use the long form exclusively amd this can be removed.
for k, v in list(SIMPLE_SENSOR.items()):
SIMPLE_SENSOR[CharacteristicsTypes.get_uuid(k)] = SIMPLE_SENSOR.pop(k)
class HomeKitHumiditySensor(HomeKitEntity, SensorEntity):
"""Representation of a Homekit humidity sensor."""
@ -415,7 +400,7 @@ async def async_setup_entry(
@callback
def async_add_service(service):
if not (entity_class := ENTITY_TYPES.get(service.short_type)):
if not (entity_class := ENTITY_TYPES.get(service.type)):
return False
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([entity_class(conn, info)], True)