コード例 #1
0
ファイル: test_command_line.py プロジェクト: lumavp/blumate
    def test_state_json_value(self):
        """Test with state JSON value."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, 'switch_status')
            oncmd = json.dumps({'status': 'ok'})
            offcmd = json.dumps({'status': 'nope'})
            test_switch = {
                'statecmd': 'cat {}'.format(path),
                'oncmd': 'echo \'{}\' > {}'.format(oncmd, path),
                'offcmd': 'echo \'{}\' > {}'.format(offcmd, path),
                'value_template': '{{ value_json.status=="ok" }}'
            }
            self.assertTrue(switch.setup(self.hass, {
                'switch': {
                    'platform': 'command_line',
                    'switches': {
                        'test': test_switch
                    }
                }
            }))

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_OFF, state.state)

            switch.turn_on(self.hass, 'switch.test')
            self.hass.pool.block_till_done()

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_ON, state.state)

            switch.turn_off(self.hass, 'switch.test')
            self.hass.pool.block_till_done()

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_OFF, state.state)
コード例 #2
0
ファイル: test_template.py プロジェクト: lumavp/blumate
    def test_template_state_text(self):
        """"Test the state text of a template."""
        assert switch.setup(self.hass, {
            'switch': {
                'platform': 'template',
                'switches': {
                    'test_template_switch': {
                        'value_template':
                            "{{ states.switch.test_state.state }}",
                        'turn_on': {
                            'service': 'switch.turn_on',
                            'entity_id': 'switch.test_state'
                        },
                        'turn_off': {
                            'service': 'switch.turn_off',
                            'entity_id': 'switch.test_state'
                        },
                    }
                }
            }
        })

        state = self.hass.states.set('switch.test_state', STATE_ON)
        self.hass.pool.block_till_done()

        state = self.hass.states.get('switch.test_template_switch')
        assert state.state == STATE_ON

        state = self.hass.states.set('switch.test_state', STATE_OFF)
        self.hass.pool.block_till_done()

        state = self.hass.states.get('switch.test_template_switch')
        assert state.state == STATE_OFF
コード例 #3
0
ファイル: test_command_line.py プロジェクト: lumavp/blumate
    def test_state_none(self):
        """Test with none state."""
        with tempfile.TemporaryDirectory() as tempdirname:
            path = os.path.join(tempdirname, 'switch_status')
            test_switch = {
                'oncmd': 'echo 1 > {}'.format(path),
                'offcmd': 'echo 0 > {}'.format(path),
            }
            self.assertTrue(switch.setup(self.hass, {
                'switch': {
                    'platform': 'command_line',
                    'switches': {
                        'test': test_switch
                    }
                }
            }))

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_OFF, state.state)

            switch.turn_on(self.hass, 'switch.test')
            self.hass.pool.block_till_done()

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_ON, state.state)

            switch.turn_off(self.hass, 'switch.test')
            self.hass.pool.block_till_done()

            state = self.hass.states.get('switch.test')
            self.assertEqual(STATE_OFF, state.state)
コード例 #4
0
ファイル: test_template.py プロジェクト: lumavp/blumate
    def test_off_action(self):
        """Test off action."""
        assert switch.setup(self.hass, {
            'switch': {
                'platform': 'template',
                'switches': {
                    'test_template_switch': {
                        'value_template':
                            "{{ states.switch.test_state.state }}",
                        'turn_on': {
                            'service': 'switch.turn_on',
                            'entity_id': 'switch.test_state'

                        },
                        'turn_off': {
                            'service': 'test.automation'
                        },
                    }
                }
            }
        })
        self.hass.states.set('switch.test_state', STATE_ON)
        self.hass.pool.block_till_done()

        state = self.hass.states.get('switch.test_template_switch')
        assert state.state == STATE_ON

        core.switch.turn_off(self.hass, 'switch.test_template_switch')
        self.hass.pool.block_till_done()

        assert 1 == len(self.calls)
コード例 #5
0
ファイル: test_template.py プロジェクト: lumavp/blumate
    def test_template_syntax_error(self):
        """Test templating syntax error."""
        assert switch.setup(self.hass, {
            'switch': {
                'platform': 'template',
                'switches': {
                    'test_template_switch': {
                        'value_template':
                            "{% if rubbish %}",
                        'turn_on': {
                            'service': 'switch.turn_on',
                            'entity_id': 'switch.test_state'
                        },
                        'turn_off': {
                            'service': 'switch.turn_off',
                            'entity_id': 'switch.test_state'
                        },
                    }
                }
            }
        })

        state = self.hass.states.set('switch.test_state', STATE_ON)
        self.hass.pool.block_till_done()
        state = self.hass.states.get('switch.test_template_switch')
        assert state.state == 'unavailable'
コード例 #6
0
ファイル: test_template.py プロジェクト: lumavp/blumate
 def test_no_switches_does_not_create(self):
     """Test if there are no switches no creation."""
     assert switch.setup(self.hass, {
         'switch': {
             'platform': 'template'
         }
     })
     assert self.hass.states.all() == []
コード例 #7
0
ファイル: test_template.py プロジェクト: lumavp/blumate
 def test_invalid_switch_does_not_create(self):
     """Test invalid name."""
     assert switch.setup(self.hass, {
         'switch': {
             'platform': 'template',
             'switches': {
                 'test_template_switch': 'Invalid'
             }
         }
     })
     assert self.hass.states.all() == []
コード例 #8
0
ファイル: test_init.py プロジェクト: lumavp/blumate
    def setUp(self):  # pylint: disable=invalid-name
        """Setup things to be run when tests are started."""
        self.hass = get_test_home_assistant()
        platform = loader.get_component('switch.test')
        platform.init()
        self.assertTrue(switch.setup(
            self.hass, {switch.DOMAIN: {CONF_PLATFORM: 'test'}}
        ))

        # Switch 1 is ON, switch 2 is OFF
        self.switch_1, self.switch_2, self.switch_3 = \
            platform.DEVICES
コード例 #9
0
ファイル: test_init.py プロジェクト: lumavp/blumate
    def test_setup_two_platforms(self):
        """Test with bad configuration."""
        # Test if switch component returns 0 switches
        test_platform = loader.get_component('switch.test')
        test_platform.init(True)

        loader.set_component('switch.test2', test_platform)
        test_platform.init(False)

        self.assertTrue(switch.setup(
            self.hass, {
                switch.DOMAIN: {CONF_PLATFORM: 'test'},
                '{} 2'.format(switch.DOMAIN): {CONF_PLATFORM: 'test2'},
            }
        ))
コード例 #10
0
ファイル: test_template.py プロジェクト: lumavp/blumate
 def test_missing_off_does_not_create(self):
     """Test missing off."""
     assert switch.setup(self.hass, {
         'switch': {
             'platform': 'template',
             'switches': {
                 'test_template_switch': {
                     'value_template':
                         "{{ states.switch.test_state.state }}",
                     'turn_on': {
                         'service': 'switch.turn_on',
                         'entity_id': 'switch.test_state'
                     },
                     'not_off': {
                         'service': 'switch.turn_off',
                         'entity_id': 'switch.test_state'
                     },
                 }
             }
         }
     })
     assert self.hass.states.all() == []
コード例 #11
0
ファイル: test_template.py プロジェクト: lumavp/blumate
 def test_invalid_name_does_not_create(self):
     """Test invalid name."""
     assert switch.setup(self.hass, {
         'switch': {
             'platform': 'template',
             'switches': {
                 'test INVALID switch': {
                     'value_template':
                         "{{ rubbish }",
                     'turn_on': {
                         'service': 'switch.turn_on',
                         'entity_id': 'switch.test_state'
                     },
                     'turn_off': {
                         'service': 'switch.turn_off',
                         'entity_id': 'switch.test_state'
                     },
                 }
             }
         }
     })
     assert self.hass.states.all() == []
コード例 #12
0
ファイル: test_template.py プロジェクト: lumavp/blumate
    def test_template_state_boolean_off(self):
        """Test the setting of the state with off."""
        assert switch.setup(self.hass, {
            'switch': {
                'platform': 'template',
                'switches': {
                    'test_template_switch': {
                        'value_template':
                            "{{ 1 == 2 }}",
                        'turn_on': {
                            'service': 'switch.turn_on',
                            'entity_id': 'switch.test_state'
                        },
                        'turn_off': {
                            'service': 'switch.turn_off',
                            'entity_id': 'switch.test_state'
                        },
                    }
                }
            }
        })

        state = self.hass.states.get('switch.test_template_switch')
        assert state.state == STATE_OFF