Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More homekit_controller clean up before config entry lands #22368

Merged
merged 1 commit into from Mar 26, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 11 additions & 27 deletions homeassistant/components/homekit_controller/__init__.py
Expand Up @@ -11,8 +11,7 @@

from .connection import get_accessory_information
from .const import (
CONTROLLER, DOMAIN, HOMEKIT_ACCESSORY_DISPATCH, KNOWN_ACCESSORIES,
KNOWN_DEVICES
CONTROLLER, DOMAIN, HOMEKIT_ACCESSORY_DISPATCH, KNOWN_DEVICES
)


Expand All @@ -34,25 +33,6 @@
PAIRING_FILE = "pairing.json"


def get_serial(accessory):
"""Obtain the serial number of a HomeKit device."""
# pylint: disable=import-error
from homekit.model.services import ServicesTypes
from homekit.model.characteristics import CharacteristicsTypes

for service in accessory['services']:
if ServicesTypes.get_short(service['type']) != \
'accessory-information':
continue
for characteristic in service['characteristics']:
ctype = CharacteristicsTypes.get_short(
characteristic['type'])
if ctype != 'serial-number':
continue
return characteristic['value']
return None


def escape_characteristic_name(char_name):
"""Escape any dash or dots in a characteristics name."""
return char_name.replace('-', '_').replace('.', '_')
Expand All @@ -76,6 +56,10 @@ def __init__(self, hass, host, port, model, hkid, config_num, config):
self.configurator = hass.components.configurator
self._connection_warning_logged = False

# This just tracks aid/iid pairs so we know if a HK service has been
# mapped to a HA entity.
self.entities = []

self.pairing_lock = asyncio.Lock(loop=hass.loop)

self.pairing = self.controller.pairings.get(hkid)
Expand All @@ -101,15 +85,16 @@ def accessory_setup(self):
self.hass, RETRY_INTERVAL, lambda _: self.accessory_setup())
return
for accessory in data:
serial = get_serial(accessory)
if serial in self.hass.data[KNOWN_ACCESSORIES]:
continue
self.hass.data[KNOWN_ACCESSORIES][serial] = self
aid = accessory['aid']
for service in accessory['services']:
iid = service['iid']
if (aid, iid) in self.entities:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where are these ids added to the list?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot. This code doesn't even exist anymore in the config flow branch, the append happens in a function that doesn't exist yet by async_setup_entry. Doh. PR incoming.

# Don't add the same entity again
continue

devtype = ServicesTypes.get_short(service['type'])
_LOGGER.debug("Found %s", devtype)
service_info = {'serial': serial,
service_info = {'serial': self.hkid,
'aid': aid,
'iid': service['iid'],
'model': self.model,
Expand Down Expand Up @@ -382,7 +367,6 @@ def discovery_dispatch(service, discovery_info):
device = HKDevice(hass, host, port, model, hkid, config_num, config)
hass.data[KNOWN_DEVICES][hkid] = device

hass.data[KNOWN_ACCESSORIES] = {}
hass.data[KNOWN_DEVICES] = {}
discovery.listen(hass, SERVICE_HOMEKIT, discovery_dispatch)
return True
Expand Up @@ -6,7 +6,7 @@
ATTR_BATTERY_LEVEL, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME,
STATE_ALARM_ARMED_NIGHT, STATE_ALARM_DISARMED, STATE_ALARM_TRIGGERED)

from . import KNOWN_ACCESSORIES, HomeKitEntity
from . import KNOWN_DEVICES, HomeKitEntity

DEPENDENCIES = ['homekit_controller']

Expand Down Expand Up @@ -34,7 +34,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up Homekit Alarm Control Panel support."""
if discovery_info is None:
return
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
accessory = hass.data[KNOWN_DEVICES][discovery_info['serial']]
add_entities([HomeKitAlarmControlPanel(accessory, discovery_info)],
True)

Expand Down
Expand Up @@ -3,7 +3,7 @@

from homeassistant.components.binary_sensor import BinarySensorDevice

from . import KNOWN_ACCESSORIES, HomeKitEntity
from . import KNOWN_DEVICES, HomeKitEntity

DEPENDENCIES = ['homekit_controller']

Expand All @@ -13,7 +13,7 @@
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up Homekit motion sensor support."""
if discovery_info is not None:
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
accessory = hass.data[KNOWN_DEVICES][discovery_info['serial']]
add_entities([HomeKitMotionSensor(accessory, discovery_info)], True)


Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/homekit_controller/climate.py
Expand Up @@ -7,7 +7,7 @@
SUPPORT_TARGET_TEMPERATURE)
from homeassistant.const import ATTR_TEMPERATURE, STATE_OFF, TEMP_CELSIUS

from . import KNOWN_ACCESSORIES, HomeKitEntity
from . import KNOWN_DEVICES, HomeKitEntity

DEPENDENCIES = ['homekit_controller']

Expand All @@ -29,7 +29,7 @@
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up Homekit climate."""
if discovery_info is not None:
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
accessory = hass.data[KNOWN_DEVICES][discovery_info['serial']]
add_entities([HomeKitClimateDevice(accessory, discovery_info)], True)


Expand Down
1 change: 0 additions & 1 deletion homeassistant/components/homekit_controller/const.py
@@ -1,7 +1,6 @@
"""Constants for the homekit_controller component."""
DOMAIN = 'homekit_controller'

KNOWN_ACCESSORIES = "{}-accessories".format(DOMAIN)
KNOWN_DEVICES = "{}-devices".format(DOMAIN)
CONTROLLER = "{}-controller".format(DOMAIN)

Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/homekit_controller/cover.py
Expand Up @@ -8,7 +8,7 @@
from homeassistant.const import (
STATE_CLOSED, STATE_CLOSING, STATE_OPEN, STATE_OPENING)

from . import KNOWN_ACCESSORIES, HomeKitEntity
from . import KNOWN_DEVICES, HomeKitEntity

STATE_STOPPED = 'stopped'

Expand Down Expand Up @@ -41,7 +41,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up HomeKit Cover support."""
if discovery_info is None:
return
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
accessory = hass.data[KNOWN_DEVICES][discovery_info['serial']]

if discovery_info['device-type'] == 'garage-door-opener':
add_entities([HomeKitGarageDoorCover(accessory, discovery_info)],
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/homekit_controller/light.py
Expand Up @@ -5,7 +5,7 @@
ATTR_BRIGHTNESS, ATTR_COLOR_TEMP, ATTR_HS_COLOR, SUPPORT_BRIGHTNESS,
SUPPORT_COLOR, SUPPORT_COLOR_TEMP, Light)

from . import KNOWN_ACCESSORIES, HomeKitEntity
from . import KNOWN_DEVICES, HomeKitEntity

DEPENDENCIES = ['homekit_controller']

Expand All @@ -15,7 +15,7 @@
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up Homekit lighting."""
if discovery_info is not None:
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
accessory = hass.data[KNOWN_DEVICES][discovery_info['serial']]
add_entities([HomeKitLight(accessory, discovery_info)], True)


Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/homekit_controller/lock.py
Expand Up @@ -5,7 +5,7 @@
from homeassistant.const import (
ATTR_BATTERY_LEVEL, STATE_LOCKED, STATE_UNLOCKED)

from . import KNOWN_ACCESSORIES, HomeKitEntity
from . import KNOWN_DEVICES, HomeKitEntity

DEPENDENCIES = ['homekit_controller']

Expand All @@ -30,7 +30,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up Homekit Lock support."""
if discovery_info is None:
return
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
accessory = hass.data[KNOWN_DEVICES][discovery_info['serial']]
add_entities([HomeKitLock(accessory, discovery_info)], True)


Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/homekit_controller/sensor.py
@@ -1,7 +1,7 @@
"""Support for Homekit sensors."""
from homeassistant.const import TEMP_CELSIUS

from . import KNOWN_ACCESSORIES, HomeKitEntity
from . import KNOWN_DEVICES, HomeKitEntity

DEPENDENCIES = ['homekit_controller']

Expand All @@ -16,7 +16,7 @@
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up Homekit sensor support."""
if discovery_info is not None:
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
accessory = hass.data[KNOWN_DEVICES][discovery_info['serial']]
devtype = discovery_info['device-type']

if devtype == 'humidity':
Expand Down
4 changes: 2 additions & 2 deletions homeassistant/components/homekit_controller/switch.py
Expand Up @@ -3,7 +3,7 @@

from homeassistant.components.switch import SwitchDevice

from . import KNOWN_ACCESSORIES, HomeKitEntity
from . import KNOWN_DEVICES, HomeKitEntity

DEPENDENCIES = ['homekit_controller']

Expand All @@ -15,7 +15,7 @@
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up Homekit switch support."""
if discovery_info is not None:
accessory = hass.data[KNOWN_ACCESSORIES][discovery_info['serial']]
accessory = hass.data[KNOWN_DEVICES][discovery_info['serial']]
add_entities([HomeKitSwitch(accessory, discovery_info)], True)


Expand Down