'"Test the sending of a new message if event changed.'

@patch('homeassistant.components.mqtt.async_publish')
@patch('homeassistant.core.dt_util.utcnow')
def test_state_changed_event_sends_message(self, mock_utcnow, mock_pub):

    now = dt_util.as_utc(dt_util.now())
    e_id = 'fake.entity'
    pub_topic = 'bar'
    mock_utcnow.return_value = now
    assert self.add_eventstream(pub_topic=pub_topic)
    self.hass.block_till_done()
    mock_pub.reset_mock()
    mock_state_change_event(self.hass, State(e_id, 'on'))
    self.hass.block_till_done()
    mock_pub.assert_called_with(self.hass, pub_topic, ANY)
    assert mock_pub.called
    msg = mock_pub.call_args[0][2]
    event = {}
    event['event_type'] = EVENT_STATE_CHANGED
    new_state = {'last_updated': now.isoformat(), 'state': 'on', 'entity_id': e_id, 'attributes': {}, 'last_changed': now.isoformat()}
    event['event_data'] = {'new_state': new_state, 'entity_id': e_id}
    assert (json.loads(msg) == event)
