Skip to content

Commit

Permalink
Use NamedTuple for homekit valve type + service info (home-assistant#…
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Oct 4, 2021
1 parent 80a225c commit e0ab4ee
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 22 deletions.
46 changes: 31 additions & 15 deletions homeassistant/components/homekit/type_sensors.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Class to hold all sensor accessories."""
from __future__ import annotations

import logging
from typing import Callable, NamedTuple

from pyhap.const import CATEGORY_SENSOR

Expand Down Expand Up @@ -60,18 +63,31 @@

_LOGGER = logging.getLogger(__name__)

BINARY_SENSOR_SERVICE_MAP = {
DEVICE_CLASS_CO: (SERV_CARBON_MONOXIDE_SENSOR, CHAR_CARBON_MONOXIDE_DETECTED, int),
DEVICE_CLASS_CO2: (SERV_CARBON_DIOXIDE_SENSOR, CHAR_CARBON_DIOXIDE_DETECTED, int),
DEVICE_CLASS_DOOR: (SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE, int),
DEVICE_CLASS_GARAGE_DOOR: (SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE, int),
DEVICE_CLASS_GAS: (SERV_CARBON_MONOXIDE_SENSOR, CHAR_CARBON_MONOXIDE_DETECTED, int),
DEVICE_CLASS_MOISTURE: (SERV_LEAK_SENSOR, CHAR_LEAK_DETECTED, int),
DEVICE_CLASS_MOTION: (SERV_MOTION_SENSOR, CHAR_MOTION_DETECTED, bool),
DEVICE_CLASS_OCCUPANCY: (SERV_OCCUPANCY_SENSOR, CHAR_OCCUPANCY_DETECTED, int),
DEVICE_CLASS_OPENING: (SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE, int),
DEVICE_CLASS_SMOKE: (SERV_SMOKE_SENSOR, CHAR_SMOKE_DETECTED, int),
DEVICE_CLASS_WINDOW: (SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE, int),

class SI(NamedTuple):
"""Service info."""

service: str
char: str
format: Callable[[bool], int | bool]


BINARY_SENSOR_SERVICE_MAP: dict[str, SI] = {
DEVICE_CLASS_CO: SI(
SERV_CARBON_MONOXIDE_SENSOR, CHAR_CARBON_MONOXIDE_DETECTED, int
),
DEVICE_CLASS_CO2: SI(SERV_CARBON_DIOXIDE_SENSOR, CHAR_CARBON_DIOXIDE_DETECTED, int),
DEVICE_CLASS_DOOR: SI(SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE, int),
DEVICE_CLASS_GARAGE_DOOR: SI(SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE, int),
DEVICE_CLASS_GAS: SI(
SERV_CARBON_MONOXIDE_SENSOR, CHAR_CARBON_MONOXIDE_DETECTED, int
),
DEVICE_CLASS_MOISTURE: SI(SERV_LEAK_SENSOR, CHAR_LEAK_DETECTED, int),
DEVICE_CLASS_MOTION: SI(SERV_MOTION_SENSOR, CHAR_MOTION_DETECTED, bool),
DEVICE_CLASS_OCCUPANCY: SI(SERV_OCCUPANCY_SENSOR, CHAR_OCCUPANCY_DETECTED, int),
DEVICE_CLASS_OPENING: SI(SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE, int),
DEVICE_CLASS_SMOKE: SI(SERV_SMOKE_SENSOR, CHAR_SMOKE_DETECTED, int),
DEVICE_CLASS_WINDOW: SI(SERV_CONTACT_SENSOR, CHAR_CONTACT_SENSOR_STATE, int),
}


Expand Down Expand Up @@ -276,11 +292,11 @@ def __init__(self, *args):
else BINARY_SENSOR_SERVICE_MAP[DEVICE_CLASS_OCCUPANCY]
)

self.format = service_char[2]
service = self.add_preload_service(service_char[0])
self.format = service_char.format
service = self.add_preload_service(service_char.service)
initial_value = False if self.format is bool else 0
self.char_detected = service.configure_char(
service_char[1], value=initial_value
service_char.char, value=initial_value
)
# Set the state so it is in sync on initial
# GET to avoid an event storm after homekit startup
Expand Down
25 changes: 18 additions & 7 deletions homeassistant/components/homekit/type_switches.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Class to hold all switch accessories."""
from __future__ import annotations

import logging
from typing import NamedTuple

from pyhap.const import (
CATEGORY_FAUCET,
Expand Down Expand Up @@ -50,11 +53,19 @@

_LOGGER = logging.getLogger(__name__)

VALVE_TYPE = {
TYPE_FAUCET: (CATEGORY_FAUCET, 3),
TYPE_SHOWER: (CATEGORY_SHOWER_HEAD, 2),
TYPE_SPRINKLER: (CATEGORY_SPRINKLER, 1),
TYPE_VALVE: (CATEGORY_FAUCET, 0),

class ValveInfo(NamedTuple):
"""Category and type information for valve."""

category: int
valve_type: int


VALVE_TYPE: dict[str, ValveInfo] = {
TYPE_FAUCET: ValveInfo(CATEGORY_FAUCET, 3),
TYPE_SHOWER: ValveInfo(CATEGORY_SHOWER_HEAD, 2),
TYPE_SPRINKLER: ValveInfo(CATEGORY_SPRINKLER, 1),
TYPE_VALVE: ValveInfo(CATEGORY_FAUCET, 0),
}


Expand Down Expand Up @@ -199,15 +210,15 @@ def __init__(self, *args):
super().__init__(*args)
state = self.hass.states.get(self.entity_id)
valve_type = self.config[CONF_TYPE]
self.category = VALVE_TYPE[valve_type][0]
self.category = VALVE_TYPE[valve_type].category

serv_valve = self.add_preload_service(SERV_VALVE)
self.char_active = serv_valve.configure_char(
CHAR_ACTIVE, value=False, setter_callback=self.set_state
)
self.char_in_use = serv_valve.configure_char(CHAR_IN_USE, value=False)
self.char_valve_type = serv_valve.configure_char(
CHAR_VALVE_TYPE, value=VALVE_TYPE[valve_type][1]
CHAR_VALVE_TYPE, value=VALVE_TYPE[valve_type].valve_type
)
# Set the state so it is in sync on initial
# GET to avoid an event storm after homekit startup
Expand Down

0 comments on commit e0ab4ee

Please sign in to comment.