Esempio n. 1
0
    def test_measure(self):
        """Test the history statistics sensor measure."""
        t0 = dt_util.utcnow() - timedelta(minutes=40)
        t1 = t0 + timedelta(minutes=20)
        t2 = dt_util.utcnow() - timedelta(minutes=10)

        # Start     t0        t1        t2        End
        # |--20min--|--20min--|--10min--|--10min--|
        # |---off---|---on----|---off---|---on----|

        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"
        )

        assert sensor1._type == "time"
        assert sensor3._type == "count"
        assert 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()

        assert sensor1.state == 0.5
        assert sensor2.state is None
        assert sensor3.state == 2
        assert sensor4.state == 50
Esempio n. 2
0
    def test_measure(self):
        """Test the history statistics sensor measure."""
        t0 = dt_util.utcnow() - timedelta(minutes=40)
        t1 = t0 + timedelta(minutes=20)
        t2 = dt_util.utcnow() - timedelta(minutes=10)

        # Start     t0        t1        t2        End
        # |--20min--|--20min--|--10min--|--10min--|
        # |---off---|---on----|---off---|---on----|

        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')

        assert sensor1._type == 'time'
        assert sensor3._type == 'count'
        assert 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()

        assert sensor1.state == 0.5
        assert sensor2.state is None
        assert sensor3.state == 2
        assert sensor4.state == 50
Esempio n. 3
0
    def test_measure(self):
        """Test the history statistics sensor measure."""
        t0 = dt_util.utcnow() - timedelta(minutes=40)
        t1 = t0 + timedelta(minutes=20)
        t2 = dt_util.utcnow() - timedelta(minutes=10)

        # Start     t0        t1        t2        End
        # |--20min--|--20min--|--10min--|--10min--|
        # |---off---|---on----|---off---|---on----|

        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')

        assert sensor1._type == 'time'
        assert sensor3._type == 'count'
        assert 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()

        assert sensor1.state == 0.5
        assert sensor2.state is None
        assert sensor3.state == 2
        assert sensor4.state == 50