diff --git a/homeassistant/components/light/rflink.py b/homeassistant/components/light/rflink.py index a36a4f43f22..e0b627f1ffa 100644 --- a/homeassistant/components/light/rflink.py +++ b/homeassistant/components/light/rflink.py @@ -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): diff --git a/homeassistant/components/rflink.py b/homeassistant/components/rflink.py index 2986b898ed0..52fd602f0de 100644 --- a/homeassistant/components/rflink.py +++ b/homeassistant/components/rflink.py @@ -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' diff --git a/homeassistant/components/sensor/rflink.py b/homeassistant/components/sensor/rflink.py index 80a5b7dfb8d..c6660701c21 100644 --- a/homeassistant/components/sensor/rflink.py +++ b/homeassistant/components/sensor/rflink.py @@ -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): diff --git a/tests/components/light/test_rflink.py b/tests/components/light/test_rflink.py index 9f1d98b0ffd..7eaa8e8fc6d 100644 --- a/tests/components/light/test_rflink.py +++ b/tests/components/light/test_rflink.py @@ -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') diff --git a/tests/components/sensor/test_rflink.py b/tests/components/sensor/test_rflink.py index 2925551ee06..a99d14cc735 100644 --- a/tests/components/sensor/test_rflink.py +++ b/tests/components/sensor/test_rflink.py @@ -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')