'Test the history statistics sensor measure.'

def test_measure(self):

    t0 = (dt_util.utcnow() - timedelta(minutes=40))
    t1 = (t0 + timedelta(minutes=20))
    t2 = (dt_util.utcnow() - timedelta(minutes=10))
    fake_states = {'binary_sensor.test_id': [ha.State('binary_sensor.test_id', 'on', last_changed=t0), ha.State('binary_sensor.test_id', 'off', last_changed=t1), ha.State('binary_sensor.test_id', 'on', last_changed=t2)]}
    start = Template('{{    as_timestamp(now())    -    3600    }}', self.hass)
    end = Template('{{    now()    }}', self.hass)
    sensor1 = HistoryStatsSensor(self.hass, 'binary_sensor.test_id', 'on', start, end, None, 'time', 'Test')
    sensor2 = HistoryStatsSensor(self.hass, 'unknown.id', 'on', start, end, None, 'time', 'Test')
    sensor3 = HistoryStatsSensor(self.hass, 'binary_sensor.test_id', 'on', start, end, None, 'count', 'test')
    sensor4 = HistoryStatsSensor(self.hass, 'binary_sensor.test_id', 'on', start, end, None, 'ratio', 'test')
    self.assertEqual(sensor1._type, 'time')
    self.assertEqual(sensor3._type, 'count')
    self.assertEqual(sensor4._type, 'ratio')
    with patch('homeassistant.components.history.state_changes_during_period', return_value=fake_states):
        with patch('homeassistant.components.history.get_state', return_value=None):
            sensor1.update()
            sensor2.update()
            sensor3.update()
            sensor4.update()
    self.assertEqual(sensor1.state, 0.5)
    self.assertEqual(sensor2.state, 0)
    self.assertEqual(sensor3.state, 2)
    self.assertEqual(sensor4.state, 50)
