convert alarmdecoder interface from async to sync (#11168)

* convert alarmdecoder interface from async to sync

* Convert he rest of alarmdecoder rom async to sync

* Update alarmdecoder.py

* Update alarmdecoder.py

* Update alarmdecoder.py

* Update alarmdecoder.py

* Update alarmdecoder.py

* Update alarmdecoder.py

* Update alarmdecoder.py

* Update alarmdecoder.py

* Update alarmdecoder.py

* Update alarmdecoder.py

* Update alarmdecoder.py

* Update alarmdecoder.py
This commit is contained in:
PhracturedBlue 2017-12-16 15:52:59 -08:00 committed by Fabian Affolter
parent a46ddcf6dd
commit 294d8171a2
No known key found for this signature in database
GPG Key ID: DDF3D6F44AAB1336
4 changed files with 54 additions and 110 deletions

View File

@ -7,30 +7,21 @@ https://home-assistant.io/components/alarm_control_panel.alarmdecoder/
import asyncio import asyncio
import logging import logging
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
import homeassistant.components.alarm_control_panel as alarm import homeassistant.components.alarm_control_panel as alarm
from homeassistant.components.alarmdecoder import (
from homeassistant.components.alarmdecoder import (DATA_AD, DATA_AD, SIGNAL_PANEL_MESSAGE)
SIGNAL_PANEL_MESSAGE)
from homeassistant.const import ( from homeassistant.const import (
STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED, STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_DISARMED,
STATE_UNKNOWN, STATE_ALARM_TRIGGERED) STATE_ALARM_TRIGGERED)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['alarmdecoder'] DEPENDENCIES = ['alarmdecoder']
@asyncio.coroutine def setup_platform(hass, config, add_devices, discovery_info=None):
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up for AlarmDecoder alarm panels.""" """Set up for AlarmDecoder alarm panels."""
_LOGGER.debug("AlarmDecoderAlarmPanel: setup") add_devices([AlarmDecoderAlarmPanel()])
device = AlarmDecoderAlarmPanel("Alarm Panel", hass)
async_add_devices([device])
return True return True
@ -38,38 +29,35 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
class AlarmDecoderAlarmPanel(alarm.AlarmControlPanel): class AlarmDecoderAlarmPanel(alarm.AlarmControlPanel):
"""Representation of an AlarmDecoder-based alarm panel.""" """Representation of an AlarmDecoder-based alarm panel."""
def __init__(self, name, hass): def __init__(self):
"""Initialize the alarm panel.""" """Initialize the alarm panel."""
self._display = "" self._display = ""
self._name = name self._name = "Alarm Panel"
self._state = STATE_UNKNOWN self._state = None
_LOGGER.debug("Setting up panel")
@asyncio.coroutine @asyncio.coroutine
def async_added_to_hass(self): def async_added_to_hass(self):
"""Register callbacks.""" """Register callbacks."""
async_dispatcher_connect( self.hass.helpers.dispatcher.async_dispatcher_connect(
self.hass, SIGNAL_PANEL_MESSAGE, self._message_callback) SIGNAL_PANEL_MESSAGE, self._message_callback)
@callback
def _message_callback(self, message): def _message_callback(self, message):
if message.alarm_sounding or message.fire_alarm: if message.alarm_sounding or message.fire_alarm:
if self._state != STATE_ALARM_TRIGGERED: if self._state != STATE_ALARM_TRIGGERED:
self._state = STATE_ALARM_TRIGGERED self._state = STATE_ALARM_TRIGGERED
self.async_schedule_update_ha_state() self.schedule_update_ha_state()
elif message.armed_away: elif message.armed_away:
if self._state != STATE_ALARM_ARMED_AWAY: if self._state != STATE_ALARM_ARMED_AWAY:
self._state = STATE_ALARM_ARMED_AWAY self._state = STATE_ALARM_ARMED_AWAY
self.async_schedule_update_ha_state() self.schedule_update_ha_state()
elif message.armed_home: elif message.armed_home:
if self._state != STATE_ALARM_ARMED_HOME: if self._state != STATE_ALARM_ARMED_HOME:
self._state = STATE_ALARM_ARMED_HOME self._state = STATE_ALARM_ARMED_HOME
self.async_schedule_update_ha_state() self.schedule_update_ha_state()
else: else:
if self._state != STATE_ALARM_DISARMED: if self._state != STATE_ALARM_DISARMED:
self._state = STATE_ALARM_DISARMED self._state = STATE_ALARM_DISARMED
self.async_schedule_update_ha_state() self.schedule_update_ha_state()
@property @property
def name(self): def name(self):
@ -91,26 +79,20 @@ class AlarmDecoderAlarmPanel(alarm.AlarmControlPanel):
"""Return the state of the device.""" """Return the state of the device."""
return self._state return self._state
@asyncio.coroutine def alarm_disarm(self, code=None):
def async_alarm_disarm(self, code=None):
"""Send disarm command.""" """Send disarm command."""
_LOGGER.debug("alarm_disarm: %s", code)
if code: if code:
_LOGGER.debug("alarm_disarm: sending %s1", str(code)) _LOGGER.debug("alarm_disarm: sending %s1", str(code))
self.hass.data[DATA_AD].send("{!s}1".format(code)) self.hass.data[DATA_AD].send("{!s}1".format(code))
@asyncio.coroutine def alarm_arm_away(self, code=None):
def async_alarm_arm_away(self, code=None):
"""Send arm away command.""" """Send arm away command."""
_LOGGER.debug("alarm_arm_away: %s", code)
if code: if code:
_LOGGER.debug("alarm_arm_away: sending %s2", str(code)) _LOGGER.debug("alarm_arm_away: sending %s2", str(code))
self.hass.data[DATA_AD].send("{!s}2".format(code)) self.hass.data[DATA_AD].send("{!s}2".format(code))
@asyncio.coroutine def alarm_arm_home(self, code=None):
def async_alarm_arm_home(self, code=None):
"""Send arm home command.""" """Send arm home command."""
_LOGGER.debug("alarm_arm_home: %s", code)
if code: if code:
_LOGGER.debug("alarm_arm_home: sending %s3", str(code)) _LOGGER.debug("alarm_arm_home: sending %s3", str(code))
self.hass.data[DATA_AD].send("{!s}3".format(code)) self.hass.data[DATA_AD].send("{!s}3".format(code))

View File

@ -4,16 +4,13 @@ Support for AlarmDecoder devices.
For more details about this component, please refer to the documentation at For more details about this component, please refer to the documentation at
https://home-assistant.io/components/alarmdecoder/ https://home-assistant.io/components/alarmdecoder/
""" """
import asyncio
import logging import logging
import voluptuous as vol import voluptuous as vol
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.core import callback
from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.discovery import load_platform
from homeassistant.helpers.dispatcher import async_dispatcher_send
REQUIREMENTS = ['alarmdecoder==0.12.3'] REQUIREMENTS = ['alarmdecoder==0.12.3']
@ -71,9 +68,9 @@ ZONE_SCHEMA = vol.Schema({
CONFIG_SCHEMA = vol.Schema({ CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({ DOMAIN: vol.Schema({
vol.Required(CONF_DEVICE): vol.Any(DEVICE_SOCKET_SCHEMA, vol.Required(CONF_DEVICE): vol.Any(
DEVICE_SERIAL_SCHEMA, DEVICE_SOCKET_SCHEMA, DEVICE_SERIAL_SCHEMA,
DEVICE_USB_SCHEMA), DEVICE_USB_SCHEMA),
vol.Optional(CONF_PANEL_DISPLAY, vol.Optional(CONF_PANEL_DISPLAY,
default=DEFAULT_PANEL_DISPLAY): cv.boolean, default=DEFAULT_PANEL_DISPLAY): cv.boolean,
vol.Optional(CONF_ZONES): {vol.Coerce(int): ZONE_SCHEMA}, vol.Optional(CONF_ZONES): {vol.Coerce(int): ZONE_SCHEMA},
@ -81,8 +78,7 @@ CONFIG_SCHEMA = vol.Schema({
}, extra=vol.ALLOW_EXTRA) }, extra=vol.ALLOW_EXTRA)
@asyncio.coroutine def setup(hass, config):
def async_setup(hass, config):
"""Set up for the AlarmDecoder devices.""" """Set up for the AlarmDecoder devices."""
from alarmdecoder import AlarmDecoder from alarmdecoder import AlarmDecoder
from alarmdecoder.devices import (SocketDevice, SerialDevice, USBDevice) from alarmdecoder.devices import (SocketDevice, SerialDevice, USBDevice)
@ -99,32 +95,25 @@ def async_setup(hass, config):
path = DEFAULT_DEVICE_PATH path = DEFAULT_DEVICE_PATH
baud = DEFAULT_DEVICE_BAUD baud = DEFAULT_DEVICE_BAUD
sync_connect = asyncio.Future(loop=hass.loop)
def handle_open(device):
"""Handle the successful connection."""
_LOGGER.info("Established a connection with the alarmdecoder")
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_alarmdecoder)
sync_connect.set_result(True)
@callback
def stop_alarmdecoder(event): def stop_alarmdecoder(event):
"""Handle the shutdown of AlarmDecoder.""" """Handle the shutdown of AlarmDecoder."""
_LOGGER.debug("Shutting down alarmdecoder") _LOGGER.debug("Shutting down alarmdecoder")
controller.close() controller.close()
@callback
def handle_message(sender, message): def handle_message(sender, message):
"""Handle message from AlarmDecoder.""" """Handle message from AlarmDecoder."""
async_dispatcher_send(hass, SIGNAL_PANEL_MESSAGE, message) hass.helpers.dispatcher.dispatcher_send(
SIGNAL_PANEL_MESSAGE, message)
def zone_fault_callback(sender, zone): def zone_fault_callback(sender, zone):
"""Handle zone fault from AlarmDecoder.""" """Handle zone fault from AlarmDecoder."""
async_dispatcher_send(hass, SIGNAL_ZONE_FAULT, zone) hass.helpers.dispatcher.dispatcher_send(
SIGNAL_ZONE_FAULT, zone)
def zone_restore_callback(sender, zone): def zone_restore_callback(sender, zone):
"""Handle zone restore from AlarmDecoder.""" """Handle zone restore from AlarmDecoder."""
async_dispatcher_send(hass, SIGNAL_ZONE_RESTORE, zone) hass.helpers.dispatcher.dispatcher_send(
SIGNAL_ZONE_RESTORE, zone)
controller = False controller = False
if device_type == 'socket': if device_type == 'socket':
@ -139,7 +128,6 @@ def async_setup(hass, config):
AlarmDecoder(USBDevice.find()) AlarmDecoder(USBDevice.find())
return False return False
controller.on_open += handle_open
controller.on_message += handle_message controller.on_message += handle_message
controller.on_zone_fault += zone_fault_callback controller.on_zone_fault += zone_fault_callback
controller.on_zone_restore += zone_restore_callback controller.on_zone_restore += zone_restore_callback
@ -148,21 +136,16 @@ def async_setup(hass, config):
controller.open(baud) controller.open(baud)
result = yield from sync_connect _LOGGER.debug("Established a connection with the alarmdecoder")
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_alarmdecoder)
if not result: load_platform(hass, 'alarm_control_panel', DOMAIN, conf, config)
return False
hass.async_add_job(
async_load_platform(hass, 'alarm_control_panel', DOMAIN, conf,
config))
if zones: if zones:
hass.async_add_job(async_load_platform( load_platform(
hass, 'binary_sensor', DOMAIN, {CONF_ZONES: zones}, config)) hass, 'binary_sensor', DOMAIN, {CONF_ZONES: zones}, config)
if display: if display:
hass.async_add_job(async_load_platform( load_platform(hass, 'sensor', DOMAIN, conf, config)
hass, 'sensor', DOMAIN, conf, config))
return True return True

View File

@ -7,39 +7,29 @@ https://home-assistant.io/components/binary_sensor.alarmdecoder/
import asyncio import asyncio
import logging import logging
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.alarmdecoder import (
from homeassistant.components.alarmdecoder import (ZONE_SCHEMA, ZONE_SCHEMA, CONF_ZONES, CONF_ZONE_NAME, CONF_ZONE_TYPE,
CONF_ZONES, SIGNAL_ZONE_FAULT, SIGNAL_ZONE_RESTORE)
CONF_ZONE_NAME,
CONF_ZONE_TYPE,
SIGNAL_ZONE_FAULT,
SIGNAL_ZONE_RESTORE)
DEPENDENCIES = ['alarmdecoder'] DEPENDENCIES = ['alarmdecoder']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@asyncio.coroutine def setup_platform(hass, config, add_devices, discovery_info=None):
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up the AlarmDecoder binary sensor devices.""" """Set up the AlarmDecoder binary sensor devices."""
configured_zones = discovery_info[CONF_ZONES] configured_zones = discovery_info[CONF_ZONES]
devices = [] devices = []
for zone_num in configured_zones: for zone_num in configured_zones:
device_config_data = ZONE_SCHEMA(configured_zones[zone_num]) device_config_data = ZONE_SCHEMA(configured_zones[zone_num])
zone_type = device_config_data[CONF_ZONE_TYPE] zone_type = device_config_data[CONF_ZONE_TYPE]
zone_name = device_config_data[CONF_ZONE_NAME] zone_name = device_config_data[CONF_ZONE_NAME]
device = AlarmDecoderBinarySensor( device = AlarmDecoderBinarySensor(zone_num, zone_name, zone_type)
hass, zone_num, zone_name, zone_type)
devices.append(device) devices.append(device)
async_add_devices(devices) add_devices(devices)
return True return True
@ -47,7 +37,7 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
class AlarmDecoderBinarySensor(BinarySensorDevice): class AlarmDecoderBinarySensor(BinarySensorDevice):
"""Representation of an AlarmDecoder binary sensor.""" """Representation of an AlarmDecoder binary sensor."""
def __init__(self, hass, zone_number, zone_name, zone_type): def __init__(self, zone_number, zone_name, zone_type):
"""Initialize the binary_sensor.""" """Initialize the binary_sensor."""
self._zone_number = zone_number self._zone_number = zone_number
self._zone_type = zone_type self._zone_type = zone_type
@ -55,16 +45,14 @@ class AlarmDecoderBinarySensor(BinarySensorDevice):
self._name = zone_name self._name = zone_name
self._type = zone_type self._type = zone_type
_LOGGER.debug("Setup up zone: %s", self._name)
@asyncio.coroutine @asyncio.coroutine
def async_added_to_hass(self): def async_added_to_hass(self):
"""Register callbacks.""" """Register callbacks."""
async_dispatcher_connect( self.hass.helpers.dispatcher.async_dispatcher_connect(
self.hass, SIGNAL_ZONE_FAULT, self._fault_callback) SIGNAL_ZONE_FAULT, self._fault_callback)
async_dispatcher_connect( self.hass.helpers.dispatcher.async_dispatcher_connect(
self.hass, SIGNAL_ZONE_RESTORE, self._restore_callback) SIGNAL_ZONE_RESTORE, self._restore_callback)
@property @property
def name(self): def name(self):
@ -97,16 +85,14 @@ class AlarmDecoderBinarySensor(BinarySensorDevice):
"""Return the class of this sensor, from DEVICE_CLASSES.""" """Return the class of this sensor, from DEVICE_CLASSES."""
return self._zone_type return self._zone_type
@callback
def _fault_callback(self, zone): def _fault_callback(self, zone):
"""Update the zone's state, if needed.""" """Update the zone's state, if needed."""
if zone is None or int(zone) == self._zone_number: if zone is None or int(zone) == self._zone_number:
self._state = 1 self._state = 1
self.async_schedule_update_ha_state() self.schedule_update_ha_state()
@callback
def _restore_callback(self, zone): def _restore_callback(self, zone):
"""Update the zone's state, if needed.""" """Update the zone's state, if needed."""
if zone is None or int(zone) == self._zone_number: if zone is None or int(zone) == self._zone_number:
self._state = 0 self._state = 0
self.async_schedule_update_ha_state() self.schedule_update_ha_state()

View File

@ -7,25 +7,21 @@ https://home-assistant.io/components/sensor.alarmdecoder/
import asyncio import asyncio
import logging import logging
from homeassistant.core import callback
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.components.alarmdecoder import (SIGNAL_PANEL_MESSAGE) from homeassistant.components.alarmdecoder import (SIGNAL_PANEL_MESSAGE)
from homeassistant.const import (STATE_UNKNOWN)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['alarmdecoder'] DEPENDENCIES = ['alarmdecoder']
@asyncio.coroutine def setup_platform(hass, config, add_devices, discovery_info=None):
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up for AlarmDecoder sensor devices.""" """Set up for AlarmDecoder sensor devices."""
_LOGGER.debug("AlarmDecoderSensor: async_setup_platform") _LOGGER.debug("AlarmDecoderSensor: setup_platform")
device = AlarmDecoderSensor(hass) device = AlarmDecoderSensor(hass)
async_add_devices([device]) add_devices([device])
class AlarmDecoderSensor(Entity): class AlarmDecoderSensor(Entity):
@ -34,23 +30,20 @@ class AlarmDecoderSensor(Entity):
def __init__(self, hass): def __init__(self, hass):
"""Initialize the alarm panel.""" """Initialize the alarm panel."""
self._display = "" self._display = ""
self._state = STATE_UNKNOWN self._state = None
self._icon = 'mdi:alarm-check' self._icon = 'mdi:alarm-check'
self._name = 'Alarm Panel Display' self._name = 'Alarm Panel Display'
_LOGGER.debug("Setting up panel")
@asyncio.coroutine @asyncio.coroutine
def async_added_to_hass(self): def async_added_to_hass(self):
"""Register callbacks.""" """Register callbacks."""
async_dispatcher_connect( self.hass.helpers.dispatcher.async_dispatcher_connect(
self.hass, SIGNAL_PANEL_MESSAGE, self._message_callback) SIGNAL_PANEL_MESSAGE, self._message_callback)
@callback
def _message_callback(self, message): def _message_callback(self, message):
if self._display != message.text: if self._display != message.text:
self._display = message.text self._display = message.text
self.async_schedule_update_ha_state() self.schedule_update_ha_state()
@property @property
def icon(self): def icon(self):