Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions homeassistant/components/homekit_controller/air_quality.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for HomeKit Controller air quality sensors."""
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes

from homeassistant.components.air_quality import AirQualityEntity
from homeassistant.core import callback
Expand Down Expand Up @@ -85,10 +86,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
conn = hass.data[KNOWN_DEVICES][hkid]

@callback
def async_add_service(aid, service):
if service["stype"] != "air-quality":
def async_add_service(service):
if service.short_type != ServicesTypes.AIR_QUALITY_SENSOR:
return False
info = {"aid": aid, "iid": service["iid"]}
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([HomeAirQualitySensor(conn, info)], True)
return True

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for Homekit Alarm Control Panel."""
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes

from homeassistant.components.alarm_control_panel import AlarmControlPanelEntity
from homeassistant.components.alarm_control_panel.const import (
Expand Down Expand Up @@ -43,10 +44,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
conn = hass.data[KNOWN_DEVICES][hkid]

@callback
def async_add_service(aid, service):
if service["stype"] != "security-system":
def async_add_service(service):
if service.short_type != ServicesTypes.SECURITY_SYSTEM:
return False
info = {"aid": aid, "iid": service["iid"]}
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([HomeKitAlarmControlPanelEntity(conn, info)], True)
return True

Expand Down
19 changes: 10 additions & 9 deletions homeassistant/components/homekit_controller/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for Homekit motion sensors."""
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes

from homeassistant.components.binary_sensor import (
DEVICE_CLASS_GAS,
Expand Down Expand Up @@ -124,12 +125,12 @@ def is_on(self):


ENTITY_TYPES = {
"motion": HomeKitMotionSensor,
"contact": HomeKitContactSensor,
"smoke": HomeKitSmokeSensor,
"carbon-monoxide": HomeKitCarbonMonoxideSensor,
"occupancy": HomeKitOccupancySensor,
"leak": HomeKitLeakSensor,
ServicesTypes.MOTION_SENSOR: HomeKitMotionSensor,
ServicesTypes.CONTACT_SENSOR: HomeKitContactSensor,
ServicesTypes.SMOKE_SENSOR: HomeKitSmokeSensor,
ServicesTypes.CARBON_MONOXIDE_SENSOR: HomeKitCarbonMonoxideSensor,
ServicesTypes.OCCUPANCY_SENSOR: HomeKitOccupancySensor,
ServicesTypes.LEAK_SENSOR: HomeKitLeakSensor,
}


Expand All @@ -139,11 +140,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
conn = hass.data[KNOWN_DEVICES][hkid]

@callback
def async_add_service(aid, service):
entity_class = ENTITY_TYPES.get(service["stype"])
def async_add_service(service):
entity_class = ENTITY_TYPES.get(service.short_type)
if not entity_class:
return False
info = {"aid": aid, "iid": service["iid"]}
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([entity_class(conn, info)], True)
return True

Expand Down
11 changes: 6 additions & 5 deletions homeassistant/components/homekit_controller/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
SwingModeValues,
TargetHeaterCoolerStateValues,
)
from aiohomekit.model.services import ServicesTypes
from aiohomekit.utils import clamp_enum_to_char

from homeassistant.components.climate import (
Expand Down Expand Up @@ -87,11 +88,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
conn = hass.data[KNOWN_DEVICES][hkid]

@callback
def async_add_service(aid, service):
entity_class = ENTITY_TYPES.get(service["stype"])
def async_add_service(service):
entity_class = ENTITY_TYPES.get(service.short_type)
if not entity_class:
return False
info = {"aid": aid, "iid": service["iid"]}
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([entity_class(conn, info)], True)
return True

Expand Down Expand Up @@ -454,6 +455,6 @@ def temperature_unit(self):


ENTITY_TYPES = {
"heater-cooler": HomeKitHeaterCoolerEntity,
"thermostat": HomeKitClimateEntity,
ServicesTypes.HEATER_COOLER: HomeKitHeaterCoolerEntity,
ServicesTypes.THERMOSTAT: HomeKitClimateEntity,
}
12 changes: 5 additions & 7 deletions homeassistant/components/homekit_controller/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,19 +317,17 @@ def add_entities(self):
self._add_new_entities_for_accessory(self.accessory_factories)

def _add_new_entities(self, callbacks):
for accessory in self.accessories:
aid = accessory["aid"]
for service in accessory["services"]:
iid = service["iid"]
stype = ServicesTypes.get_short(service["type"].upper())
service["stype"] = stype
for accessory in self.entity_map.accessories:
aid = accessory.aid
for service in accessory.services:
iid = service.iid

if (aid, iid) in self.entities:
# Don't add the same entity again
continue

for listener in callbacks:
if listener(aid, service):
if listener(service):
self.entities.append((aid, iid))
break

Expand Down
25 changes: 14 additions & 11 deletions homeassistant/components/homekit_controller/cover.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for Homekit covers."""
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes

from homeassistant.components.cover import (
ATTR_POSITION,
Expand Down Expand Up @@ -39,17 +40,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
conn = hass.data[KNOWN_DEVICES][hkid]

@callback
def async_add_service(aid, service):
info = {"aid": aid, "iid": service["iid"]}
if service["stype"] == "garage-door-opener":
async_add_entities([HomeKitGarageDoorCover(conn, info)], True)
return True

if service["stype"] in ("window-covering", "window"):
async_add_entities([HomeKitWindowCover(conn, info)], True)
return True

return False
def async_add_service(service):
entity_class = ENTITY_TYPES.get(service.short_type)
if not entity_class:
return False
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([entity_class(conn, info)], True)
return True

conn.add_listener(async_add_service)

Expand Down Expand Up @@ -246,3 +243,9 @@ def device_state_attributes(self):
if not obstruction_detected:
return {}
return {"obstruction-detected": obstruction_detected}


ENTITY_TYPES = {
ServicesTypes.GARAGE_DOOR_OPENER: HomeKitGarageDoorCover,
ServicesTypes.WINDOW_COVERING: HomeKitWindowCover,
}
16 changes: 6 additions & 10 deletions homeassistant/components/homekit_controller/device_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ def enumerate_doorbell(service):


TRIGGER_FINDERS = {
"service-label": enumerate_stateless_switch_group,
"stateless-programmable-switch": enumerate_stateless_switch,
"doorbell": enumerate_doorbell,
ServicesTypes.SERVICE_LABEL: enumerate_stateless_switch_group,
ServicesTypes.STATELESS_PROGRAMMABLE_SWITCH: enumerate_stateless_switch,
ServicesTypes.DOORBELL: enumerate_doorbell,
}


Expand All @@ -186,8 +186,9 @@ async def async_setup_triggers_for_entry(hass: HomeAssistant, config_entry):
conn = hass.data[KNOWN_DEVICES][hkid]

@callback
def async_add_service(aid, service_dict):
service_type = service_dict["stype"]
def async_add_service(service):
aid = service.accessory.aid
service_type = service.short_type

# If not a known service type then we can't handle any stateless events for it
if service_type not in TRIGGER_FINDERS:
Expand All @@ -201,11 +202,6 @@ def async_add_service(aid, service_dict):
if device_id in hass.data[TRIGGERS]:
return False

# At the moment add_listener calls us with the raw service dict, rather than
# a service model. So turn it into a service ourselves.
accessory = conn.entity_map.aid(aid)
service = accessory.services.iid(service_dict["iid"])

# Just because we recognise the service type doesn't mean we can actually
# extract any triggers - so only proceed if we can
triggers = TRIGGER_FINDERS[service_type](service)
Expand Down
11 changes: 6 additions & 5 deletions homeassistant/components/homekit_controller/fan.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for Homekit fans."""
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes

from homeassistant.components.fan import (
DIRECTION_FORWARD,
Expand Down Expand Up @@ -161,8 +162,8 @@ class HomeKitFanV2(BaseHomeKitFan):


ENTITY_TYPES = {
"fan": HomeKitFanV1,
"fanv2": HomeKitFanV2,
ServicesTypes.FAN: HomeKitFanV1,
ServicesTypes.FAN_V2: HomeKitFanV2,
}


Expand All @@ -172,11 +173,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
conn = hass.data[KNOWN_DEVICES][hkid]

@callback
def async_add_service(aid, service):
entity_class = ENTITY_TYPES.get(service["stype"])
def async_add_service(service):
entity_class = ENTITY_TYPES.get(service.short_type)
if not entity_class:
return False
info = {"aid": aid, "iid": service["iid"]}
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([entity_class(conn, info)], True)
return True

Expand Down
52 changes: 12 additions & 40 deletions homeassistant/components/homekit_controller/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Optional

from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes

from homeassistant.components.humidifier import HumidifierEntity
from homeassistant.components.humidifier.const import (
Expand Down Expand Up @@ -253,51 +254,22 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
hkid = config_entry.data["AccessoryPairingID"]
conn = hass.data[KNOWN_DEVICES][hkid]

def get_accessory(conn, aid):
for acc in conn.accessories:
if acc.get("aid") == aid:
return acc
return None

def get_service(acc, iid):
for serv in acc.get("services"):
if serv.get("iid") == iid:
return serv
return None

def get_char(serv, iid):
try:
type_name = CharacteristicsTypes[iid]
type_uuid = CharacteristicsTypes.get_uuid(type_name)
for char in serv.get("characteristics"):
if char.get("type") == type_uuid:
return char
except KeyError:
return None
return None

@callback
def async_add_service(aid, service):
if service["stype"] != "humidifier-dehumidifier":
def async_add_service(service):
if service.short_type != ServicesTypes.HUMIDIFIER_DEHUMIDIFIER:
return False
info = {"aid": aid, "iid": service["iid"]}

acc = get_accessory(conn, aid)
serv = get_service(acc, service["iid"])
info = {"aid": service.accessory.aid, "iid": service.iid}

if (
get_char(serv, CharacteristicsTypes.RELATIVE_HUMIDITY_HUMIDIFIER_THRESHOLD)
is not None
):
async_add_entities([HomeKitHumidifier(conn, info)], True)
entities = []

if (
get_char(
serv, CharacteristicsTypes.RELATIVE_HUMIDITY_DEHUMIDIFIER_THRESHOLD
)
is not None
):
async_add_entities([HomeKitDehumidifier(conn, info)], True)
if service.has(CharacteristicsTypes.RELATIVE_HUMIDITY_HUMIDIFIER_THRESHOLD):
entities.append(HomeKitHumidifier(conn, info))

if service.has(CharacteristicsTypes.RELATIVE_HUMIDITY_DEHUMIDIFIER_THRESHOLD):
entities.append(HomeKitDehumidifier(conn, info))

async_add_entities(entities, True)

return True

Expand Down
7 changes: 4 additions & 3 deletions homeassistant/components/homekit_controller/light.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for Homekit lights."""
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes

from homeassistant.components.light import (
ATTR_BRIGHTNESS,
Expand All @@ -21,10 +22,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
conn = hass.data[KNOWN_DEVICES][hkid]

@callback
def async_add_service(aid, service):
if service["stype"] != "lightbulb":
def async_add_service(service):
if service.short_type != ServicesTypes.LIGHTBULB:
return False
info = {"aid": aid, "iid": service["iid"]}
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([HomeKitLight(conn, info)], True)
return True

Expand Down
7 changes: 4 additions & 3 deletions homeassistant/components/homekit_controller/lock.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Support for HomeKit Controller locks."""
from aiohomekit.model.characteristics import CharacteristicsTypes
from aiohomekit.model.services import ServicesTypes

from homeassistant.components.lock import LockEntity
from homeassistant.const import ATTR_BATTERY_LEVEL, STATE_LOCKED, STATE_UNLOCKED
Expand All @@ -20,10 +21,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
conn = hass.data[KNOWN_DEVICES][hkid]

@callback
def async_add_service(aid, service):
if service["stype"] != "lock-mechanism":
def async_add_service(service):
if service.short_type != ServicesTypes.LOCK_MECHANISM:
return False
info = {"aid": aid, "iid": service["iid"]}
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([HomeKitLock(conn, info)], True)
return True

Expand Down
6 changes: 3 additions & 3 deletions homeassistant/components/homekit_controller/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
conn = hass.data[KNOWN_DEVICES][hkid]

@callback
def async_add_service(aid, service):
if service["stype"] != "television":
def async_add_service(service):
if service.short_type != ServicesTypes.TELEVISION:
return False
info = {"aid": aid, "iid": service["iid"]}
info = {"aid": service.accessory.aid, "iid": service.iid}
async_add_entities([HomeKitTelevision(conn, info)], True)
return True

Expand Down
Loading