예제 #1
0
def test_invalid_topic(mock_load_platform, hass, mqtt_mock):
    """Test sending in invalid JSON."""
    mock_load_platform.return_value = mock_coro()
    async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/not_config',
                            '{}')
    yield from hass.async_block_till_done()
    assert not mock_load_platform.called
예제 #2
0
def test_only_valid_components(mock_load_platform, hass, mqtt_mock, caplog):
    """Test sending in invalid JSON."""
    mock_load_platform.return_value = mock_coro()
    async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config', '{}')
    yield from hass.async_block_till_done()
    assert 'Component climate is not supported' in caplog.text
    assert not mock_load_platform.called
예제 #3
0
def test_subscribing_config_topic(hass, mqtt_mock):
    """Test setting up discovery."""
    hass_config = {}
    discovery_topic = 'homeassistant'
    async_start(hass, discovery_topic, hass_config)
    assert mqtt_mock.subscribe.called
    call_args = mqtt_mock.subscribe.mock_calls[0][1]
    assert call_args[0] == discovery_topic + '/#'
    assert call_args[1] == 0
예제 #4
0
def test_invalid_json(mock_load_platform, hass, mqtt_mock, caplog):
    """Test sending in invalid JSON."""
    mock_load_platform.return_value = mock_coro()
    async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
                            'not json')
    yield from hass.async_block_till_done()
    assert 'Unable to parse JSON' in caplog.text
    assert not mock_load_platform.called
예제 #5
0
def test_correct_config_discovery(hass, mqtt_mock, caplog):
    """Test sending in invalid JSON."""
    async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
                            '{ "name": "Beer" }')
    yield from hass.async_block_till_done()

    state = hass.states.get('binary_sensor.beer')

    assert state is not None
    assert state.name == 'Beer'
def test_discovery_removal(hass, mqtt_mock, caplog):
    """Test expansion of abbreviated discovery payload."""
    yield from async_start(hass, 'homeassistant', {})

    data = (
        '{ "name": "Beer",'
        '  "status_topic": "test_topic",'
        '  "command_topic": "test_topic" }'
    )

    async_fire_mqtt_message(hass, 'homeassistant/switch/bla/config',
                            data)
    yield from hass.async_block_till_done()

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

    async_fire_mqtt_message(hass, 'homeassistant/switch/bla/config',
                            '')
    yield from hass.async_block_till_done()
    yield from hass.async_block_till_done()

    state = hass.states.get('switch.beer')
    assert state is None
예제 #7
0
def test_discovery_expansion(hass, mqtt_mock, caplog):
    """Test expansion of abbreviated discovery payload."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN)

    yield from async_start(hass, 'homeassistant', {}, entry)

    data = ('{ "~": "some/base/topic",'
            '  "name": "DiscoveryExpansionTest1",'
            '  "stat_t": "test_topic/~",'
            '  "cmd_t": "~/test_topic",'
            '  "dev":{'
            '    "ids":["5706DF"],'
            '    "name":"DiscoveryExpansionTest1 Device",'
            '    "mdl":"Generic",'
            '    "sw":"1.2.3.4",'
            '    "mf":"Noone"'
            '  }'
            '}')

    async_fire_mqtt_message(hass, 'homeassistant/switch/bla/config', data)
    yield from hass.async_block_till_done()

    state = hass.states.get('switch.DiscoveryExpansionTest1')
    assert state is not None
    assert state.name == 'DiscoveryExpansionTest1'
    assert ('switch', 'bla') in hass.data[ALREADY_DISCOVERED]
    assert state.state == STATE_OFF

    async_fire_mqtt_message(hass, 'test_topic/some/base/topic', 'ON')
    yield from hass.async_block_till_done()
    yield from hass.async_block_till_done()

    state = hass.states.get('switch.DiscoveryExpansionTest1')
    assert state.state == STATE_ON
예제 #8
0
def test_discovery_expansion(hass, mqtt_mock, caplog):
    """Test expansion of abbreviated discovery payload."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN)

    yield from async_start(hass, 'homeassistant', {}, entry)

    data = (
        '{ "~": "some/base/topic",'
        '  "name": "DiscoveryExpansionTest1",'
        '  "stat_t": "test_topic/~",'
        '  "cmd_t": "~/test_topic" }'
    )

    async_fire_mqtt_message(
        hass, 'homeassistant/switch/bla/config', data)
    yield from hass.async_block_till_done()

    state = hass.states.get('switch.DiscoveryExpansionTest1')
    assert state is not None
    assert state.name == 'DiscoveryExpansionTest1'
    assert ('switch', 'bla') in hass.data[ALREADY_DISCOVERED]
    assert state.state == STATE_OFF

    async_fire_mqtt_message(hass, 'test_topic/some/base/topic',
                            'ON')
    yield from hass.async_block_till_done()
    yield from hass.async_block_till_done()

    state = hass.states.get('switch.DiscoveryExpansionTest1')
    assert state.state == STATE_ON
예제 #9
0
def test_invalid_topic(mock_load_platform, hass, mqtt_mock):
    """Test sending to invalid topic."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN,
                            data={mqtt.CONF_BROKER: 'test-broker'})

    mock_load_platform.return_value = mock_coro()
    yield from async_start(hass, 'homeassistant', {}, entry)

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/not_config',
                            '{}')
    yield from hass.async_block_till_done()
    assert not mock_load_platform.called
예제 #10
0
def test_subscribing_config_topic(hass, mqtt_mock):
    """Test setting up discovery."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN,
                            data={mqtt.CONF_BROKER: 'test-broker'})

    hass_config = {}
    discovery_topic = 'homeassistant'
    yield from async_start(hass, discovery_topic, hass_config, entry)

    assert mqtt_mock.async_subscribe.called
    call_args = mqtt_mock.async_subscribe.mock_calls[0][1]
    assert call_args[0] == discovery_topic + '/#'
    assert call_args[2] == 0
예제 #11
0
def test_discovery_incl_nodeid(hass, mqtt_mock, caplog):
    """Test sending in correct JSON with optional node_id included."""
    yield from async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/my_node_id/bla'
                            '/config', '{ "name": "Beer" }')
    yield from hass.async_block_till_done()

    state = hass.states.get('binary_sensor.beer')

    assert state is not None
    assert state.name == 'Beer'
    assert ('binary_sensor', 'my_node_id_bla') in hass.data[ALREADY_DISCOVERED]
예제 #12
0
def test_invalid_json(mock_load_platform, hass, mqtt_mock, caplog):
    """Test sending in invalid JSON."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN,
                            data={mqtt.CONF_BROKER: 'test-broker'})

    mock_load_platform.return_value = mock_coro()
    yield from async_start(hass, 'homeassistant', {}, entry)

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
                            'not json')
    yield from hass.async_block_till_done()
    assert 'Unable to parse JSON' in caplog.text
    assert not mock_load_platform.called
예제 #13
0
def test_invalid_topic(mock_load_platform, hass, mqtt_mock):
    """Test sending to invalid topic."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN, data={
        mqtt.CONF_BROKER: 'test-broker'
    })

    mock_load_platform.return_value = mock_coro()
    yield from async_start(hass, 'homeassistant', {}, entry)

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/not_config',
                            '{}')
    yield from hass.async_block_till_done()
    assert not mock_load_platform.called
예제 #14
0
def test_discover_fan(hass, mqtt_mock, caplog):
    """Test discovering an MQTT fan."""
    yield from async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(hass, 'homeassistant/fan/bla/config',
                            ('{ "name": "Beer",'
                             '  "command_topic": "test_topic" }'))
    yield from hass.async_block_till_done()

    state = hass.states.get('fan.beer')

    assert state is not None
    assert state.name == 'Beer'
    assert ('fan', 'bla') in hass.data[ALREADY_DISCOVERED]
예제 #15
0
def test_subscribing_config_topic(hass, mqtt_mock):
    """Test setting up discovery."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN, data={
        mqtt.CONF_BROKER: 'test-broker'
    })

    hass_config = {}
    discovery_topic = 'homeassistant'
    yield from async_start(hass, discovery_topic, hass_config, entry)

    assert mqtt_mock.async_subscribe.called
    call_args = mqtt_mock.async_subscribe.mock_calls[0][1]
    assert call_args[0] == discovery_topic + '/#'
    assert call_args[2] == 0
예제 #16
0
def test_discover_fan(hass, mqtt_mock, caplog):
    """Test discovering an MQTT fan."""
    yield from async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(hass, 'homeassistant/fan/bla/config',
                            ('{ "name": "Beer",'
                             '  "command_topic": "test_topic" }'))
    yield from hass.async_block_till_done()

    state = hass.states.get('fan.beer')

    assert state is not None
    assert state.name == 'Beer'
    assert ('fan', 'bla') in hass.data[ALREADY_DISCOVERED]
예제 #17
0
def test_invalid_json(mock_load_platform, hass, mqtt_mock, caplog):
    """Test sending in invalid JSON."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN, data={
        mqtt.CONF_BROKER: 'test-broker'
    })

    mock_load_platform.return_value = mock_coro()
    yield from async_start(hass, 'homeassistant', {}, entry)

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
                            'not json')
    yield from hass.async_block_till_done()
    assert 'Unable to parse JSON' in caplog.text
    assert not mock_load_platform.called
예제 #18
0
def test_correct_config_discovery(hass, mqtt_mock, caplog):
    """Test sending in correct JSON."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN)

    yield from async_start(hass, 'homeassistant', {}, entry)

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
                            '{ "name": "Beer" }')
    yield from hass.async_block_till_done()

    state = hass.states.get('binary_sensor.beer')

    assert state is not None
    assert state.name == 'Beer'
    assert ('binary_sensor', 'bla') in hass.data[ALREADY_DISCOVERED]
예제 #19
0
def test_correct_config_discovery(hass, mqtt_mock, caplog):
    """Test sending in correct JSON."""
    entry = MockConfigEntry(domain=mqtt.DOMAIN)

    yield from async_start(hass, 'homeassistant', {}, entry)

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
                            '{ "name": "Beer" }')
    yield from hass.async_block_till_done()

    state = hass.states.get('binary_sensor.beer')

    assert state is not None
    assert state.name == 'Beer'
    assert ('binary_sensor', 'bla') in hass.data[ALREADY_DISCOVERED]
예제 #20
0
def test_discover_climate(hass, mqtt_mock, caplog):
    """Test discovering an MQTT climate component."""
    yield from async_start(hass, 'homeassistant', {})

    data = ('{ "name": "ClimateTest",'
            '  "current_temperature_topic": "climate/bla/current_temp",'
            '  "temperature_command_topic": "climate/bla/target_temp" }')

    async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config', data)
    yield from hass.async_block_till_done()

    state = hass.states.get('climate.ClimateTest')

    assert state is not None
    assert state.name == 'ClimateTest'
    assert ('climate', 'bla') in hass.data[ALREADY_DISCOVERED]
예제 #21
0
def test_only_valid_components(mock_load_platform, hass, mqtt_mock, caplog):
    """Test for a valid component."""
    invalid_component = "timer"

    mock_load_platform.return_value = mock_coro()
    yield from async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(
        hass, 'homeassistant/{}/bla/config'.format(invalid_component), '{}')

    yield from hass.async_block_till_done()

    assert 'Component {} is not supported'.format(
        invalid_component) in caplog.text

    assert not mock_load_platform.called
예제 #22
0
def test_non_duplicate_discovery(hass, mqtt_mock, caplog):
    """Test for a non duplicate component."""
    yield from async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
                            '{ "name": "Beer" }')
    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
                            '{ "name": "Beer" }')
    yield from hass.async_block_till_done()

    state = hass.states.get('binary_sensor.beer')
    state_duplicate = hass.states.get('binary_sensor.beer1')

    assert state is not None
    assert state.name == 'Beer'
    assert state_duplicate is None
    assert 'Component has already been discovered: ' \
           'binary_sensor bla' in caplog.text
예제 #23
0
def test_non_duplicate_discovery(hass, mqtt_mock, caplog):
    """Test for a non duplicate component."""
    yield from async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
                            '{ "name": "Beer" }')
    async_fire_mqtt_message(hass, 'homeassistant/binary_sensor/bla/config',
                            '{ "name": "Beer" }')
    yield from hass.async_block_till_done()

    state = hass.states.get('binary_sensor.beer')
    state_duplicate = hass.states.get('binary_sensor.beer1')

    assert state is not None
    assert state.name == 'Beer'
    assert state_duplicate is None
    assert 'Component has already been discovered: ' \
           'binary_sensor bla' in caplog.text
예제 #24
0
def test_discover_alarm_control_panel(hass, mqtt_mock, caplog):
    """Test discovering an MQTT alarm control panel component."""
    yield from async_start(hass, 'homeassistant', {})

    data = ('{ "name": "AlarmControlPanelTest",'
            '  "state_topic": "test_topic",'
            '  "command_topic": "test_topic" }')

    async_fire_mqtt_message(hass,
                            'homeassistant/alarm_control_panel/bla/config',
                            data)
    yield from hass.async_block_till_done()

    state = hass.states.get('alarm_control_panel.AlarmControlPanelTest')

    assert state is not None
    assert state.name == 'AlarmControlPanelTest'
    assert ('alarm_control_panel', 'bla') in hass.data[ALREADY_DISCOVERED]
예제 #25
0
def test_discover_climate(hass, mqtt_mock, caplog):
    """Test discovering an MQTT climate component."""
    yield from async_start(hass, 'homeassistant', {})

    data = (
        '{ "name": "ClimateTest",'
        '  "current_temperature_topic": "climate/bla/current_temp",'
        '  "temperature_command_topic": "climate/bla/target_temp" }'
    )

    async_fire_mqtt_message(hass, 'homeassistant/climate/bla/config', data)
    yield from hass.async_block_till_done()

    state = hass.states.get('climate.ClimateTest')

    assert state is not None
    assert state.name == 'ClimateTest'
    assert ('climate', 'bla') in hass.data[ALREADY_DISCOVERED]
예제 #26
0
def test_only_valid_components(mock_load_platform, hass, mqtt_mock, caplog):
    """Test for a valid component."""
    invalid_component = "timer"

    mock_load_platform.return_value = mock_coro()
    yield from async_start(hass, 'homeassistant', {})

    async_fire_mqtt_message(hass, 'homeassistant/{}/bla/config'.format(
        invalid_component
    ), '{}')

    yield from hass.async_block_till_done()

    assert 'Component {} is not supported'.format(
        invalid_component
    ) in caplog.text

    assert not mock_load_platform.called
예제 #27
0
def test_discover_alarm_control_panel(hass, mqtt_mock, caplog):
    """Test discovering an MQTT alarm control panel component."""
    yield from async_start(hass, 'homeassistant', {})

    data = (
        '{ "name": "AlarmControlPanelTest",'
        '  "state_topic": "test_topic",'
        '  "command_topic": "test_topic" }'
    )

    async_fire_mqtt_message(
        hass, 'homeassistant/alarm_control_panel/bla/config', data)
    yield from hass.async_block_till_done()

    state = hass.states.get('alarm_control_panel.AlarmControlPanelTest')

    assert state is not None
    assert state.name == 'AlarmControlPanelTest'
    assert ('alarm_control_panel', 'bla') in hass.data[ALREADY_DISCOVERED]
예제 #28
0
def test_discovery_removal(hass, mqtt_mock, caplog):
    """Test expansion of abbreviated discovery payload."""
    yield from async_start(hass, 'homeassistant', {})

    data = ('{ "name": "Beer",'
            '  "status_topic": "test_topic",'
            '  "command_topic": "test_topic" }')

    async_fire_mqtt_message(hass, 'homeassistant/switch/bla/config', data)
    yield from hass.async_block_till_done()

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

    async_fire_mqtt_message(hass, 'homeassistant/switch/bla/config', '')
    yield from hass.async_block_till_done()
    yield from hass.async_block_till_done()

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