Skip to content

Commit

Permalink
Recreate alarm component if discovery info is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Nov 4, 2018
1 parent 1e9e2da commit 7a3bcca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 5 deletions.
15 changes: 10 additions & 5 deletions homeassistant/components/alarm_control_panel/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ async def async_discover(discovery_payload):
"""Discover and add an MQTT alarm control panel."""
config = PLATFORM_SCHEMA(discovery_payload)
await _async_setup_entity(hass, config, async_add_entities,
discovery_payload[ATTR_DISCOVERY_HASH])
discovery_payload, async_discover)

async_dispatcher_connect(
hass, MQTT_DISCOVERY_NEW.format(alarm.DOMAIN, 'mqtt'),
async_discover)


async def _async_setup_entity(hass, config, async_add_entities,
discovery_hash=None):
discovery_payload=None, async_discover=None):
"""Set up the MQTT Alarm Control Panel platform."""
async_add_entities([MqttAlarm(
config.get(CONF_NAME),
Expand All @@ -83,7 +83,8 @@ async def _async_setup_entity(hass, config, async_add_entities,
config.get(CONF_AVAILABILITY_TOPIC),
config.get(CONF_PAYLOAD_AVAILABLE),
config.get(CONF_PAYLOAD_NOT_AVAILABLE),
discovery_hash,)])
discovery_payload,
async_discover,)])


class MqttAlarm(MqttAvailability, MqttDiscoveryUpdate,
Expand All @@ -93,11 +94,12 @@ class MqttAlarm(MqttAvailability, MqttDiscoveryUpdate,
def __init__(self, name, state_topic, command_topic, qos, retain,
payload_disarm, payload_arm_home, payload_arm_away, code,
availability_topic, payload_available, payload_not_available,
discovery_hash):
discovery_payload, async_discover):
"""Init the MQTT Alarm Control Panel."""
MqttAvailability.__init__(self, availability_topic, qos,
payload_available, payload_not_available)
MqttDiscoveryUpdate.__init__(self, discovery_hash)
MqttDiscoveryUpdate.__init__(self, None, discovery_payload,
async_discover)
self._state = STATE_UNKNOWN
self._name = name
self._state_topic = state_topic
Expand All @@ -108,6 +110,9 @@ def __init__(self, name, state_topic, command_topic, qos, retain,
self._payload_arm_home = payload_arm_home
self._payload_arm_away = payload_arm_away
self._code = code
discovery_hash = None
if discovery_payload:
discovery_hash = discovery_payload[ATTR_DISCOVERY_HASH]
self._discovery_hash = discovery_hash

async def async_added_to_hass(self):
Expand Down
39 changes: 39 additions & 0 deletions tests/components/alarm_control_panel/test_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,42 @@ async def test_discovery_removal_alarm(hass, mqtt_mock, caplog):

state = hass.states.get('alarm_control_panel.beer')
assert state is None


async def test_discovery_update_alarm(hass, mqtt_mock, caplog):
"""Test removal of discovered alarm_control_panel."""
entry = MockConfigEntry(domain=mqtt.DOMAIN)
await async_start(hass, 'homeassistant', {}, entry)

data1 = (
'{ "name": "Beer",'
' "status_topic": "test_topic",'
' "command_topic": "test_topic" }'
)
data2 = (
'{ "name": "Milk",'
' "status_topic": "test_topic",'
' "command_topic": "test_topic" }'
)

async_fire_mqtt_message(hass,
'homeassistant/alarm_control_panel/bla/config',
data1)
await hass.async_block_till_done()

state = hass.states.get('alarm_control_panel.beer')
assert state is not None
assert state.name == 'Beer'

async_fire_mqtt_message(hass,
'homeassistant/alarm_control_panel/bla/config',
data2)
await hass.async_block_till_done()
await hass.async_block_till_done()

state = hass.states.get('alarm_control_panel.beer')
assert state is None

state = hass.states.get('alarm_control_panel.milk')
assert state is not None
assert state.name == 'Milk'

0 comments on commit 7a3bcca

Please sign in to comment.