2018-02-19 22:46:22 +00:00
|
|
|
"""Extend the basic Accessory and Bridge functions."""
|
2018-04-06 21:11:53 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
from functools import wraps
|
|
|
|
from inspect import getmodule
|
2018-02-26 03:27:40 +00:00
|
|
|
import logging
|
|
|
|
|
2018-05-04 14:46:00 +00:00
|
|
|
from pyhap.accessory import Accessory, Bridge
|
2018-03-15 01:48:21 +00:00
|
|
|
from pyhap.accessory_driver import AccessoryDriver
|
2018-05-04 14:46:00 +00:00
|
|
|
from pyhap.const import CATEGORY_OTHER
|
2018-02-19 22:46:22 +00:00
|
|
|
|
2018-04-28 21:17:30 +00:00
|
|
|
from homeassistant.const import __version__
|
2018-04-11 20:24:14 +00:00
|
|
|
from homeassistant.core import callback as ha_callback
|
2018-04-28 21:17:30 +00:00
|
|
|
from homeassistant.core import split_entity_id
|
2018-04-06 21:11:53 +00:00
|
|
|
from homeassistant.helpers.event import (
|
|
|
|
async_track_state_change, track_point_in_utc_time)
|
|
|
|
from homeassistant.util import dt as dt_util
|
2018-03-16 00:05:28 +00:00
|
|
|
|
2018-02-19 22:46:22 +00:00
|
|
|
from .const import (
|
2018-05-11 12:22:45 +00:00
|
|
|
BRIDGE_MODEL, BRIDGE_NAME, BRIDGE_SERIAL_NUMBER,
|
|
|
|
DEBOUNCE_TIMEOUT, MANUFACTURER)
|
2018-03-15 01:48:21 +00:00
|
|
|
from .util import (
|
|
|
|
show_setup_message, dismiss_setup_message)
|
2018-02-19 22:46:22 +00:00
|
|
|
|
2018-02-26 03:27:40 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-02-19 22:46:22 +00:00
|
|
|
|
|
|
|
|
2018-04-06 21:11:53 +00:00
|
|
|
def debounce(func):
|
|
|
|
"""Decorator function. Debounce callbacks form HomeKit."""
|
2018-04-11 20:24:14 +00:00
|
|
|
@ha_callback
|
2018-04-06 21:11:53 +00:00
|
|
|
def call_later_listener(*args):
|
|
|
|
"""Callback listener called from call_later."""
|
|
|
|
# pylint: disable=unsubscriptable-object
|
|
|
|
nonlocal lastargs, remove_listener
|
|
|
|
hass = lastargs['hass']
|
|
|
|
hass.async_add_job(func, *lastargs['args'])
|
|
|
|
lastargs = remove_listener = None
|
|
|
|
|
|
|
|
@wraps(func)
|
|
|
|
def wrapper(*args):
|
|
|
|
"""Wrapper starts async timer.
|
|
|
|
|
|
|
|
The accessory must have 'self.hass' and 'self.entity_id' as attributes.
|
|
|
|
"""
|
|
|
|
# pylint: disable=not-callable
|
|
|
|
hass = args[0].hass
|
|
|
|
nonlocal lastargs, remove_listener
|
|
|
|
if remove_listener:
|
|
|
|
remove_listener()
|
|
|
|
lastargs = remove_listener = None
|
|
|
|
lastargs = {'hass': hass, 'args': [*args]}
|
|
|
|
remove_listener = track_point_in_utc_time(
|
|
|
|
hass, call_later_listener,
|
|
|
|
dt_util.utcnow() + timedelta(seconds=DEBOUNCE_TIMEOUT))
|
|
|
|
logger.debug('%s: Start %s timeout', args[0].entity_id,
|
|
|
|
func.__name__.replace('set_', ''))
|
|
|
|
|
|
|
|
remove_listener = None
|
|
|
|
lastargs = None
|
|
|
|
name = getmodule(func).__name__
|
|
|
|
logger = logging.getLogger(name)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2018-02-26 03:27:40 +00:00
|
|
|
class HomeAccessory(Accessory):
|
2018-03-15 01:48:21 +00:00
|
|
|
"""Adapter class for Accessory."""
|
2018-02-26 03:27:40 +00:00
|
|
|
|
2018-05-11 12:22:45 +00:00
|
|
|
def __init__(self, hass, name, entity_id, aid, config,
|
|
|
|
category=CATEGORY_OTHER):
|
2018-02-26 03:27:40 +00:00
|
|
|
"""Initialize a Accessory object."""
|
2018-04-11 20:24:14 +00:00
|
|
|
super().__init__(name, aid=aid)
|
2018-05-11 12:22:45 +00:00
|
|
|
model = split_entity_id(entity_id)[0].replace("_", " ").title()
|
2018-05-04 14:46:00 +00:00
|
|
|
self.set_info_service(
|
|
|
|
firmware_revision=__version__, manufacturer=MANUFACTURER,
|
2018-05-11 12:22:45 +00:00
|
|
|
model=model, serial_number=entity_id)
|
2018-05-04 14:46:00 +00:00
|
|
|
self.category = category
|
2018-05-11 12:22:45 +00:00
|
|
|
self.config = config
|
2018-04-11 20:24:14 +00:00
|
|
|
self.entity_id = entity_id
|
|
|
|
self.hass = hass
|
2018-02-19 22:46:22 +00:00
|
|
|
|
2018-03-16 00:05:28 +00:00
|
|
|
def run(self):
|
|
|
|
"""Method called by accessory after driver is started."""
|
2018-04-04 22:52:25 +00:00
|
|
|
state = self.hass.states.get(self.entity_id)
|
2018-04-11 20:24:14 +00:00
|
|
|
self.update_state_callback(new_state=state)
|
2018-03-16 00:05:28 +00:00
|
|
|
async_track_state_change(
|
2018-04-11 20:24:14 +00:00
|
|
|
self.hass, self.entity_id, self.update_state_callback)
|
|
|
|
|
|
|
|
def update_state_callback(self, entity_id=None, old_state=None,
|
|
|
|
new_state=None):
|
|
|
|
"""Callback from state change listener."""
|
|
|
|
_LOGGER.debug('New_state: %s', new_state)
|
|
|
|
if new_state is None:
|
|
|
|
return
|
|
|
|
self.update_state(new_state)
|
|
|
|
|
|
|
|
def update_state(self, new_state):
|
|
|
|
"""Method called on state change to update HomeKit value.
|
|
|
|
|
|
|
|
Overridden by accessory types.
|
|
|
|
"""
|
|
|
|
pass
|
2018-03-16 00:05:28 +00:00
|
|
|
|
2018-02-19 22:46:22 +00:00
|
|
|
|
|
|
|
class HomeBridge(Bridge):
|
2018-03-15 01:48:21 +00:00
|
|
|
"""Adapter class for Bridge."""
|
2018-02-19 22:46:22 +00:00
|
|
|
|
2018-04-11 20:24:14 +00:00
|
|
|
def __init__(self, hass, name=BRIDGE_NAME):
|
2018-02-19 22:46:22 +00:00
|
|
|
"""Initialize a Bridge object."""
|
2018-04-11 20:24:14 +00:00
|
|
|
super().__init__(name)
|
2018-05-04 14:46:00 +00:00
|
|
|
self.set_info_service(
|
|
|
|
firmware_revision=__version__, manufacturer=MANUFACTURER,
|
|
|
|
model=BRIDGE_MODEL, serial_number=BRIDGE_SERIAL_NUMBER)
|
2018-04-04 22:52:25 +00:00
|
|
|
self.hass = hass
|
2018-03-01 23:20:02 +00:00
|
|
|
|
2018-03-15 01:48:21 +00:00
|
|
|
def setup_message(self):
|
|
|
|
"""Prevent print of pyhap setup message to terminal."""
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class HomeDriver(AccessoryDriver):
|
|
|
|
"""Adapter class for AccessoryDriver."""
|
|
|
|
|
2018-05-18 14:32:57 +00:00
|
|
|
def __init__(self, hass, *args, **kwargs):
|
2018-03-15 01:48:21 +00:00
|
|
|
"""Initialize a AccessoryDriver object."""
|
|
|
|
super().__init__(*args, **kwargs)
|
2018-05-18 14:32:57 +00:00
|
|
|
self.hass = hass
|
|
|
|
|
|
|
|
def pair(self, client_uuid, client_public):
|
|
|
|
"""Override super function to dismiss setup message if paired."""
|
|
|
|
value = super().pair(client_uuid, client_public)
|
|
|
|
if value:
|
|
|
|
dismiss_setup_message(self.hass)
|
|
|
|
return value
|
|
|
|
|
|
|
|
def unpair(self, client_uuid):
|
|
|
|
"""Override super function to show setup message if unpaired."""
|
|
|
|
super().unpair(client_uuid)
|
|
|
|
show_setup_message(self.hass, self.state.pincode)
|