Beispiel #1
0
    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)
Beispiel #2
0
    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
Beispiel #3
0
    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)
Beispiel #4
0
    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)
Beispiel #5
0
    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'
Beispiel #6
0
 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() == []
Beispiel #7
0
 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() == []
Beispiel #8
0
    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
Beispiel #9
0
    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'},
            }
        ))
Beispiel #10
0
 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() == []
Beispiel #11
0
 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() == []
Beispiel #12
0
    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