async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish):
    'Test the sending MQTT commands in optimistic mode.'
    fake_state = ha.State('switch.test', 'on')
    with patch('homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state', return_value=mock_coro(fake_state)):
        assert (await async_setup_component(hass, switch.DOMAIN, {switch.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'payload_on': 'beer on', 'payload_off': 'beer off', 'qos': '2'}}))
    state = hass.states.get('switch.test')
    assert (STATE_ON == state.state)
    assert state.attributes.get(ATTR_ASSUMED_STATE)
    common.turn_on(hass, 'switch.test')
    (await hass.async_block_till_done())
    mock_publish.async_publish.assert_called_once_with('command-topic', 'beer on', 2, False)
    mock_publish.async_publish.reset_mock()
    state = hass.states.get('switch.test')
    assert (STATE_ON == state.state)
    common.turn_off(hass, 'switch.test')
    (await hass.async_block_till_done())
    (await hass.async_block_till_done())
    mock_publish.async_publish.assert_called_once_with('command-topic', 'beer off', 2, False)
    state = hass.states.get('switch.test')
    assert (STATE_OFF == state.state)
Test the sending MQTT commands in optimistic mode.
async def test_sending_mqtt_commands_and_optimistic(hass, mock_publish):
    
    fake_state = ha.State('switch.test', 'on')
    with patch('homeassistant.helpers.restore_state.RestoreEntity.async_get_last_state', return_value=mock_coro(fake_state)):
        assert (await async_setup_component(hass, switch.DOMAIN, {switch.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'command_topic': 'command-topic', 'payload_on': 'beer on', 'payload_off': 'beer off', 'qos': '2'}}))
    state = hass.states.get('switch.test')
    assert (STATE_ON == state.state)
    assert state.attributes.get(ATTR_ASSUMED_STATE)
    common.turn_on(hass, 'switch.test')
    (await hass.async_block_till_done())
    mock_publish.async_publish.assert_called_once_with('command-topic', 'beer on', 2, False)
    mock_publish.async_publish.reset_mock()
    state = hass.states.get('switch.test')
    assert (STATE_ON == state.state)
    common.turn_off(hass, 'switch.test')
    (await hass.async_block_till_done())
    (await hass.async_block_till_done())
    mock_publish.async_publish.assert_called_once_with('command-topic', 'beer off', 2, False)
    state = hass.states.get('switch.test')
    assert (STATE_OFF == state.state)