Init sub-components using global var. (#17220)
This commit is contained in:
parent
2de1193fd9
commit
e922dd10ba
6 changed files with 13 additions and 138 deletions
|
@ -9,8 +9,7 @@ import logging
|
|||
import homeassistant.components.alarm_control_panel as alarm
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.components.spc import (
|
||||
ATTR_DISCOVER_AREAS, DATA_API, SIGNAL_UPDATE_ALARM)
|
||||
from homeassistant.components.spc import (DATA_API, SIGNAL_UPDATE_ALARM)
|
||||
from homeassistant.const import (
|
||||
STATE_ALARM_ARMED_AWAY, STATE_ALARM_ARMED_HOME, STATE_ALARM_ARMED_NIGHT,
|
||||
STATE_ALARM_DISARMED, STATE_ALARM_TRIGGERED)
|
||||
|
@ -37,12 +36,9 @@ def _get_alarm_state(area):
|
|||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
"""Set up the SPC alarm control panel platform."""
|
||||
if (discovery_info is None or
|
||||
discovery_info[ATTR_DISCOVER_AREAS] is None):
|
||||
return
|
||||
|
||||
async_add_entities([SpcAlarm(area=area, api=hass.data[DATA_API])
|
||||
for area in discovery_info[ATTR_DISCOVER_AREAS]])
|
||||
api = hass.data[DATA_API]
|
||||
async_add_entities([SpcAlarm(area=area, api=api)
|
||||
for area in api.areas.values()])
|
||||
|
||||
|
||||
class SpcAlarm(alarm.AlarmControlPanel):
|
||||
|
|
|
@ -9,8 +9,7 @@ import logging
|
|||
from homeassistant.components.binary_sensor import BinarySensorDevice
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.components.spc import (
|
||||
ATTR_DISCOVER_DEVICES, SIGNAL_UPDATE_SENSOR)
|
||||
from homeassistant.components.spc import (DATA_API, SIGNAL_UPDATE_SENSOR)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -27,13 +26,10 @@ def _get_device_class(zone_type):
|
|||
async def async_setup_platform(hass, config, async_add_entities,
|
||||
discovery_info=None):
|
||||
"""Set up the SPC binary sensor."""
|
||||
if (discovery_info is None or
|
||||
discovery_info[ATTR_DISCOVER_DEVICES] is None):
|
||||
return
|
||||
|
||||
async_add_entities(SpcBinarySensor(zone)
|
||||
for zone in discovery_info[ATTR_DISCOVER_DEVICES]
|
||||
if _get_device_class(zone.type))
|
||||
api = hass.data[DATA_API]
|
||||
async_add_entities([SpcBinarySensor(zone)
|
||||
for zone in api.zones.values()
|
||||
if _get_device_class(zone.type)])
|
||||
|
||||
|
||||
class SpcBinarySensor(BinarySensorDevice):
|
||||
|
|
|
@ -16,9 +16,6 @@ REQUIREMENTS = ['pyspcwebgw==0.4.0']
|
|||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
ATTR_DISCOVER_DEVICES = 'devices'
|
||||
ATTR_DISCOVER_AREAS = 'areas'
|
||||
|
||||
CONF_WS_URL = 'ws_url'
|
||||
CONF_API_URL = 'api_url'
|
||||
|
||||
|
@ -66,13 +63,11 @@ async def async_setup(hass, config):
|
|||
|
||||
# add sensor devices for each zone (typically motion/fire/door sensors)
|
||||
hass.async_create_task(discovery.async_load_platform(
|
||||
hass, 'binary_sensor', DOMAIN,
|
||||
{ATTR_DISCOVER_DEVICES: spc.zones.values()}, config))
|
||||
hass, 'binary_sensor', DOMAIN))
|
||||
|
||||
# create a separate alarm panel for each area
|
||||
hass.async_create_task(discovery.async_load_platform(
|
||||
hass, 'alarm_control_panel', DOMAIN,
|
||||
{ATTR_DISCOVER_AREAS: spc.areas.values()}, config))
|
||||
hass, 'alarm_control_panel', DOMAIN))
|
||||
|
||||
# start listening for incoming events over websocket
|
||||
spc.start()
|
||||
|
|
|
@ -1,58 +0,0 @@
|
|||
"""Tests for Vanderbilt SPC alarm control panel platform."""
|
||||
from homeassistant.components.alarm_control_panel import spc
|
||||
from homeassistant.const import (
|
||||
STATE_ALARM_ARMED_AWAY, STATE_ALARM_DISARMED)
|
||||
from homeassistant.components.spc import (DATA_API)
|
||||
|
||||
|
||||
async def test_setup_platform(hass):
|
||||
"""Test adding areas as separate alarm control panel devices."""
|
||||
added_entities = []
|
||||
|
||||
def add_entities(entities):
|
||||
nonlocal added_entities
|
||||
added_entities = list(entities)
|
||||
|
||||
area_defs = [{
|
||||
'id': '1',
|
||||
'name': 'House',
|
||||
'mode': '3',
|
||||
'last_set_time': '1485759851',
|
||||
'last_set_user_id': '1',
|
||||
'last_set_user_name': 'Pelle',
|
||||
'last_unset_time': '1485800564',
|
||||
'last_unset_user_id': '1',
|
||||
'last_unset_user_name': 'Lisa',
|
||||
'last_alarm': '1478174896'
|
||||
}, {
|
||||
'id': '3',
|
||||
'name': 'Garage',
|
||||
'mode': '0',
|
||||
'last_set_time': '1483705803',
|
||||
'last_set_user_id': '9998',
|
||||
'last_set_user_name': 'Pelle',
|
||||
'last_unset_time': '1483705808',
|
||||
'last_unset_user_id': '9998',
|
||||
'last_unset_user_name': 'Lisa'
|
||||
}]
|
||||
|
||||
from pyspcwebgw import Area
|
||||
|
||||
areas = [Area(gateway=None, spc_area=a) for a in area_defs]
|
||||
|
||||
hass.data[DATA_API] = None
|
||||
|
||||
await spc.async_setup_platform(hass=hass,
|
||||
config={},
|
||||
async_add_entities=add_entities,
|
||||
discovery_info={'areas': areas})
|
||||
|
||||
assert len(added_entities) == 2
|
||||
|
||||
assert added_entities[0].name == 'House'
|
||||
assert added_entities[0].state == STATE_ALARM_ARMED_AWAY
|
||||
assert added_entities[0].changed_by == 'Pelle'
|
||||
|
||||
assert added_entities[1].name == 'Garage'
|
||||
assert added_entities[1].state == STATE_ALARM_DISARMED
|
||||
assert added_entities[1].changed_by == 'Lisa'
|
|
@ -1,55 +0,0 @@
|
|||
"""Tests for Vanderbilt SPC binary sensor platform."""
|
||||
from homeassistant.components.binary_sensor import spc
|
||||
|
||||
|
||||
async def test_setup_platform(hass):
|
||||
"""Test autodiscovery of supported device types."""
|
||||
added_entities = []
|
||||
|
||||
zone_defs = [{
|
||||
'id': '1',
|
||||
'type': '3',
|
||||
'zone_name': 'Kitchen smoke',
|
||||
'area': '1',
|
||||
'area_name': 'House',
|
||||
'input': '0',
|
||||
'status': '0',
|
||||
}, {
|
||||
'id': '3',
|
||||
'type': '0',
|
||||
'zone_name': 'Hallway PIR',
|
||||
'area': '1',
|
||||
'area_name': 'House',
|
||||
'input': '0',
|
||||
'status': '0',
|
||||
}, {
|
||||
'id': '5',
|
||||
'type': '1',
|
||||
'zone_name': 'Front door',
|
||||
'area': '1',
|
||||
'area_name': 'House',
|
||||
'input': '1',
|
||||
'status': '0',
|
||||
}]
|
||||
|
||||
def add_entities(entities):
|
||||
nonlocal added_entities
|
||||
added_entities = list(entities)
|
||||
|
||||
from pyspcwebgw import Zone
|
||||
|
||||
zones = [Zone(area=None, spc_zone=z) for z in zone_defs]
|
||||
|
||||
await spc.async_setup_platform(hass=hass,
|
||||
config={},
|
||||
async_add_entities=add_entities,
|
||||
discovery_info={'devices': zones})
|
||||
|
||||
assert len(added_entities) == 3
|
||||
assert added_entities[0].device_class == 'smoke'
|
||||
assert added_entities[0].state == 'off'
|
||||
assert added_entities[1].device_class == 'motion'
|
||||
assert added_entities[1].state == 'off'
|
||||
assert added_entities[2].device_class == 'opening'
|
||||
assert added_entities[2].state == 'on'
|
||||
assert all(d.hidden for d in added_entities)
|
|
@ -59,7 +59,8 @@ async def test_update_alarm_device(hass):
|
|||
return_value=mock_coro(True)):
|
||||
assert await async_setup_component(hass, 'spc', config) is True
|
||||
|
||||
await hass.async_block_till_done()
|
||||
await hass.async_block_till_done()
|
||||
|
||||
entity_id = 'alarm_control_panel.house'
|
||||
|
||||
assert hass.states.get(entity_id).state == STATE_ALARM_ARMED_AWAY
|
||||
|
|
Loading…
Add table
Reference in a new issue