コード例 #1
0
 def test_update(self):
     hass = mock.MagicMock()
     vs = template.BinarySensorTemplate(hass, 'parent', 'Parent', 'motion',
                                        '{{ 2 > 1 }}')
     self.assertEqual(None, vs._state)
     vs.update()
     self.assertTrue(vs._state)
コード例 #2
0
 def test_event(self):
     hass = mock.MagicMock()
     vs = template.BinarySensorTemplate(hass, 'parent', 'Parent', 'motion',
                                        '{{ 1 > 1 }}')
     with mock.patch.object(vs, 'update_ha_state') as mock_update:
         vs._event_listener(None)
         mock_update.assert_called_once_with(True)
コード例 #3
0
 def test_update_template_error(self, mock_render):
     hass = mock.MagicMock()
     vs = template.BinarySensorTemplate(hass, 'parent', 'Parent', 'motion',
                                        '{{ 1 > 1 }}')
     mock_render.side_effect = TemplateError('foo')
     vs.update()
     mock_render.side_effect = TemplateError(
         "UndefinedError: 'None' has no attribute")
     vs.update()
コード例 #4
0
 def test_update_template_error(self, mock_render):
     """"Test the template update error."""
     vs = template.BinarySensorTemplate(
         self.hass, 'parent', 'Parent', 'motion',
         template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL)
     mock_render.side_effect = TemplateError('foo')
     vs.update()
     mock_render.side_effect = TemplateError(
         "UndefinedError: 'None' has no attribute")
     vs.update()
コード例 #5
0
    def test_event(self):
        """"Test the event."""
        vs = template.BinarySensorTemplate(
            self.hass, 'parent', 'Parent', 'motion',
            template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL)
        vs.update_ha_state()
        self.hass.block_till_done()

        with mock.patch.object(vs, 'async_update') as mock_update:
            self.hass.bus.fire(EVENT_STATE_CHANGED)
            self.hass.block_till_done()
            assert mock_update.call_count == 1
コード例 #6
0
    def test_attributes(self):
        hass = mock.MagicMock()
        vs = template.BinarySensorTemplate(hass, 'parent', 'Parent', 'motion',
                                           '{{ 1 > 1 }}')
        self.assertFalse(vs.should_poll)
        self.assertEqual('motion', vs.sensor_class)
        self.assertEqual('Parent', vs.name)

        vs.update()
        self.assertFalse(vs.is_on)

        vs._template = "{{ 2 > 1 }}"
        vs.update()
        self.assertTrue(vs.is_on)
コード例 #7
0
    def test_event(self):
        """"Test the event."""
        hass = get_test_home_assistant()
        vs = template.BinarySensorTemplate(hass, 'parent', 'Parent',
                                           'motion', '{{ 1 > 1 }}', MATCH_ALL)
        vs.update_ha_state()
        hass.pool.block_till_done()

        with mock.patch.object(vs, 'update') as mock_update:
            hass.bus.fire(EVENT_STATE_CHANGED)
            hass.pool.block_till_done()
            try:
                assert mock_update.call_count == 1
            finally:
                hass.stop()
コード例 #8
0
    def test_attributes(self):
        """"Test the attributes."""
        vs = template.BinarySensorTemplate(
            self.hass, 'parent', 'Parent', 'motion',
            template_hlpr.Template('{{ 1 > 1 }}', self.hass), MATCH_ALL)
        self.assertFalse(vs.should_poll)
        self.assertEqual('motion', vs.sensor_class)
        self.assertEqual('Parent', vs.name)

        vs.update()
        self.assertFalse(vs.is_on)

        vs._template = template_hlpr.Template("{{ 2 > 1 }}", self.hass)
        vs.update()
        self.assertTrue(vs.is_on)