'Test the expiration of the value.'

@patch('homeassistant.core.dt_util.utcnow')
def test_setting_sensor_value_expires(self, mock_utcnow):

    mock_component(self.hass, 'mqtt')
    assert setup_component(self.hass, sensor.DOMAIN, {sensor.DOMAIN: {'platform': 'mqtt', 'name': 'test', 'state_topic': 'test-topic', 'unit_of_measurement': 'fav    unit', 'expire_after': '4', 'force_update': True}})
    state = self.hass.states.get('sensor.test')
    self.assertEqual('unknown', state.state)
    now = datetime(2017, 1, 1, 1, tzinfo=dt_util.UTC)
    mock_utcnow.return_value = now
    fire_mqtt_message(self.hass, 'test-topic', '100')
    self.hass.block_till_done()
    state = self.hass.states.get('sensor.test')
    self.assertEqual('100', state.state)
    now = (now + timedelta(seconds=3))
    self._send_time_changed(now)
    self.hass.block_till_done()
    state = self.hass.states.get('sensor.test')
    self.assertEqual('100', state.state)
    mock_utcnow.return_value = now
    fire_mqtt_message(self.hass, 'test-topic', '101')
    self.hass.block_till_done()
    state = self.hass.states.get('sensor.test')
    self.assertEqual('101', state.state)
    now = (now + timedelta(seconds=3))
    self._send_time_changed(now)
    self.hass.block_till_done()
    state = self.hass.states.get('sensor.test')
    self.assertEqual('101', state.state)
    now = (now + timedelta(seconds=2))
    self._send_time_changed(now)
    self.hass.block_till_done()
    state = self.hass.states.get('sensor.test')
    self.assertEqual('unknown', state.state)
