Beispiel #1
0
async def test_on_command_rgb(hass, mqtt_mock):
    """Test on command in RGB brightness mode."""
    config = {light.DOMAIN: {
        'platform': 'mqtt',
        'name': 'test',
        'command_topic': 'test_light/set',
        'rgb_command_topic': "test_light/rgb",
    }}

    assert await async_setup_component(hass, light.DOMAIN, config)

    state = hass.states.get('light.test')
    assert STATE_OFF == state.state

    common.async_turn_on(hass, 'light.test', brightness=127)
    await hass.async_block_till_done()

    # Should get the following MQTT messages.
    #    test_light/rgb: '127,127,127'
    #    test_light/set: 'ON'
    mqtt_mock.async_publish.assert_has_calls([
        mock.call('test_light/rgb', '127,127,127', 0, False),
        mock.call('test_light/set', 'ON', 0, False),
    ], any_order=True)
    mqtt_mock.async_publish.reset_mock()

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with(
        'test_light/set', 'OFF', 0, False)
Beispiel #2
0
async def test_on_command_rgb(hass, mqtt_mock):
    """Test on command in RGB brightness mode."""
    config = {
        light.DOMAIN: {
            'platform': 'mqtt',
            'name': 'test',
            'command_topic': 'test_light/set',
            'rgb_command_topic': "test_light/rgb",
        }
    }

    assert await async_setup_component(hass, light.DOMAIN, config)

    state = hass.states.get('light.test')
    assert STATE_OFF == state.state

    common.async_turn_on(hass, 'light.test', brightness=127)
    await hass.async_block_till_done()

    # Should get the following MQTT messages.
    #    test_light/rgb: '127,127,127'
    #    test_light/set: 'ON'
    mqtt_mock.async_publish.assert_has_calls([
        mock.call('test_light/rgb', '127,127,127', 0, False),
        mock.call('test_light/set', 'ON', 0, False),
    ],
                                             any_order=True)
    mqtt_mock.async_publish.reset_mock()

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with('test_light/set', 'OFF', 0,
                                                    False)
Beispiel #3
0
async def test_on_command_first(hass, mqtt_mock):
    """Test on command being sent before brightness."""
    config = {
        light.DOMAIN: {
            'platform': 'mqtt',
            'name': 'test',
            'command_topic': 'test_light/set',
            'brightness_command_topic': 'test_light/bright',
            'on_command_type': 'first',
        }
    }

    assert await async_setup_component(hass, light.DOMAIN, config)

    state = hass.states.get('light.test')
    assert STATE_OFF == state.state

    common.async_turn_on(hass, 'light.test', brightness=50)
    await hass.async_block_till_done()

    # Should get the following MQTT messages.
    #    test_light/set: 'ON'
    #    test_light/bright: 50
    mqtt_mock.async_publish.assert_has_calls([
        mock.call('test_light/set', 'ON', 0, False),
        mock.call('test_light/bright', 50, 0, False),
    ],
                                             any_order=True)
    mqtt_mock.async_publish.reset_mock()

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with('test_light/set', 'OFF', 0,
                                                    False)
Beispiel #4
0
async def test_light_service_calls(hass):
    """Test service calls to light."""
    await async_setup_component(hass, 'switch', {'switch': [
        {'platform': 'demo'}
    ]})
    await async_setup_component(hass, 'light', {'light': [
        {'platform': 'switch', 'entity_id': 'switch.decorative_lights'}
    ]})
    await hass.async_block_till_done()

    assert hass.states.get('light.light_switch').state == 'on'

    common.async_toggle(hass, 'light.light_switch')
    await hass.async_block_till_done()

    assert hass.states.get('switch.decorative_lights').state == 'off'
    assert hass.states.get('light.light_switch').state == 'off'

    common.async_turn_on(hass, 'light.light_switch')
    await hass.async_block_till_done()

    assert hass.states.get('switch.decorative_lights').state == 'on'
    assert hass.states.get('light.light_switch').state == 'on'

    common.async_turn_off(hass, 'light.light_switch')
    await hass.async_block_till_done()

    assert hass.states.get('switch.decorative_lights').state == 'off'
    assert hass.states.get('light.light_switch').state == 'off'
async def test_light_service_calls(hass):
    """Test service calls to light."""
    await async_setup_component(hass, 'switch',
                                {'switch': [{
                                    'platform': 'demo'
                                }]})
    await async_setup_component(hass, 'light', {
        'light': [{
            'platform': 'switch',
            'entity_id': 'switch.decorative_lights'
        }]
    })
    await hass.async_block_till_done()

    assert hass.states.get('light.light_switch').state == 'on'

    common.async_toggle(hass, 'light.light_switch')
    await hass.async_block_till_done()

    assert hass.states.get('switch.decorative_lights').state == 'off'
    assert hass.states.get('light.light_switch').state == 'off'

    common.async_turn_on(hass, 'light.light_switch')
    await hass.async_block_till_done()

    assert hass.states.get('switch.decorative_lights').state == 'on'
    assert hass.states.get('light.light_switch').state == 'on'

    common.async_turn_off(hass, 'light.light_switch')
    await hass.async_block_till_done()

    assert hass.states.get('switch.decorative_lights').state == 'off'
    assert hass.states.get('light.light_switch').state == 'off'
Beispiel #6
0
async def test_on_command_first(hass, mqtt_mock):
    """Test on command being sent before brightness."""
    config = {light.DOMAIN: {
        'platform': 'mqtt',
        'name': 'test',
        'command_topic': 'test_light/set',
        'brightness_command_topic': 'test_light/bright',
        'on_command_type': 'first',
    }}

    assert await async_setup_component(hass, light.DOMAIN, config)

    state = hass.states.get('light.test')
    assert STATE_OFF == state.state

    common.async_turn_on(hass, 'light.test', brightness=50)
    await hass.async_block_till_done()

    # Should get the following MQTT messages.
    #    test_light/set: 'ON'
    #    test_light/bright: 50
    mqtt_mock.async_publish.assert_has_calls([
        mock.call('test_light/set', 'ON', 0, False),
        mock.call('test_light/bright', 50, 0, False),
    ], any_order=True)
    mqtt_mock.async_publish.reset_mock()

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with(
        'test_light/set', 'OFF', 0, False)
Beispiel #7
0
async def test_on_command_brightness(hass, mqtt_mock):
    """Test on command being sent as only brightness."""
    config = {
        light.DOMAIN: {
            'platform': 'mqtt',
            'name': 'test',
            'command_topic': 'test_light/set',
            'brightness_command_topic': 'test_light/bright',
            'rgb_command_topic': "test_light/rgb",
            'on_command_type': 'brightness',
        }
    }

    assert await async_setup_component(hass, light.DOMAIN, config)

    state = hass.states.get('light.test')
    assert STATE_OFF == state.state

    # Turn on w/ no brightness - should set to max
    common.async_turn_on(hass, 'light.test')
    await hass.async_block_till_done()

    # Should get the following MQTT messages.
    #    test_light/bright: 255
    mqtt_mock.async_publish.assert_called_once_with('test_light/bright', 255,
                                                    0, False)
    mqtt_mock.async_publish.reset_mock()

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with('test_light/set', 'OFF', 0,
                                                    False)
    mqtt_mock.async_publish.reset_mock()

    # Turn on w/ brightness
    common.async_turn_on(hass, 'light.test', brightness=50)
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with('test_light/bright', 50, 0,
                                                    False)
    mqtt_mock.async_publish.reset_mock()

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    # Turn on w/ just a color to insure brightness gets
    # added and sent.
    common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0])
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_has_calls([
        mock.call('test_light/rgb', '255,128,0', 0, False),
        mock.call('test_light/bright', 50, 0, False)
    ],
                                             any_order=True)
Beispiel #8
0
async def test_service_calls(hass):
    """Test service calls."""
    await async_setup_component(hass, 'light', {'light': [
        {'platform': 'demo'},
        {'platform': 'group', 'entities': ['light.bed_light',
                                           'light.ceiling_lights',
                                           'light.kitchen_lights']}
    ]})
    await hass.async_block_till_done()

    assert hass.states.get('light.light_group').state == 'on'
    common.async_toggle(hass, 'light.light_group')
    await hass.async_block_till_done()

    assert hass.states.get('light.bed_light').state == 'off'
    assert hass.states.get('light.ceiling_lights').state == 'off'
    assert hass.states.get('light.kitchen_lights').state == 'off'

    common.async_turn_on(hass, 'light.light_group')
    await hass.async_block_till_done()

    assert hass.states.get('light.bed_light').state == 'on'
    assert hass.states.get('light.ceiling_lights').state == 'on'
    assert hass.states.get('light.kitchen_lights').state == 'on'

    common.async_turn_off(hass, 'light.light_group')
    await hass.async_block_till_done()

    assert hass.states.get('light.bed_light').state == 'off'
    assert hass.states.get('light.ceiling_lights').state == 'off'
    assert hass.states.get('light.kitchen_lights').state == 'off'

    common.async_turn_on(hass, 'light.light_group', brightness=128,
                         effect='Random', rgb_color=(42, 255, 255))
    await hass.async_block_till_done()

    state = hass.states.get('light.bed_light')
    assert state.state == 'on'
    assert state.attributes['brightness'] == 128
    assert state.attributes['effect'] == 'Random'
    assert state.attributes['rgb_color'] == (42, 255, 255)

    state = hass.states.get('light.ceiling_lights')
    assert state.state == 'on'
    assert state.attributes['brightness'] == 128
    assert state.attributes['effect'] == 'Random'
    assert state.attributes['rgb_color'] == (42, 255, 255)

    state = hass.states.get('light.kitchen_lights')
    assert state.state == 'on'
    assert state.attributes['brightness'] == 128
    assert state.attributes['effect'] == 'Random'
    assert state.attributes['rgb_color'] == (42, 255, 255)
Beispiel #9
0
async def test_on_command_brightness(hass, mqtt_mock):
    """Test on command being sent as only brightness."""
    config = {light.DOMAIN: {
        'platform': 'mqtt',
        'name': 'test',
        'command_topic': 'test_light/set',
        'brightness_command_topic': 'test_light/bright',
        'rgb_command_topic': "test_light/rgb",
        'on_command_type': 'brightness',
    }}

    assert await async_setup_component(hass, light.DOMAIN, config)

    state = hass.states.get('light.test')
    assert STATE_OFF == state.state

    # Turn on w/ no brightness - should set to max
    common.async_turn_on(hass, 'light.test')
    await hass.async_block_till_done()

    # Should get the following MQTT messages.
    #    test_light/bright: 255
    mqtt_mock.async_publish.assert_called_once_with(
        'test_light/bright', 255, 0, False)
    mqtt_mock.async_publish.reset_mock()

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with(
        'test_light/set', 'OFF', 0, False)
    mqtt_mock.async_publish.reset_mock()

    # Turn on w/ brightness
    common.async_turn_on(hass, 'light.test', brightness=50)
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with(
        'test_light/bright', 50, 0, False)
    mqtt_mock.async_publish.reset_mock()

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    # Turn on w/ just a color to insure brightness gets
    # added and sent.
    common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0])
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_has_calls([
        mock.call('test_light/rgb', '255,128,0', 0, False),
        mock.call('test_light/bright', 50, 0, False)
    ], any_order=True)
Beispiel #10
0
async def test_lights_turn_on_when_coming_home_after_sun_set(hass, scanner):
    """Test lights turn on when coming home after sun set."""
    test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC)
    with patch('homeassistant.util.dt.utcnow', return_value=test_time):
        common_light.async_turn_off(hass)
        await hass.async_block_till_done()

        assert await async_setup_component(
            hass, device_sun_light_trigger.DOMAIN, {
                device_sun_light_trigger.DOMAIN: {}})

        hass.states.async_set(
            device_tracker.ENTITY_ID_FORMAT.format('device_2'), STATE_HOME)

        await hass.async_block_till_done()
    assert light.is_on(hass)
Beispiel #11
0
async def test_lights_turn_on_when_coming_home_after_sun_set(hass, scanner):
    """Test lights turn on when coming home after sun set."""
    test_time = datetime(2017, 4, 5, 3, 2, 3, tzinfo=dt_util.UTC)
    with patch('homeassistant.util.dt.utcnow', return_value=test_time):
        common_light.async_turn_off(hass)
        await hass.async_block_till_done()

        assert await async_setup_component(
            hass, device_sun_light_trigger.DOMAIN,
            {device_sun_light_trigger.DOMAIN: {}})

        hass.states.async_set(
            device_tracker.ENTITY_ID_FORMAT.format('device_2'), STATE_HOME)

        await hass.async_block_till_done()
    assert light.is_on(hass)
Beispiel #12
0
async def test_lights_on_when_sun_sets(hass, scanner):
    """Test lights go on when there is someone home and the sun sets."""
    test_time = datetime(2017, 4, 5, 1, 2, 3, tzinfo=dt_util.UTC)
    with patch('homeassistant.util.dt.utcnow', return_value=test_time):
        assert await async_setup_component(
            hass, device_sun_light_trigger.DOMAIN,
            {device_sun_light_trigger.DOMAIN: {}})

    common_light.async_turn_off(hass)

    await hass.async_block_till_done()

    test_time = test_time.replace(hour=3)
    with patch('homeassistant.util.dt.utcnow', return_value=test_time):
        async_fire_time_changed(hass, test_time)
        await hass.async_block_till_done()

    assert light.is_on(hass)
Beispiel #13
0
async def test_lights_on_when_sun_sets(hass, scanner):
    """Test lights go on when there is someone home and the sun sets."""
    test_time = datetime(2017, 4, 5, 1, 2, 3, tzinfo=dt_util.UTC)
    with patch('homeassistant.util.dt.utcnow', return_value=test_time):
        assert await async_setup_component(
            hass, device_sun_light_trigger.DOMAIN, {
                device_sun_light_trigger.DOMAIN: {}})

    common_light.async_turn_off(hass)

    await hass.async_block_till_done()

    test_time = test_time.replace(hour=3)
    with patch('homeassistant.util.dt.utcnow', return_value=test_time):
        async_fire_time_changed(hass, test_time)
        await hass.async_block_till_done()

    assert light.is_on(hass)
Beispiel #14
0
async def test_transition(hass, mqtt_mock):
    """Test for transition time being sent when included."""
    assert await async_setup_component(
        hass, light.DOMAIN, {
            light.DOMAIN: {
                'platform': 'mqtt',
                'schema': 'json',
                'name': 'test',
                'command_topic': 'test_light_rgb/set',
                'qos': 0
            }
        })

    state = hass.states.get('light.test')
    assert STATE_OFF == state.state
    assert 40 == state.attributes.get(ATTR_SUPPORTED_FEATURES)

    common.async_turn_on(hass, 'light.test', transition=15)
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with(
        'test_light_rgb/set',
        JsonValidator('{"state": "ON", "transition": 15}'), 0, False)
    mqtt_mock.async_publish.reset_mock()
    state = hass.states.get('light.test')
    assert STATE_ON == state.state

    common.async_turn_off(hass, 'light.test', transition=30)
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with(
        'test_light_rgb/set',
        JsonValidator('{"state": "OFF", "transition": 30}'), 0, False)
    mqtt_mock.async_publish.reset_mock()
    state = hass.states.get('light.test')
    assert STATE_OFF == state.state
Beispiel #15
0
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
    """Test the sending of command in optimistic mode."""
    fake_state = ha.State(
        'light.test', 'on', {
            'brightness': 95,
            'hs_color': [100, 100],
            'effect': 'random',
            'color_temp': 100,
            'white_value': 50
        })

    with patch(
            'homeassistant.helpers.restore_state.RestoreEntity'
            '.async_get_last_state',
            return_value=mock_coro(fake_state)):
        assert await async_setup_component(
            hass, light.DOMAIN, {
                light.DOMAIN: {
                    'platform': 'mqtt',
                    'schema': 'json',
                    'name': 'test',
                    'command_topic': 'test_light_rgb/set',
                    'brightness': True,
                    'color_temp': True,
                    'effect': True,
                    'hs': True,
                    'rgb': True,
                    'xy': True,
                    'white_value': True,
                    'qos': 2
                }
            })

    state = hass.states.get('light.test')
    assert STATE_ON == state.state
    assert 95 == state.attributes.get('brightness')
    assert (100, 100) == state.attributes.get('hs_color')
    assert 'random' == state.attributes.get('effect')
    assert 100 == state.attributes.get('color_temp')
    assert 50 == state.attributes.get('white_value')
    assert 191 == state.attributes.get(ATTR_SUPPORTED_FEATURES)
    assert state.attributes.get(ATTR_ASSUMED_STATE)

    common.async_turn_on(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with('test_light_rgb/set',
                                                    '{"state": "ON"}', 2,
                                                    False)
    mqtt_mock.async_publish.reset_mock()
    state = hass.states.get('light.test')
    assert STATE_ON == state.state

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with('test_light_rgb/set',
                                                    '{"state": "OFF"}', 2,
                                                    False)
    mqtt_mock.async_publish.reset_mock()
    state = hass.states.get('light.test')
    assert STATE_OFF == state.state

    mqtt_mock.reset_mock()
    common.async_turn_on(hass,
                         'light.test',
                         brightness=50,
                         xy_color=[0.123, 0.123])
    common.async_turn_on(hass, 'light.test', brightness=50, hs_color=[359, 78])
    common.async_turn_on(hass,
                         'light.test',
                         rgb_color=[255, 128, 0],
                         white_value=80)
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_has_calls([
        mock.call(
            'test_light_rgb/set',
            JsonValidator(
                '{"state": "ON", "color": {"r": 0, "g": 123, "b": 255,'
                ' "x": 0.14, "y": 0.131, "h": 210.824, "s": 100.0},'
                ' "brightness": 50}'), 2, False),
        mock.call(
            'test_light_rgb/set',
            JsonValidator(
                '{"state": "ON", "color": {"r": 255, "g": 56, "b": 59,'
                ' "x": 0.654, "y": 0.301, "h": 359.0, "s": 78.0},'
                ' "brightness": 50}'), 2, False),
        mock.call(
            'test_light_rgb/set',
            JsonValidator(
                '{"state": "ON", "color": {"r": 255, "g": 128, "b": 0,'
                ' "x": 0.611, "y": 0.375, "h": 30.118, "s": 100.0},'
                ' "white_value": 80}'), 2, False),
    ],
                                             any_order=True)

    state = hass.states.get('light.test')
    assert STATE_ON == state.state
    assert (255, 128, 0) == state.attributes['rgb_color']
    assert 50 == state.attributes['brightness']
    assert (30.118, 100) == state.attributes['hs_color']
    assert 80 == state.attributes['white_value']
    assert (0.611, 0.375) == state.attributes['xy_color']
Beispiel #16
0
async def test_service_calls(hass):
    """Test service calls."""
    await async_setup_component(
        hass, 'light', {
            'light': [{
                'platform': 'demo'
            }, {
                'platform':
                'group',
                'entities': [
                    'light.bed_light', 'light.ceiling_lights',
                    'light.kitchen_lights'
                ]
            }]
        })
    await hass.async_block_till_done()

    assert hass.states.get('light.light_group').state == 'on'
    common.async_toggle(hass, 'light.light_group')
    await hass.async_block_till_done()

    assert hass.states.get('light.bed_light').state == 'off'
    assert hass.states.get('light.ceiling_lights').state == 'off'
    assert hass.states.get('light.kitchen_lights').state == 'off'

    common.async_turn_on(hass, 'light.light_group')
    await hass.async_block_till_done()

    assert hass.states.get('light.bed_light').state == 'on'
    assert hass.states.get('light.ceiling_lights').state == 'on'
    assert hass.states.get('light.kitchen_lights').state == 'on'

    common.async_turn_off(hass, 'light.light_group')
    await hass.async_block_till_done()

    assert hass.states.get('light.bed_light').state == 'off'
    assert hass.states.get('light.ceiling_lights').state == 'off'
    assert hass.states.get('light.kitchen_lights').state == 'off'

    common.async_turn_on(hass,
                         'light.light_group',
                         brightness=128,
                         effect='Random',
                         rgb_color=(42, 255, 255))
    await hass.async_block_till_done()

    state = hass.states.get('light.bed_light')
    assert state.state == 'on'
    assert state.attributes['brightness'] == 128
    assert state.attributes['effect'] == 'Random'
    assert state.attributes['rgb_color'] == (42, 255, 255)

    state = hass.states.get('light.ceiling_lights')
    assert state.state == 'on'
    assert state.attributes['brightness'] == 128
    assert state.attributes['effect'] == 'Random'
    assert state.attributes['rgb_color'] == (42, 255, 255)

    state = hass.states.get('light.kitchen_lights')
    assert state.state == 'on'
    assert state.attributes['brightness'] == 128
    assert state.attributes['effect'] == 'Random'
    assert state.attributes['rgb_color'] == (42, 255, 255)
Beispiel #17
0
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
    """Test the sending of command in optimistic mode."""
    config = {light.DOMAIN: {
        'platform': 'mqtt',
        'name': 'test',
        'command_topic': 'test_light_rgb/set',
        'brightness_command_topic': 'test_light_rgb/brightness/set',
        'rgb_command_topic': 'test_light_rgb/rgb/set',
        'color_temp_command_topic': 'test_light_rgb/color_temp/set',
        'effect_command_topic': 'test_light_rgb/effect/set',
        'hs_command_topic': 'test_light_rgb/hs/set',
        'white_value_command_topic': 'test_light_rgb/white_value/set',
        'xy_command_topic': 'test_light_rgb/xy/set',
        'effect_list': ['colorloop', 'random'],
        'qos': 2,
        'payload_on': 'on',
        'payload_off': 'off'
    }}
    fake_state = ha.State('light.test', 'on', {'brightness': 95,
                                               'hs_color': [100, 100],
                                               'effect': 'random',
                                               'color_temp': 100,
                                               'white_value': 50})
    with patch('homeassistant.components.light.mqtt.async_get_last_state',
               return_value=mock_coro(fake_state)):
        with assert_setup_component(1, light.DOMAIN):
            assert await async_setup_component(hass, light.DOMAIN, config)

    state = hass.states.get('light.test')
    assert STATE_ON == state.state
    assert 95 == state.attributes.get('brightness')
    assert (100, 100) == state.attributes.get('hs_color')
    assert 'random' == state.attributes.get('effect')
    assert 100 == state.attributes.get('color_temp')
    assert 50 == state.attributes.get('white_value')
    assert state.attributes.get(ATTR_ASSUMED_STATE)

    common.async_turn_on(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with(
        'test_light_rgb/set', 'on', 2, False)
    mqtt_mock.async_publish.reset_mock()
    state = hass.states.get('light.test')
    assert STATE_ON == state.state

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with(
        'test_light_rgb/set', 'off', 2, False)
    mqtt_mock.async_publish.reset_mock()
    state = hass.states.get('light.test')
    assert STATE_OFF == state.state

    mqtt_mock.reset_mock()
    common.async_turn_on(hass, 'light.test',
                         brightness=50, xy_color=[0.123, 0.123])
    common.async_turn_on(hass, 'light.test',
                         brightness=50, hs_color=[359, 78])
    common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0],
                         white_value=80)
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_has_calls([
        mock.call('test_light_rgb/set', 'on', 2, False),
        mock.call('test_light_rgb/rgb/set', '255,128,0', 2, False),
        mock.call('test_light_rgb/brightness/set', 50, 2, False),
        mock.call('test_light_rgb/hs/set', '359.0,78.0', 2, False),
        mock.call('test_light_rgb/white_value/set', 80, 2, False),
        mock.call('test_light_rgb/xy/set', '0.14,0.131', 2, False),
    ], any_order=True)

    state = hass.states.get('light.test')
    assert STATE_ON == state.state
    assert (255, 128, 0) == state.attributes['rgb_color']
    assert 50 == state.attributes['brightness']
    assert (30.118, 100) == state.attributes['hs_color']
    assert 80 == state.attributes['white_value']
    assert (0.611, 0.375) == state.attributes['xy_color']
Beispiel #18
0
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
    """Test the sending of command in optimistic mode."""
    config = {
        light.DOMAIN: {
            'platform': 'mqtt',
            'name': 'test',
            'command_topic': 'test_light_rgb/set',
            'brightness_command_topic': 'test_light_rgb/brightness/set',
            'rgb_command_topic': 'test_light_rgb/rgb/set',
            'color_temp_command_topic': 'test_light_rgb/color_temp/set',
            'effect_command_topic': 'test_light_rgb/effect/set',
            'hs_command_topic': 'test_light_rgb/hs/set',
            'white_value_command_topic': 'test_light_rgb/white_value/set',
            'xy_command_topic': 'test_light_rgb/xy/set',
            'effect_list': ['colorloop', 'random'],
            'qos': 2,
            'payload_on': 'on',
            'payload_off': 'off'
        }
    }
    fake_state = ha.State(
        'light.test', 'on', {
            'brightness': 95,
            'hs_color': [100, 100],
            'effect': 'random',
            'color_temp': 100,
            'white_value': 50
        })
    with patch(
            'homeassistant.helpers.restore_state.RestoreEntity'
            '.async_get_last_state',
            return_value=mock_coro(fake_state)):
        with assert_setup_component(1, light.DOMAIN):
            assert await async_setup_component(hass, light.DOMAIN, config)

    state = hass.states.get('light.test')
    assert STATE_ON == state.state
    assert 95 == state.attributes.get('brightness')
    assert (100, 100) == state.attributes.get('hs_color')
    assert 'random' == state.attributes.get('effect')
    assert 100 == state.attributes.get('color_temp')
    assert 50 == state.attributes.get('white_value')
    assert state.attributes.get(ATTR_ASSUMED_STATE)

    common.async_turn_on(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with('test_light_rgb/set', 'on',
                                                    2, False)
    mqtt_mock.async_publish.reset_mock()
    state = hass.states.get('light.test')
    assert STATE_ON == state.state

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with('test_light_rgb/set',
                                                    'off', 2, False)
    mqtt_mock.async_publish.reset_mock()
    state = hass.states.get('light.test')
    assert STATE_OFF == state.state

    mqtt_mock.reset_mock()
    common.async_turn_on(hass,
                         'light.test',
                         brightness=50,
                         xy_color=[0.123, 0.123])
    common.async_turn_on(hass, 'light.test', brightness=50, hs_color=[359, 78])
    common.async_turn_on(hass,
                         'light.test',
                         rgb_color=[255, 128, 0],
                         white_value=80)
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_has_calls([
        mock.call('test_light_rgb/set', 'on', 2, False),
        mock.call('test_light_rgb/rgb/set', '255,128,0', 2, False),
        mock.call('test_light_rgb/brightness/set', 50, 2, False),
        mock.call('test_light_rgb/hs/set', '359.0,78.0', 2, False),
        mock.call('test_light_rgb/white_value/set', 80, 2, False),
        mock.call('test_light_rgb/xy/set', '0.14,0.131', 2, False),
    ],
                                             any_order=True)

    state = hass.states.get('light.test')
    assert STATE_ON == state.state
    assert (255, 128, 0) == state.attributes['rgb_color']
    assert 50 == state.attributes['brightness']
    assert (30.118, 100) == state.attributes['hs_color']
    assert 80 == state.attributes['white_value']
    assert (0.611, 0.375) == state.attributes['xy_color']