Add option to disable automatic add for lights and sensors. (#6852)

pull/6860/head
Johan Bloemberg 2017-03-30 17:02:03 +02:00 committed by Paulus Schoutsen
parent ee8701b560
commit 816b1891b5
5 changed files with 70 additions and 7 deletions

View File

@ -10,8 +10,8 @@ import logging
from homeassistant.components.light import (
ATTR_BRIGHTNESS, SUPPORT_BRIGHTNESS, Light)
from homeassistant.components.rflink import (
CONF_ALIASSES, CONF_DEVICE_DEFAULTS, CONF_DEVICES, CONF_FIRE_EVENT,
CONF_GROUP, CONF_GROUP_ALIASSES, CONF_IGNORE_DEVICES,
CONF_ALIASSES, CONF_AUTOMATIC_ADD, CONF_DEVICE_DEFAULTS, CONF_DEVICES,
CONF_FIRE_EVENT, CONF_GROUP, CONF_GROUP_ALIASSES, CONF_IGNORE_DEVICES,
CONF_NOGROUP_ALIASSES, CONF_SIGNAL_REPETITIONS, DATA_DEVICE_REGISTER,
DATA_ENTITY_GROUP_LOOKUP, DATA_ENTITY_LOOKUP, DEVICE_DEFAULTS_SCHEMA,
DOMAIN, EVENT_KEY_COMMAND, EVENT_KEY_ID, SwitchableRflinkDevice, cv, vol)
@ -32,6 +32,7 @@ PLATFORM_SCHEMA = vol.Schema({
vol.Optional(CONF_IGNORE_DEVICES): vol.All(cv.ensure_list, [cv.string]),
vol.Optional(CONF_DEVICE_DEFAULTS, default=DEVICE_DEFAULTS_SCHEMA({})):
DEVICE_DEFAULTS_SCHEMA,
vol.Optional(CONF_AUTOMATIC_ADD, default=True): cv.boolean,
vol.Optional(CONF_DEVICES, default={}): vol.Schema({
cv.string: {
vol.Optional(CONF_NAME): cv.string,
@ -165,7 +166,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
# Schedule task to process event after entity is created
hass.async_add_job(device.handle_event, event)
hass.data[DATA_DEVICE_REGISTER][EVENT_KEY_COMMAND] = add_new_device
if config[CONF_AUTOMATIC_ADD]:
hass.data[DATA_DEVICE_REGISTER][EVENT_KEY_COMMAND] = add_new_device
class RflinkLight(SwitchableRflinkDevice, Light):

View File

@ -33,6 +33,7 @@ CONF_GROUP = 'group'
CONF_NOGROUP_ALIASSES = 'nogroup_aliasses'
CONF_DEVICE_DEFAULTS = 'device_defaults'
CONF_DEVICES = 'devices'
CONF_AUTOMATIC_ADD = 'automatic_add'
CONF_FIRE_EVENT = 'fire_event'
CONF_IGNORE_DEVICES = 'ignore_devices'
CONF_RECONNECT_INTERVAL = 'reconnect_interval'

View File

@ -9,9 +9,9 @@ from functools import partial
import logging
from homeassistant.components.rflink import (
CONF_ALIASSES, CONF_DEVICES, DATA_DEVICE_REGISTER, DATA_ENTITY_LOOKUP,
DOMAIN, EVENT_KEY_ID, EVENT_KEY_SENSOR, EVENT_KEY_UNIT, RflinkDevice,
cv, vol)
CONF_ALIASSES, CONF_AUTOMATIC_ADD, CONF_DEVICES, DATA_DEVICE_REGISTER,
DATA_ENTITY_LOOKUP, DOMAIN, EVENT_KEY_ID, EVENT_KEY_SENSOR, EVENT_KEY_UNIT,
RflinkDevice, cv, vol)
from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, CONF_NAME, CONF_PLATFORM,
CONF_UNIT_OF_MEASUREMENT)
@ -30,6 +30,7 @@ CONF_SENSOR_TYPE = 'sensor_type'
PLATFORM_SCHEMA = vol.Schema({
vol.Required(CONF_PLATFORM): DOMAIN,
vol.Optional(CONF_AUTOMATIC_ADD, default=True): cv.boolean,
vol.Optional(CONF_DEVICES, default={}): vol.Schema({
cv.string: {
vol.Optional(CONF_NAME): cv.string,
@ -91,7 +92,8 @@ def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
# Schedule task to process event after entity is created
hass.async_add_job(device.handle_event, event)
hass.data[DATA_DEVICE_REGISTER][EVENT_KEY_SENSOR] = add_new_device
if config[CONF_AUTOMATIC_ADD]:
hass.data[DATA_DEVICE_REGISTER][EVENT_KEY_SENSOR] = add_new_device
class RflinkSensor(RflinkDevice):

View File

@ -533,3 +533,31 @@ def test_nogroup_device_id(hass, monkeypatch):
yield from hass.async_block_till_done()
# should affect state
assert hass.states.get('light.test').state == 'on'
@asyncio.coroutine
def test_disable_automatic_add(hass, monkeypatch):
"""If disabled new devices should not be automatically added."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'automatic_add': False,
},
}
# setup mocking rflink module
event_callback, _, _, _ = yield from mock_rflink(
hass, config, DOMAIN, monkeypatch)
# test event for new unconfigured sensor
event_callback({
'id': 'protocol_0_0',
'command': 'off',
})
yield from hass.async_block_till_done()
# make sure new device is not added
assert not hass.states.get('light.protocol_0_0')

View File

@ -70,3 +70,33 @@ def test_default_setup(hass, monkeypatch):
assert new_sensor.state == '0'
assert new_sensor.attributes['unit_of_measurement'] == '°C'
assert new_sensor.attributes['icon'] == 'mdi:thermometer'
@asyncio.coroutine
def test_disable_automatic_add(hass, monkeypatch):
"""If disabled new devices should not be automatically added."""
config = {
'rflink': {
'port': '/dev/ttyABC0',
},
DOMAIN: {
'platform': 'rflink',
'automatic_add': False,
},
}
# setup mocking rflink module
event_callback, _, _, _ = yield from mock_rflink(
hass, config, DOMAIN, monkeypatch)
# test event for new unconfigured sensor
event_callback({
'id': 'test2',
'sensor': 'temperature',
'value': 0,
'unit': '°C',
})
yield from hass.async_block_till_done()
# make sure new device is not added
assert not hass.states.get('sensor.test2')