Esempio n. 1
0
 def test_setup_minimum(self, mock_req):
     """Test setup with minimum configuration."""
     mock_req.get('http://localhost', status_code=200)
     self.assertTrue(setup_component(self.hass, 'sensor', {
         'sensor': {
             'platform': 'rest',
             'resource': 'http://localhost'
         }
     }))
     self.assertEqual(2, mock_req.call_count)
     assert_setup_component(1, 'switch')
 def test_setup_minimum(self, mock_req):
     """Test setup with minimum configuration."""
     resource = '{}{}{}'.format(
         'http://', google_wifi.DEFAULT_HOST, google_wifi.ENDPOINT)
     mock_req.get(resource, status_code=200)
     self.assertTrue(setup_component(self.hass, 'sensor', {
         'sensor': {
             'platform': 'google_wifi',
             'monitored_conditions': ['uptime']
         }
     }))
     assert_setup_component(1, 'sensor')
Esempio n. 3
0
    def test_validate_platform_config_2(self, caplog):
        """Test component PLATFORM_SCHEMA_BASE prio over PLATFORM_SCHEMA."""
        platform_schema = PLATFORM_SCHEMA.extend({
            'hello': str,
        })
        platform_schema_base = PLATFORM_SCHEMA_BASE.extend({
            'hello': 'world',
        })
        loader.set_component(
            self.hass,
            'platform_conf',
            MockModule('platform_conf',
                       platform_schema=platform_schema,
                       platform_schema_base=platform_schema_base))

        loader.set_component(
            self.hass,
            'platform_conf.whatever',
            MockPlatform('whatever',
                         platform_schema=platform_schema))

        with assert_setup_component(1):
            assert setup.setup_component(self.hass, 'platform_conf', {
                # fail: no extra keys allowed in platform schema
                'platform_conf': {
                    'platform': 'whatever',
                    'hello': 'world',
                    'invalid': 'extra',
                }
            })
            assert caplog.text.count('Your configuration contains '
                                     'extra keys') == 1

        self.hass.data.pop(setup.DATA_SETUP)
        self.hass.config.components.remove('platform_conf')

        with assert_setup_component(1):
            assert setup.setup_component(self.hass, 'platform_conf', {
                # pass
                'platform_conf': {
                    'platform': 'whatever',
                    'hello': 'world',
                },
                # fail: key hello violates component platform_schema_base
                'platform_conf 2': {
                    'platform': 'whatever',
                    'hello': 'there'
                }
            })

        self.hass.data.pop(setup.DATA_SETUP)
        self.hass.config.components.remove('platform_conf')
Esempio n. 4
0
    def test_validate_platform_config_3(self):
        """Test fallback to component PLATFORM_SCHEMA."""
        component_schema = PLATFORM_SCHEMA_BASE.extend({
            'hello': str,
        })
        platform_schema = PLATFORM_SCHEMA.extend({
            'cheers': str,
            'hello': 'world',
        })
        loader.set_component(
            self.hass,
            'platform_conf',
            MockModule('platform_conf',
                       platform_schema=component_schema))

        loader.set_component(
            self.hass,
            'platform_conf.whatever',
            MockPlatform('whatever',
                         platform_schema=platform_schema))

        with assert_setup_component(0):
            assert setup.setup_component(self.hass, 'platform_conf', {
                'platform_conf': {
                    # fail: no extra keys allowed
                    'platform': 'whatever',
                    'hello': 'world',
                    'invalid': 'extra',
                }
            })

        self.hass.data.pop(setup.DATA_SETUP)
        self.hass.config.components.remove('platform_conf')

        with assert_setup_component(1):
            assert setup.setup_component(self.hass, 'platform_conf', {
                # pass
                'platform_conf': {
                    'platform': 'whatever',
                    'hello': 'world',
                },
                # fail: key hello violates component platform_schema
                'platform_conf 2': {
                    'platform': 'whatever',
                    'hello': 'there'
                }
            })

        self.hass.data.pop(setup.DATA_SETUP)
        self.hass.config.components.remove('platform_conf')
Esempio n. 5
0
 def test_setup(self, mock_req):
     """Test setup with valid configuration."""
     mock_req.get('localhost', status_code=200)
     self.assertTrue(setup_component(self.hass, 'switch', {
         'switch': {
             'platform': 'rest',
             'name': 'foo',
             'resource': 'http://localhost',
             'body_on': 'custom on text',
             'body_off': 'custom off text',
         }
     }))
     self.assertEqual(1, mock_req.call_count)
     assert_setup_component(1, 'switch')
Esempio n. 6
0
 def test_setup(self, aioclient_mock):
     """Test setup with valid configuration."""
     aioclient_mock.get('http://localhost', status=200)
     assert setup_component(self.hass, 'switch', {
         'switch': {
             'platform': 'rest',
             'name': 'foo',
             'resource': 'http://localhost',
             'body_on': 'custom on text',
             'body_off': 'custom off text',
         }
     })
     assert aioclient_mock.call_count == 1
     assert_setup_component(1, 'switch')
Esempio n. 7
0
 def test_multi_jails(self):
     """Test that multiple jails can be set up as sensors.."""
     config = {
         'sensor': {
             'platform': 'fail2ban',
             'jails': ['jail_one', 'jail_two']
         }
     }
     mock_fh = MockOpen()
     with patch('homeassistant.components.sensor.fail2ban.open', mock_fh,
                create=True):
         assert setup_component(self.hass, 'sensor', config)
         self.hass.block_till_done()
     assert_setup_component(2, 'sensor')
Esempio n. 8
0
    def test_setup_component_invalidprovince(self):
        """Setup workday component."""
        with assert_setup_component(1, 'binary_sensor'):
            setup_component(self.hass, 'binary_sensor',
                            self.config_invalidprovince)

        assert self.hass.states.get('binary_sensor.workday_sensor') is None
Esempio n. 9
0
    def test_see_service(self, mock_see):
        """Test the see service with a unicode dev_id and NO MAC."""
        with assert_setup_component(1, device_tracker.DOMAIN):
            assert setup_component(self.hass, device_tracker.DOMAIN,
                                   TEST_PLATFORM)
        params = {
            'dev_id': 'some_device',
            'host_name': 'example.com',
            'location_name': 'Work',
            'gps': [.3, .8],
            'attributes': {
                'test': 'test'
            }
        }
        device_tracker.see(self.hass, **params)
        self.hass.block_till_done()
        assert mock_see.call_count == 1
        assert mock_see.call_count == 1
        assert mock_see.call_args == call(**params)

        mock_see.reset_mock()
        params['dev_id'] += chr(233)  # e' acute accent from icloud

        device_tracker.see(self.hass, **params)
        self.hass.block_till_done()
        assert mock_see.call_count == 1
        assert mock_see.call_count == 1
        assert mock_see.call_args == call(**params)
Esempio n. 10
0
    def test_new_device_event_fired(self):
        """Test that the device tracker will fire an event."""
        with assert_setup_component(1, device_tracker.DOMAIN):
            assert setup_component(self.hass, device_tracker.DOMAIN,
                                   TEST_PLATFORM)
        test_events = []

        @callback
        def listener(event):
            """Record that our event got called."""
            test_events.append(event)

        self.hass.bus.listen("device_tracker_new_device", listener)

        device_tracker.see(self.hass, 'mac_1', host_name='hello')
        device_tracker.see(self.hass, 'mac_1', host_name='hello')

        self.hass.block_till_done()

        assert len(test_events) == 1

        # Assert we can serialize the event
        json.dumps(test_events[0].as_dict(), cls=JSONEncoder)

        assert test_events[0].data == {
            'entity_id': 'device_tracker.hello',
            'host_name': 'hello',
            'mac': 'MAC_1',
        }
Esempio n. 11
0
    def test_missing_off_does_not_create(self):
        """Test missing off."""
        with assert_setup_component(0, 'switch'):
            assert setup.setup_component(self.hass, 'switch', {
                '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'
                            },
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        assert self.hass.states.all() == []
Esempio n. 12
0
    def test_template_state_boolean_on(self):
        """Test the setting of the state with boolean on."""
        with assert_setup_component(1, 'switch'):
            assert setup.setup_component(self.hass, 'switch', {
                'switch': {
                    'platform': 'template',
                    'switches': {
                        'test_template_switch': {
                            'value_template':
                                "{{ 1 == 1 }}",
                            'turn_on': {
                                'service': 'switch.turn_on',
                                'entity_id': 'switch.test_state'
                            },
                            'turn_off': {
                                'service': 'switch.turn_off',
                                'entity_id': 'switch.test_state'
                            },
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get('switch.test_template_switch')
        assert state.state == STATE_ON
Esempio n. 13
0
    def test_template_syntax_error(self):
        """Test templating syntax error."""
        with assert_setup_component(0, 'switch'):
            assert setup.setup_component(self.hass, 'switch', {
                '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'
                            },
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        assert self.hass.states.all() == []
Esempio n. 14
0
    def test_invalid_name_does_not_create(self):
        """Test invalid name."""
        with assert_setup_component(0, 'switch'):
            assert setup.setup_component(self.hass, 'switch', {
                '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'
                            },
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        assert self.hass.states.all() == []
Esempio n. 15
0
def test_websocket_api(hass):
    """Test API streams."""
    log = logging.getLogger('homeassistant.components.websocket_api')

    with assert_setup_component(1):
        yield from async_setup_component(hass, 'sensor', {
            'sensor': {
                'platform': 'api_streams',
            }
        })

    state = hass.states.get('sensor.connected_clients')
    assert state.state == '0'

    log.debug('WS %s: %s', id(log), 'Connected')
    yield from hass.async_block_till_done()

    state = hass.states.get('sensor.connected_clients')
    assert state.state == '1'

    log.debug('WS %s: %s', id(log), 'Connected')
    yield from hass.async_block_till_done()

    state = hass.states.get('sensor.connected_clients')
    assert state.state == '2'

    log.debug('WS %s: %s', id(log), 'Closed connection')
    yield from hass.async_block_till_done()

    state = hass.states.get('sensor.connected_clients')
    assert state.state == '1'
Esempio n. 16
0
def test_api_streams(hass):
    """Test API streams."""
    log = logging.getLogger('homeassistant.components.api')

    with assert_setup_component(1):
        yield from async_setup_component(hass, 'sensor', {
            'sensor': {
                'platform': 'api_streams',
            }
        })

    state = hass.states.get('sensor.connected_clients')
    assert state.state == '0'

    log.debug('STREAM 1 ATTACHED')
    yield from hass.async_block_till_done()

    state = hass.states.get('sensor.connected_clients')
    assert state.state == '1'

    log.debug('STREAM 1 ATTACHED')
    yield from hass.async_block_till_done()

    state = hass.states.get('sensor.connected_clients')
    assert state.state == '2'

    log.debug('STREAM 1 RESPONSE CLOSED')
    yield from hass.async_block_till_done()

    state = hass.states.get('sensor.connected_clients')
    assert state.state == '1'
Esempio n. 17
0
    def test_close_tilt_action(self):
        """Test the close_cover_tilt command."""
        with assert_setup_component(1, 'cover'):
            assert setup.setup_component(self.hass, 'cover', {
                'cover': {
                    'platform': 'template',
                    'covers': {
                        'test_template_cover': {
                            'position_template':
                                "{{ 100 }}",
                            'open_cover': {
                                'service': 'cover.open_cover',
                                'entity_id': 'cover.test_state'
                            },
                            'close_cover': {
                                'service': 'cover.close_cover',
                                'entity_id': 'cover.test_state'
                            },
                            'set_cover_tilt_position': {
                                'service': 'test.automation',
                            },
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        cover.close_cover_tilt(self.hass, 'cover.test_template_cover')
        self.hass.block_till_done()

        assert len(self.calls) == 1
Esempio n. 18
0
    def test_template_state_boolean(self):
        """Test the value_template attribute."""
        with assert_setup_component(1, 'cover'):
            assert setup.setup_component(self.hass, 'cover', {
                'cover': {
                    'platform': 'template',
                    'covers': {
                        'test_template_cover': {
                            'value_template':
                                "{{ 1 == 1 }}",
                            'open_cover': {
                                'service': 'cover.open_cover',
                                'entity_id': 'cover.test_state'
                            },
                            'close_cover': {
                                'service': 'cover.close_cover',
                                'entity_id': 'cover.test_state'
                            },
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get('cover.test_template_cover')
        assert state.state == STATE_OPEN
Esempio n. 19
0
    def test_template_non_numeric(self):
        """Test that tilt_template values are numeric."""
        with assert_setup_component(1, 'cover'):
            assert setup.setup_component(self.hass, 'cover', {
                'cover': {
                    'platform': 'template',
                    'covers': {
                        'test_template_cover': {
                            'position_template':
                                "{{ on }}",
                            'tilt_template':
                                "{% if states.cover.test_state.state %}"
                                "on"
                                "{% else %}"
                                "off"
                                "{% endif %}",
                            'open_cover': {
                                'service': 'cover.open_cover',
                                'entity_id': 'cover.test_state'
                            },
                            'close_cover': {
                                'service': 'cover.close_cover',
                                'entity_id': 'cover.test_state'
                            },
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get('cover.test_template_cover')
        assert state.attributes.get('current_tilt_position') is None
        assert state.attributes.get('current_position') is None
Esempio n. 20
0
    def test_open_action(self):
        """Test the open_cover command."""
        with assert_setup_component(1, 'cover'):
            assert setup.setup_component(self.hass, 'cover', {
                'cover': {
                    'platform': 'template',
                    'covers': {
                        'test_template_cover': {
                            'position_template':
                                "{{ 0 }}",
                            'open_cover': {
                                'service': 'test.automation',
                            },
                            'close_cover': {
                                'service': 'cover.close_cover',
                                'entity_id': 'cover.test_state'
                            },
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get('cover.test_template_cover')
        assert state.state == STATE_CLOSED

        cover.open_cover(self.hass, 'cover.test_template_cover')
        self.hass.block_till_done()

        assert len(self.calls) == 1
Esempio n. 21
0
    def test_template_out_of_bounds(self):
        """Test template out-of-bounds condition."""
        with assert_setup_component(1, 'cover'):
            assert setup.setup_component(self.hass, 'cover', {
                'cover': {
                    'platform': 'template',
                    'covers': {
                        'test_template_cover': {
                            'position_template':
                                "{{ -1 }}",
                            'tilt_template':
                                "{{ 110 }}",
                            'open_cover': {
                                'service': 'cover.open_cover',
                                'entity_id': 'cover.test_state'
                            },
                            'close_cover': {
                                'service': 'cover.close_cover',
                                'entity_id': 'cover.test_state'
                            },
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get('cover.test_template_cover')
        assert state.attributes.get('current_tilt_position') is None
        assert state.attributes.get('current_position') is None
Esempio n. 22
0
    def test_template_mutex(self):
        """Test that only value or position template can be used."""
        with assert_setup_component(0, 'cover'):
            assert setup.setup_component(self.hass, 'cover', {
                'cover': {
                    'platform': 'template',
                    'covers': {
                        'test_template_cover': {
                            'value_template':
                                "{{ 1 == 1 }}",
                            'position_template':
                                "{{ 42 }}",
                            'open_cover': {
                                'service': 'cover.open_cover',
                                'entity_id': 'cover.test_state'
                            },
                            'close_cover': {
                                'service': 'cover.close_cover',
                                'entity_id': 'cover.test_state'
                            },
                            'icon_template':
                                "{% if states.cover.test_state.state %}"
                                "mdi:check"
                                "{% endif %}"
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        assert self.hass.states.all() == []
Esempio n. 23
0
    def test_setup_component(self):
        """Setup ffmpeg component."""
        with assert_setup_component(1, 'binary_sensor'):
            setup_component(self.hass, 'binary_sensor', self.config)

        assert self.hass.data['ffmpeg'].binary == 'ffmpeg'
        assert self.hass.states.get('binary_sensor.ffmpeg_motion') is not None
Esempio n. 24
0
    def test_template_tilt(self):
        """Test the tilt_template attribute."""
        with assert_setup_component(1, 'cover'):
            assert setup.setup_component(self.hass, 'cover', {
                'cover': {
                    'platform': 'template',
                    'covers': {
                        'test_template_cover': {
                            'value_template':
                                "{{ 1 == 1 }}",
                            'tilt_template':
                                "{{ 42 }}",
                            'open_cover': {
                                'service': 'cover.open_cover',
                                'entity_id': 'cover.test_state'
                            },
                            'close_cover': {
                                'service': 'cover.close_cover',
                                'entity_id': 'cover.test_state'
                            },
                        }
                    }
                }
            })

        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get('cover.test_template_cover')
        assert state.attributes.get('current_tilt_position') == 42.0
    def test_event(self):
        """"Test the event."""
        config = {
            'binary_sensor': {
                'platform': 'template',
                'sensors': {
                    'test': {
                        'friendly_name': 'virtual thingy',
                        'value_template':
                            "{{ states.sensor.test_state.state == 'on' }}",
                        'device_class': 'motion',
                    },
                },
            },
        }
        with assert_setup_component(1):
            assert setup.setup_component(
                self.hass, 'binary_sensor', config)

        self.hass.start()
        self.hass.block_till_done()

        state = self.hass.states.get('binary_sensor.test')
        assert state.state == 'off'

        self.hass.states.set('sensor.test_state', 'on')
        self.hass.block_till_done()

        state = self.hass.states.get('binary_sensor.test')
        assert state.state == 'on'
Esempio n. 26
0
    def test_reload_config_when_invalid_config(self, mock_load_yaml):
        """Test the reload config service handling invalid config."""
        with assert_setup_component(1):
            assert setup_component(self.hass, automation.DOMAIN, {
                automation.DOMAIN: {
                    'alias': 'hello',
                    'trigger': {
                        'platform': 'event',
                        'event_type': 'test_event',
                    },
                    'action': {
                        'service': 'test.automation',
                        'data_template': {
                            'event': '{{ trigger.event.event_type }}'
                        }
                    }
                }
            })
        assert self.hass.states.get('automation.hello') is not None

        self.hass.bus.fire('test_event')
        self.hass.block_till_done()

        assert len(self.calls) == 1
        assert self.calls[0].data.get('event') == 'test_event'

        automation.reload(self.hass)
        self.hass.block_till_done()

        assert self.hass.states.get('automation.hello') is None

        self.hass.bus.fire('test_event')
        self.hass.block_till_done()
        assert len(self.calls) == 1
Esempio n. 27
0
    def test_scan_devices(self):
        """Test creating device info (MAC, name) from response.

        The created known_devices.yaml device info is compared
        to the DD-WRT Lan Status request response fixture.
        This effectively checks the data parsing functions.
        """
        with requests_mock.Mocker() as mock_request:
            mock_request.register_uri(
                'GET', r'http://%s/Status_Wireless.live.asp' % TEST_HOST,
                text=load_fixture('Ddwrt_Status_Wireless.txt'))
            mock_request.register_uri(
                'GET', r'http://%s/Status_Lan.live.asp' % TEST_HOST,
                text=load_fixture('Ddwrt_Status_Lan.txt'))

            with assert_setup_component(1):
                assert setup_component(
                    self.hass, DOMAIN, {DOMAIN: {
                        CONF_PLATFORM: 'ddwrt',
                        CONF_HOST: TEST_HOST,
                        CONF_USERNAME: '******',
                        CONF_PASSWORD: '******'
                    }})
                self.hass.block_till_done()

            path = self.hass.config.path(device_tracker.YAML_DEVICES)
            devices = config.load_yaml_config_file(path)
            for device in devices:
                self.assertIn(
                    devices[device]['mac'],
                    load_fixture('Ddwrt_Status_Lan.txt'))
                self.assertIn(
                    slugify(devices[device]['name']),
                    load_fixture('Ddwrt_Status_Lan.txt'))
Esempio n. 28
0
    def test_device_name_no_dhcp(self):
        """Test creating device info (MAC) when missing dhcp response."""
        with requests_mock.Mocker() as mock_request:
            mock_request.register_uri(
                'GET', r'http://%s/Status_Wireless.live.asp' % TEST_HOST,
                text=load_fixture('Ddwrt_Status_Wireless.txt'))
            mock_request.register_uri(
                'GET', r'http://%s/Status_Lan.live.asp' % TEST_HOST,
                text=load_fixture('Ddwrt_Status_Lan.txt').
                replace('dhcp_leases', 'missing'))

            with assert_setup_component(1):
                assert setup_component(
                    self.hass, DOMAIN, {DOMAIN: {
                        CONF_PLATFORM: 'ddwrt',
                        CONF_HOST: TEST_HOST,
                        CONF_USERNAME: '******',
                        CONF_PASSWORD: '******'
                    }})
                self.hass.block_till_done()

            path = self.hass.config.path(device_tracker.YAML_DEVICES)
            devices = config.load_yaml_config_file(path)
            for device in devices:
                _LOGGER.error(devices[device])
                self.assertIn(
                    devices[device]['mac'],
                    load_fixture('Ddwrt_Status_Lan.txt'))
Esempio n. 29
0
    def test_update_stale(self):
        """Test stalled update."""
        scanner = get_component(self.hass, 'device_tracker.test').SCANNER
        scanner.reset()
        scanner.come_home('DEV1')

        register_time = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
        scan_time = datetime(2015, 9, 15, 23, 1, tzinfo=dt_util.UTC)

        with patch('homeassistant.components.device_tracker.dt_util.utcnow',
                   return_value=register_time):
            with assert_setup_component(1, device_tracker.DOMAIN):
                assert setup_component(self.hass, device_tracker.DOMAIN, {
                    device_tracker.DOMAIN: {
                        CONF_PLATFORM: 'test',
                        device_tracker.CONF_CONSIDER_HOME: 59,
                    }})
                self.hass.block_till_done()

        assert STATE_HOME == \
            self.hass.states.get('device_tracker.dev1').state

        scanner.leave_home('DEV1')

        with patch('homeassistant.components.device_tracker.dt_util.utcnow',
                   return_value=scan_time):
            fire_time_changed(self.hass, scan_time)
            self.hass.block_till_done()

        assert STATE_NOT_HOME == \
            self.hass.states.get('device_tracker.dev1').state
Esempio n. 30
0
    def test_service_say_russian_config(self, aioclient_mock):
        """Test service call say."""
        calls = mock_service(self.hass, DOMAIN_MP, SERVICE_PLAY_MEDIA)

        url_param = {
            'text': 'HomeAssistant',
            'lang': 'ru-RU',
            'key': '1234567xx',
            'speaker': 'zahar',
            'format': 'mp3',
            'emotion': 'neutral',
            'speed': 1
        }
        aioclient_mock.get(
            self._base_url, status=200, content=b'test', params=url_param)

        config = {
            tts.DOMAIN: {
                'platform': 'yandextts',
                'api_key': '1234567xx',
                'language': 'ru-RU',
            }
        }

        with assert_setup_component(1, tts.DOMAIN):
            setup_component(self.hass, tts.DOMAIN, config)

        self.hass.services.call(tts.DOMAIN, 'yandextts_say', {
            tts.ATTR_MESSAGE: "HomeAssistant",
        })
        self.hass.block_till_done()

        assert len(aioclient_mock.mock_calls) == 1
        assert len(calls) == 1
Esempio n. 31
0
async def test_setup(hass):
    """Test the general setup of the platform."""
    # Set up some mock feed entries for this test.
    mock_entry_1 = _generate_mock_feed_entry("1234", "Title 1", 15.5,
                                             (-31.0, 150.0))
    mock_entry_2 = _generate_mock_feed_entry("2345", "Title 2", 20.5,
                                             (-31.1, 150.1))
    mock_entry_3 = _generate_mock_feed_entry("3456", "Title 3", 25.5,
                                             (-31.2, 150.2))
    mock_entry_4 = _generate_mock_feed_entry("4567", "Title 4", 12.5,
                                             (-31.3, 150.3))

    # Patching 'utcnow' to gain more control over the timed update.
    utcnow = dt_util.utcnow()
    with patch("homeassistant.util.dt.utcnow", return_value=utcnow), patch(
            "geojson_client.generic_feed.GenericFeed") as mock_feed:
        mock_feed.return_value.update.return_value = (
            "OK",
            [mock_entry_1, mock_entry_2, mock_entry_3],
        )
        with assert_setup_component(1, geo_location.DOMAIN):
            assert await async_setup_component(hass, geo_location.DOMAIN,
                                               CONFIG)
            # Artificially trigger update.
            hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
            # Collect events.
            await hass.async_block_till_done()

            all_states = hass.states.async_all()
            assert len(all_states) == 3

            state = hass.states.get("geo_location.title_1")
            assert state is not None
            assert state.name == "Title 1"
            assert state.attributes == {
                ATTR_EXTERNAL_ID: "1234",
                ATTR_LATITUDE: -31.0,
                ATTR_LONGITUDE: 150.0,
                ATTR_FRIENDLY_NAME: "Title 1",
                ATTR_UNIT_OF_MEASUREMENT: "km",
                ATTR_SOURCE: "geo_json_events",
            }
            assert round(abs(float(state.state) - 15.5), 7) == 0

            state = hass.states.get("geo_location.title_2")
            assert state is not None
            assert state.name == "Title 2"
            assert state.attributes == {
                ATTR_EXTERNAL_ID: "2345",
                ATTR_LATITUDE: -31.1,
                ATTR_LONGITUDE: 150.1,
                ATTR_FRIENDLY_NAME: "Title 2",
                ATTR_UNIT_OF_MEASUREMENT: "km",
                ATTR_SOURCE: "geo_json_events",
            }
            assert round(abs(float(state.state) - 20.5), 7) == 0

            state = hass.states.get("geo_location.title_3")
            assert state is not None
            assert state.name == "Title 3"
            assert state.attributes == {
                ATTR_EXTERNAL_ID: "3456",
                ATTR_LATITUDE: -31.2,
                ATTR_LONGITUDE: 150.2,
                ATTR_FRIENDLY_NAME: "Title 3",
                ATTR_UNIT_OF_MEASUREMENT: "km",
                ATTR_SOURCE: "geo_json_events",
            }
            assert round(abs(float(state.state) - 25.5), 7) == 0

            # Simulate an update - one existing, one new entry,
            # one outdated entry
            mock_feed.return_value.update.return_value = (
                "OK",
                [mock_entry_1, mock_entry_4, mock_entry_3],
            )
            async_fire_time_changed(hass, utcnow + SCAN_INTERVAL)
            await hass.async_block_till_done()

            all_states = hass.states.async_all()
            assert len(all_states) == 3

            # Simulate an update - empty data, but successful update,
            # so no changes to entities.
            mock_feed.return_value.update.return_value = "OK_NO_DATA", None
            async_fire_time_changed(hass, utcnow + 2 * SCAN_INTERVAL)
            await hass.async_block_till_done()

            all_states = hass.states.async_all()
            assert len(all_states) == 3

            # Simulate an update - empty data, removes all entities
            mock_feed.return_value.update.return_value = "ERROR", None
            async_fire_time_changed(hass, utcnow + 3 * SCAN_INTERVAL)
            await hass.async_block_till_done()

            all_states = hass.states.async_all()
            assert len(all_states) == 0
Esempio n. 32
0
async def assert_setup_sensor(hass, config, count=1):
    """Set up the sensor and assert it's been created."""
    with assert_setup_component(count):
        assert await async_setup_component(hass, sensor.DOMAIN, config)
Esempio n. 33
0
async def test_invalid_values(hass, mqtt_mock):
    """Test that invalid values are ignored."""
    with assert_setup_component(1, light.DOMAIN):
        assert await async_setup_component(
            hass,
            light.DOMAIN,
            {
                light.DOMAIN: {
                    "platform": "mqtt",
                    "schema": "template",
                    "name": "test",
                    "effect_list": ["rainbow", "colorloop"],
                    "state_topic": "test_light_rgb",
                    "command_topic": "test_light_rgb/set",
                    "command_on_template": "on,"
                    "{{ brightness|d }},"
                    "{{ color_temp|d }},"
                    "{{ red|d }}-"
                    "{{ green|d }}-"
                    "{{ blue|d }},"
                    "{{ effect|d }}",
                    "command_off_template": "off",
                    "state_template": '{{ value.split(",")[0] }}',
                    "brightness_template": '{{ value.split(",")[1] }}',
                    "color_temp_template": '{{ value.split(",")[2] }}',
                    "white_value_template": '{{ value.split(",")[3] }}',
                    "red_template": '{{ value.split(",")[4].' 'split("-")[0] }}',
                    "green_template": '{{ value.split(",")[4].' 'split("-")[1] }}',
                    "blue_template": '{{ value.split(",")[4].' 'split("-")[2] }}',
                    "effect_template": '{{ value.split(",")[5] }}',
                }
            },
        )
        await hass.async_block_till_done()

    state = hass.states.get("light.test")
    assert state.state == STATE_OFF
    assert state.attributes.get("rgb_color") is None
    assert state.attributes.get("brightness") is None
    assert state.attributes.get("color_temp") is None
    assert state.attributes.get("effect") is None
    assert state.attributes.get("white_value") is None
    assert not state.attributes.get(ATTR_ASSUMED_STATE)

    # turn on the light, full white
    async_fire_mqtt_message(
        hass, "test_light_rgb", "on,255,215,222,255-255-255,rainbow"
    )

    state = hass.states.get("light.test")
    assert state.state == STATE_ON
    assert state.attributes.get("brightness") == 255
    assert state.attributes.get("color_temp") == 215
    assert state.attributes.get("rgb_color") == (255, 255, 255)
    assert state.attributes.get("white_value") == 222
    assert state.attributes.get("effect") == "rainbow"

    # bad state value
    async_fire_mqtt_message(hass, "test_light_rgb", "offf")

    # state should not have changed
    state = hass.states.get("light.test")
    assert state.state == STATE_ON

    # bad brightness values
    async_fire_mqtt_message(hass, "test_light_rgb", "on,off,255-255-255")

    # brightness should not have changed
    state = hass.states.get("light.test")
    assert state.attributes.get("brightness") == 255

    # bad color temp values
    async_fire_mqtt_message(hass, "test_light_rgb", "on,,off,255-255-255")

    # color temp should not have changed
    state = hass.states.get("light.test")
    assert state.attributes.get("color_temp") == 215

    # bad color values
    async_fire_mqtt_message(hass, "test_light_rgb", "on,255,a-b-c")

    # color should not have changed
    state = hass.states.get("light.test")
    assert state.attributes.get("rgb_color") == (255, 255, 255)

    # bad white value values
    async_fire_mqtt_message(hass, "test_light_rgb", "on,,,off,255-255-255")

    # white value should not have changed
    state = hass.states.get("light.test")
    assert state.attributes.get("white_value") == 222

    # bad effect value
    async_fire_mqtt_message(hass, "test_light_rgb", "on,255,a-b-c,white")

    # effect should not have changed
    state = hass.states.get("light.test")
    assert state.attributes.get("effect") == "rainbow"
Esempio n. 34
0
async def test_sending_mqtt_commands_and_optimistic(opp, mqtt_mock):
    """Test the sending of command in optimistic mode."""
    config = {
        light.DOMAIN: {
            "platform": "mqtt",
            "name": "test",
            "command_topic": "test_light_rgb/set",
            "brightness_command_topic": "test_light_rgb/brightness/set",
            "rgb_command_topic": "test_light_rgb/rgb/set",
            "color_temp_command_topic": "test_light_rgb/color_temp/set",
            "effect_command_topic": "test_light_rgb/effect/set",
            "hs_command_topic": "test_light_rgb/hs/set",
            "white_value_command_topic": "test_light_rgb/white_value/set",
            "xy_command_topic": "test_light_rgb/xy/set",
            "effect_list": ["colorloop", "random"],
            "qos": 2,
            "payload_on": "on",
            "payload_off": "off",
        }
    }
    fake_state = op.State(
        "light.test",
        "on",
        {
            "brightness": 95,
            "hs_color": [100, 100],
            "effect": "random",
            "color_temp": 100,
            "white_value": 50,
        },
    )
    with patch(
            "openpeerpower.helpers.restore_state.RestoreEntity.async_get_last_state",
            return_value=mock_coro(fake_state),
    ):
        with assert_setup_component(1, light.DOMAIN):
            assert await async_setup_component(opp, light.DOMAIN, config)

    state = opp.states.get("light.test")
    assert state.state == STATE_ON
    assert state.attributes.get("brightness") == 95
    assert state.attributes.get("hs_color") == (100, 100)
    assert state.attributes.get("effect") == "random"
    assert state.attributes.get("color_temp") == 100
    assert state.attributes.get("white_value") == 50
    assert state.attributes.get(ATTR_ASSUMED_STATE)

    await common.async_turn_on(opp, "light.test")

    mqtt_mock.async_publish.assert_called_once_with("test_light_rgb/set", "on",
                                                    2, False)
    mqtt_mock.async_publish.reset_mock()
    state = opp.states.get("light.test")
    assert state.state == STATE_ON

    await common.async_turn_off(opp, "light.test")

    mqtt_mock.async_publish.assert_called_once_with("test_light_rgb/set",
                                                    "off", 2, False)
    mqtt_mock.async_publish.reset_mock()
    state = opp.states.get("light.test")
    assert state.state == STATE_OFF

    mqtt_mock.reset_mock()
    await common.async_turn_on(opp,
                               "light.test",
                               brightness=50,
                               xy_color=[0.123, 0.123])
    await common.async_turn_on(opp,
                               "light.test",
                               brightness=50,
                               hs_color=[359, 78])
    await common.async_turn_on(opp,
                               "light.test",
                               rgb_color=[255, 128, 0],
                               white_value=80)

    mqtt_mock.async_publish.assert_has_calls(
        [
            mock.call("test_light_rgb/set", "on", 2, False),
            mock.call("test_light_rgb/rgb/set", "255,128,0", 2, False),
            mock.call("test_light_rgb/brightness/set", 50, 2, False),
            mock.call("test_light_rgb/hs/set", "359.0,78.0", 2, False),
            mock.call("test_light_rgb/white_value/set", 80, 2, False),
            mock.call("test_light_rgb/xy/set", "0.14,0.131", 2, False),
        ],
        any_order=True,
    )

    state = opp.states.get("light.test")
    assert state.state == STATE_ON
    assert state.attributes["rgb_color"] == (255, 128, 0)
    assert state.attributes["brightness"] == 50
    assert state.attributes["hs_color"] == (30.118, 100)
    assert state.attributes["white_value"] == 80
    assert state.attributes["xy_color"] == (0.611, 0.375)
    def test_optimistic(self): \
            # pylint: disable=invalid-name
        """Test optimistic mode."""
        with assert_setup_component(1, light.DOMAIN):
            assert setup_component(self.hass, light.DOMAIN, {
                light.DOMAIN: {
                    'platform': 'mqtt_template',
                    'name': 'test',
                    'command_topic': 'test_light_rgb/set',
                    'command_on_template': 'on,'
                                           '{{ brightness|d }},'
                                           '{{ color_temp|d }},'
                                           '{{ white_value|d }},'
                                           '{{ red|d }}-'
                                           '{{ green|d }}-'
                                           '{{ blue|d }}',
                    'command_off_template': 'off',
                    'qos': 2
                }
            })

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)
        self.assertTrue(state.attributes.get(ATTR_ASSUMED_STATE))

        # turn on the light
        light.turn_on(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'on,,,,--', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        # turn the light off
        light.turn_off(self.hass, 'light.test')
        self.hass.block_till_done()

        self.assertEqual(('test_light_rgb/set', 'off', 2, False),
                         self.mock_publish.mock_calls[-2][1])
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)

        # turn on the light with brightness, color
        light.turn_on(self.hass, 'light.test', brightness=50,
                      rgb_color=[75, 75, 75])
        self.hass.block_till_done()

        self.assertEqual('test_light_rgb/set',
                         self.mock_publish.mock_calls[-2][1][0])

        # check the payload
        payload = self.mock_publish.mock_calls[-2][1][1]
        self.assertEqual('on,50,,,75-75-75', payload)

        # turn on the light with color temp and white val
        light.turn_on(self.hass, 'light.test', color_temp=200, white_value=139)
        self.hass.block_till_done()

        payload = self.mock_publish.mock_calls[-2][1][1]
        self.assertEqual('on,,200,139,--', payload)

        self.assertEqual(2, self.mock_publish.mock_calls[-2][1][2])
        self.assertEqual(False, self.mock_publish.mock_calls[-2][1][3])

        # check the state
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual((75, 75, 75), state.attributes['rgb_color'])
        self.assertEqual(50, state.attributes['brightness'])
        self.assertEqual(200, state.attributes['color_temp'])
        self.assertEqual(139, state.attributes['white_value'])
Esempio n. 36
0
async def test_attributes(hass):
    """Test handling of state attributes."""
    config = {
        DOMAIN: {
            'platform': 'group',
            CONF_ENTITIES:
            [DEMO_COVER, DEMO_COVER_POS, DEMO_COVER_TILT, DEMO_TILT]
        }
    }

    with assert_setup_component(1, DOMAIN):
        await async_setup_component(hass, DOMAIN, config)

    state = hass.states.get(COVER_GROUP)
    assert state.state == STATE_CLOSED
    assert state.attributes[ATTR_FRIENDLY_NAME] == DEFAULT_NAME
    assert ATTR_ASSUMED_STATE not in state.attributes
    assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0
    assert ATTR_CURRENT_POSITION not in state.attributes
    assert ATTR_CURRENT_TILT_POSITION not in state.attributes

    # Add Entity that supports open / close / stop
    hass.states.async_set(DEMO_COVER, STATE_OPEN,
                          {ATTR_SUPPORTED_FEATURES: 11})
    await hass.async_block_till_done()

    state = hass.states.get(COVER_GROUP)
    assert state.state == STATE_OPEN
    assert ATTR_ASSUMED_STATE not in state.attributes
    assert state.attributes[ATTR_SUPPORTED_FEATURES] == 11
    assert ATTR_CURRENT_POSITION not in state.attributes
    assert ATTR_CURRENT_TILT_POSITION not in state.attributes

    # Add Entity that supports set_cover_position
    hass.states.async_set(DEMO_COVER_POS, STATE_OPEN, {
        ATTR_SUPPORTED_FEATURES: 4,
        ATTR_CURRENT_POSITION: 70
    })
    await hass.async_block_till_done()

    state = hass.states.get(COVER_GROUP)
    assert state.state == STATE_OPEN
    assert ATTR_ASSUMED_STATE not in state.attributes
    assert state.attributes[ATTR_SUPPORTED_FEATURES] == 15
    assert state.attributes[ATTR_CURRENT_POSITION] == 70
    assert ATTR_CURRENT_TILT_POSITION not in state.attributes

    # Add Entity that supports open tilt / close tilt / stop tilt
    hass.states.async_set(DEMO_TILT, STATE_OPEN,
                          {ATTR_SUPPORTED_FEATURES: 112})
    await hass.async_block_till_done()

    state = hass.states.get(COVER_GROUP)
    assert state.state == STATE_OPEN
    assert ATTR_ASSUMED_STATE not in state.attributes
    assert state.attributes[ATTR_SUPPORTED_FEATURES] == 127
    assert state.attributes[ATTR_CURRENT_POSITION] == 70
    assert ATTR_CURRENT_TILT_POSITION not in state.attributes

    # Add Entity that supports set_tilt_position
    hass.states.async_set(DEMO_COVER_TILT, STATE_OPEN, {
        ATTR_SUPPORTED_FEATURES: 128,
        ATTR_CURRENT_TILT_POSITION: 60
    })
    await hass.async_block_till_done()

    state = hass.states.get(COVER_GROUP)
    assert state.state == STATE_OPEN
    assert ATTR_ASSUMED_STATE not in state.attributes
    assert state.attributes[ATTR_SUPPORTED_FEATURES] == 255
    assert state.attributes[ATTR_CURRENT_POSITION] == 70
    assert state.attributes[ATTR_CURRENT_TILT_POSITION] == 60

    # ### Test assumed state ###
    # ##########################

    # For covers
    hass.states.async_set(DEMO_COVER, STATE_OPEN, {
        ATTR_SUPPORTED_FEATURES: 4,
        ATTR_CURRENT_POSITION: 100
    })
    await hass.async_block_till_done()

    state = hass.states.get(COVER_GROUP)
    assert state.state == STATE_OPEN
    assert state.attributes[ATTR_ASSUMED_STATE] is True
    assert state.attributes[ATTR_SUPPORTED_FEATURES] == 244
    assert state.attributes[ATTR_CURRENT_POSITION] == 100
    assert state.attributes[ATTR_CURRENT_TILT_POSITION] == 60

    hass.states.async_remove(DEMO_COVER)
    hass.states.async_remove(DEMO_COVER_POS)
    await hass.async_block_till_done()

    state = hass.states.get(COVER_GROUP)
    assert state.state == STATE_OPEN
    assert ATTR_ASSUMED_STATE not in state.attributes
    assert state.attributes[ATTR_SUPPORTED_FEATURES] == 240
    assert ATTR_CURRENT_POSITION not in state.attributes
    assert state.attributes[ATTR_CURRENT_TILT_POSITION] == 60

    # For tilts
    hass.states.async_set(DEMO_TILT, STATE_OPEN, {
        ATTR_SUPPORTED_FEATURES: 128,
        ATTR_CURRENT_TILT_POSITION: 100
    })
    await hass.async_block_till_done()

    state = hass.states.get(COVER_GROUP)
    assert state.state == STATE_OPEN
    assert state.attributes[ATTR_ASSUMED_STATE] is True
    assert state.attributes[ATTR_SUPPORTED_FEATURES] == 128
    assert ATTR_CURRENT_POSITION not in state.attributes
    assert state.attributes[ATTR_CURRENT_TILT_POSITION] == 100

    hass.states.async_remove(DEMO_COVER_TILT)
    hass.states.async_set(DEMO_TILT, STATE_CLOSED)
    await hass.async_block_till_done()

    state = hass.states.get(COVER_GROUP)
    assert state.state == STATE_CLOSED
    assert ATTR_ASSUMED_STATE not in state.attributes
    assert state.attributes[ATTR_SUPPORTED_FEATURES] == 0
    assert ATTR_CURRENT_POSITION not in state.attributes
    assert ATTR_CURRENT_TILT_POSITION not in state.attributes

    hass.states.async_set(DEMO_TILT, STATE_CLOSED, {ATTR_ASSUMED_STATE: True})
    await hass.async_block_till_done()

    state = hass.states.get(COVER_GROUP)
    assert state.attributes[ATTR_ASSUMED_STATE] is True
Esempio n. 37
0
async def setup_comp(hass):
    """Set up group cover component."""
    with assert_setup_component(2, DOMAIN):
        await async_setup_component(hass, DOMAIN, CONFIG)
Esempio n. 38
0
async def test_set_tilt_position_optimistic(hass, calls):
    """Test the optimistic tilt_position mode."""
    with assert_setup_component(1, "cover"):
        assert await setup.async_setup_component(
            hass,
            "cover",
            {
                "cover": {
                    "platform": "template",
                    "covers": {
                        "test_template_cover": {
                            "position_template": "{{ 100 }}",
                            "set_cover_position": {
                                "service": "test.automation"
                            },
                            "set_cover_tilt_position": {
                                "service": "test.automation"
                            },
                        }
                    },
                }
            },
        )
    await hass.async_block_till_done()
    await hass.async_start()
    await hass.async_block_till_done()

    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_tilt_position") is None

    await hass.services.async_call(
        DOMAIN,
        SERVICE_SET_COVER_TILT_POSITION,
        {
            ATTR_ENTITY_ID: ENTITY_COVER,
            ATTR_TILT_POSITION: 42
        },
        blocking=True,
    )
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_tilt_position") == 42.0

    await hass.services.async_call(DOMAIN,
                                   SERVICE_CLOSE_COVER_TILT,
                                   {ATTR_ENTITY_ID: ENTITY_COVER},
                                   blocking=True)
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_tilt_position") == 0.0

    await hass.services.async_call(DOMAIN,
                                   SERVICE_OPEN_COVER_TILT,
                                   {ATTR_ENTITY_ID: ENTITY_COVER},
                                   blocking=True)
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_tilt_position") == 100.0

    await hass.services.async_call(DOMAIN,
                                   SERVICE_TOGGLE_COVER_TILT,
                                   {ATTR_ENTITY_ID: ENTITY_COVER},
                                   blocking=True)
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_tilt_position") == 0.0

    await hass.services.async_call(DOMAIN,
                                   SERVICE_TOGGLE_COVER_TILT,
                                   {ATTR_ENTITY_ID: ENTITY_COVER},
                                   blocking=True)
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_tilt_position") == 100.0
Esempio n. 39
0
async def test_set_position(hass, calls):
    """Test the set_position command."""
    with assert_setup_component(1, "cover"):
        assert await setup.async_setup_component(
            hass,
            "input_number",
            {
                "input_number": {
                    "test": {
                        "min": "0",
                        "max": "100",
                        "initial": "42"
                    }
                }
            },
        )
        assert await setup.async_setup_component(
            hass,
            "cover",
            {
                "cover": {
                    "platform": "template",
                    "covers": {
                        "test_template_cover": {
                            "position_template":
                            "{{ states.input_number.test.state | int }}",
                            "set_cover_position": {
                                "service": "input_number.set_value",
                                "entity_id": "input_number.test",
                                "data_template": {
                                    "value": "{{ position }}"
                                },
                            },
                        }
                    },
                }
            },
        )

    await hass.async_block_till_done()
    await hass.async_start()
    await hass.async_block_till_done()

    state = hass.states.async_set("input_number.test", 42)
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.state == STATE_OPEN

    await hass.services.async_call(DOMAIN,
                                   SERVICE_OPEN_COVER,
                                   {ATTR_ENTITY_ID: ENTITY_COVER},
                                   blocking=True)
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_position") == 100.0

    await hass.services.async_call(DOMAIN,
                                   SERVICE_CLOSE_COVER,
                                   {ATTR_ENTITY_ID: ENTITY_COVER},
                                   blocking=True)
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_position") == 0.0

    await hass.services.async_call(DOMAIN,
                                   SERVICE_TOGGLE,
                                   {ATTR_ENTITY_ID: ENTITY_COVER},
                                   blocking=True)
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_position") == 100.0

    await hass.services.async_call(DOMAIN,
                                   SERVICE_TOGGLE,
                                   {ATTR_ENTITY_ID: ENTITY_COVER},
                                   blocking=True)
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_position") == 0.0

    await hass.services.async_call(
        DOMAIN,
        SERVICE_SET_COVER_POSITION,
        {
            ATTR_ENTITY_ID: ENTITY_COVER,
            ATTR_POSITION: 25
        },
        blocking=True,
    )
    await hass.async_block_till_done()
    state = hass.states.get("cover.test_template_cover")
    assert state.attributes.get("current_position") == 25.0
    def test_invalid_values(self): \
            # pylint: disable=invalid-name
        """Test that invalid values are ignored."""
        with assert_setup_component(1, light.DOMAIN):
            assert setup_component(self.hass, light.DOMAIN, {
                light.DOMAIN: {
                    'platform': 'mqtt_template',
                    'name': 'test',
                    'effect_list': ['rainbow', 'colorloop'],
                    'state_topic': 'test_light_rgb',
                    'command_topic': 'test_light_rgb/set',
                    'command_on_template': 'on,'
                                           '{{ brightness|d }},'
                                           '{{ color_temp|d }},'
                                           '{{ red|d }}-'
                                           '{{ green|d }}-'
                                           '{{ blue|d }},'
                                           '{{ effect|d }}',
                    'command_off_template': 'off',
                    'state_template': '{{ value.split(",")[0] }}',
                    'brightness_template': '{{ value.split(",")[1] }}',
                    'color_temp_template': '{{ value.split(",")[2] }}',
                    'white_value_template': '{{ value.split(",")[3] }}',
                    'red_template': '{{ value.split(",")[4].'
                                    'split("-")[0] }}',
                    'green_template': '{{ value.split(",")[4].'
                                      'split("-")[1] }}',
                    'blue_template': '{{ value.split(",")[4].'
                                     'split("-")[2] }}',
                    'effect_template': '{{ value.split(",")[5] }}',
                }
            })

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)
        self.assertIsNone(state.attributes.get('rgb_color'))
        self.assertIsNone(state.attributes.get('brightness'))
        self.assertIsNone(state.attributes.get('color_temp'))
        self.assertIsNone(state.attributes.get('effect'))
        self.assertIsNone(state.attributes.get('white_value'))
        self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE))

        # turn on the light, full white
        fire_mqtt_message(self.hass, 'test_light_rgb',
                          'on,255,215,222,255-255-255,rainbow')
        self.hass.block_till_done()

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual(255, state.attributes.get('brightness'))
        self.assertEqual(215, state.attributes.get('color_temp'))
        self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))
        self.assertEqual(222, state.attributes.get('white_value'))
        self.assertEqual('rainbow', state.attributes.get('effect'))

        # bad state value
        fire_mqtt_message(self.hass, 'test_light_rgb', 'offf')
        self.hass.block_till_done()

        # state should not have changed
        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)

        # bad brightness values
        fire_mqtt_message(self.hass, 'test_light_rgb', 'on,off,255-255-255')
        self.hass.block_till_done()

        # brightness should not have changed
        state = self.hass.states.get('light.test')
        self.assertEqual(255, state.attributes.get('brightness'))

        # bad color temp values
        fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,off,255-255-255')
        self.hass.block_till_done()

        # color temp should not have changed
        state = self.hass.states.get('light.test')
        self.assertEqual(215, state.attributes.get('color_temp'))

        # bad color values
        fire_mqtt_message(self.hass, 'test_light_rgb', 'on,255,a-b-c')
        self.hass.block_till_done()

        # color should not have changed
        state = self.hass.states.get('light.test')
        self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))

        # bad white value values
        fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,,off,255-255-255')
        self.hass.block_till_done()

        # white value should not have changed
        state = self.hass.states.get('light.test')
        self.assertEqual(222, state.attributes.get('white_value'))

        # bad effect value
        fire_mqtt_message(self.hass, 'test_light_rgb', 'on,255,a-b-c,white')
        self.hass.block_till_done()

        # effect should not have changed
        state = self.hass.states.get('light.test')
        self.assertEqual('rainbow', state.attributes.get('effect'))
Esempio n. 41
0
    def test_setup_component_without_api_key(self):
        """Test setup component without api key."""
        config = {tts.DOMAIN: {"platform": "voicerss"}}

        with assert_setup_component(0, tts.DOMAIN):
            setup_component(self.hass, tts.DOMAIN, config)
    def test_state_brightness_color_effect_temp_white_change_via_topic(self): \
            # pylint: disable=invalid-name
        """Test state, bri, color, effect, color temp, white val change."""
        with assert_setup_component(1, light.DOMAIN):
            assert setup_component(self.hass, light.DOMAIN, {
                light.DOMAIN: {
                    'platform': 'mqtt_template',
                    'name': 'test',
                    'effect_list': ['rainbow', 'colorloop'],
                    'state_topic': 'test_light_rgb',
                    'command_topic': 'test_light_rgb/set',
                    'command_on_template': 'on,'
                                           '{{ brightness|d }},'
                                           '{{ color_temp|d }},'
                                           '{{ white_value|d }},'
                                           '{{ red|d }}-'
                                           '{{ green|d }}-'
                                           '{{ blue|d }},'
                                           '{{ effect|d }}',
                    'command_off_template': 'off',
                    'state_template': '{{ value.split(",")[0] }}',
                    'brightness_template': '{{ value.split(",")[1] }}',
                    'color_temp_template': '{{ value.split(",")[2] }}',
                    'white_value_template': '{{ value.split(",")[3] }}',
                    'red_template': '{{ value.split(",")[4].'
                                    'split("-")[0] }}',
                    'green_template': '{{ value.split(",")[4].'
                                      'split("-")[1] }}',
                    'blue_template': '{{ value.split(",")[4].'
                                     'split("-")[2] }}',
                    'effect_template': '{{ value.split(",")[5] }}'
                }
            })

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_OFF, state.state)
        self.assertIsNone(state.attributes.get('rgb_color'))
        self.assertIsNone(state.attributes.get('brightness'))
        self.assertIsNone(state.attributes.get('effect'))
        self.assertIsNone(state.attributes.get('color_temp'))
        self.assertIsNone(state.attributes.get('white_value'))
        self.assertFalse(state.attributes.get(ATTR_ASSUMED_STATE))

        # turn on the light, full white
        fire_mqtt_message(self.hass, 'test_light_rgb',
                          'on,255,145,123,255-255-255,')
        self.hass.block_till_done()

        state = self.hass.states.get('light.test')
        self.assertEqual(STATE_ON, state.state)
        self.assertEqual([255, 255, 255], state.attributes.get('rgb_color'))
        self.assertEqual(255, state.attributes.get('brightness'))
        self.assertEqual(145, state.attributes.get('color_temp'))
        self.assertEqual(123, state.attributes.get('white_value'))
        self.assertIsNone(state.attributes.get('effect'))

        # turn the light off
        fire_mqtt_message(self.hass, 'test_light_rgb', 'off')
        self.hass.block_till_done()

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

        # lower the brightness
        fire_mqtt_message(self.hass, 'test_light_rgb', 'on,100')
        self.hass.block_till_done()

        light_state = self.hass.states.get('light.test')
        self.hass.block_till_done()
        self.assertEqual(100, light_state.attributes['brightness'])

        # change the color temp
        fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,195')
        self.hass.block_till_done()

        light_state = self.hass.states.get('light.test')
        self.hass.block_till_done()
        self.assertEqual(195, light_state.attributes['color_temp'])

        # change the color
        fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,,,41-42-43')
        self.hass.block_till_done()

        light_state = self.hass.states.get('light.test')
        self.assertEqual([41, 42, 43], light_state.attributes.get('rgb_color'))

        # change the white value
        fire_mqtt_message(self.hass, 'test_light_rgb', 'on,,,134')
        self.hass.block_till_done()

        light_state = self.hass.states.get('light.test')
        self.hass.block_till_done()
        self.assertEqual(134, light_state.attributes['white_value'])

        # change the effect
        fire_mqtt_message(self.hass, 'test_light_rgb',
                          'on,,,,41-42-43,rainbow')
        self.hass.block_till_done()

        light_state = self.hass.states.get('light.test')
        self.assertEqual('rainbow', light_state.attributes.get('effect'))
Esempio n. 43
0
    def test_setup_component(self):
        """Test setup component."""
        config = {tts.DOMAIN: {"platform": "marytts"}}

        with assert_setup_component(1, tts.DOMAIN):
            setup_component(self.hass, tts.DOMAIN, config)
Esempio n. 44
0
async def test_setup_race_condition(hass):
    """Test a particular race condition experienced."""
    # 1. Feed returns 1 entry -> Feed manager creates 1 entity.
    # 2. Feed returns error -> Feed manager removes 1 entity.
    #    However, this stayed on and kept listening for dispatcher signals.
    # 3. Feed returns 1 entry -> Feed manager creates 1 entity.
    # 4. Feed returns 1 entry -> Feed manager updates 1 entity.
    #    Internally, the previous entity is updating itself, too.
    # 5. Feed returns error -> Feed manager removes 1 entity.
    #    There are now 2 entities trying to remove themselves from HA, but
    #    the second attempt fails of course.

    # Set up some mock feed entries for this test.
    mock_entry_1 = _generate_mock_feed_entry("1234", "Title 1", 15.5,
                                             (-31.0, 150.0))
    delete_signal = f"geo_json_events_delete_1234"
    update_signal = f"geo_json_events_update_1234"

    # Patching 'utcnow' to gain more control over the timed update.
    utcnow = dt_util.utcnow()
    with patch("homeassistant.util.dt.utcnow", return_value=utcnow), patch(
            "geojson_client.generic_feed.GenericFeed") as mock_feed:
        with assert_setup_component(1, geo_location.DOMAIN):
            assert await async_setup_component(hass, geo_location.DOMAIN,
                                               CONFIG)

            mock_feed.return_value.update.return_value = "OK", [mock_entry_1]

            # Artificially trigger update.
            hass.bus.async_fire(EVENT_HOMEASSISTANT_START)
            # Collect events.
            await hass.async_block_till_done()

            all_states = hass.states.async_all()
            assert len(all_states) == 1
            assert len(hass.data[DATA_DISPATCHER][delete_signal]) == 1
            assert len(hass.data[DATA_DISPATCHER][update_signal]) == 1

            # Simulate an update - empty data, removes all entities
            mock_feed.return_value.update.return_value = "ERROR", None
            async_fire_time_changed(hass, utcnow + SCAN_INTERVAL)
            await hass.async_block_till_done()

            all_states = hass.states.async_all()
            assert len(all_states) == 0
            assert len(hass.data[DATA_DISPATCHER][delete_signal]) == 0
            assert len(hass.data[DATA_DISPATCHER][update_signal]) == 0

            # Simulate an update - 1 entry
            mock_feed.return_value.update.return_value = "OK", [mock_entry_1]
            async_fire_time_changed(hass, utcnow + 2 * SCAN_INTERVAL)
            await hass.async_block_till_done()

            all_states = hass.states.async_all()
            assert len(all_states) == 1
            assert len(hass.data[DATA_DISPATCHER][delete_signal]) == 1
            assert len(hass.data[DATA_DISPATCHER][update_signal]) == 1

            # Simulate an update - 1 entry
            mock_feed.return_value.update.return_value = "OK", [mock_entry_1]
            async_fire_time_changed(hass, utcnow + 3 * SCAN_INTERVAL)
            await hass.async_block_till_done()

            all_states = hass.states.async_all()
            assert len(all_states) == 1
            assert len(hass.data[DATA_DISPATCHER][delete_signal]) == 1
            assert len(hass.data[DATA_DISPATCHER][update_signal]) == 1

            # Simulate an update - empty data, removes all entities
            mock_feed.return_value.update.return_value = "ERROR", None
            async_fire_time_changed(hass, utcnow + 4 * SCAN_INTERVAL)
            await hass.async_block_till_done()

            all_states = hass.states.async_all()
            assert len(all_states) == 0
            # Ensure that delete and update signal targets are now empty.
            assert len(hass.data[DATA_DISPATCHER][delete_signal]) == 0
            assert len(hass.data[DATA_DISPATCHER][update_signal]) == 0
Esempio n. 45
0
    def test_setup_component(self):
        """Set up demo platform on camera component."""
        config = {camera.DOMAIN: {"platform": "demo"}}

        with assert_setup_component(1, camera.DOMAIN):
            setup_component(self.hass, camera.DOMAIN, config)
Esempio n. 46
0
    def test_setup_component(self):
        """Test setup component."""
        config = {tts.DOMAIN: {"platform": "voicerss", "api_key": "1234567xx"}}

        with assert_setup_component(1, tts.DOMAIN):
            setup_component(self.hass, tts.DOMAIN, config)
Esempio n. 47
0
 def test_setup_component(self, mock_update):
     """Set up component."""
     with assert_setup_component(3, mf.DOMAIN):
         setup_component(self.hass, mf.DOMAIN, self.config)
Esempio n. 48
0
    def test_setup_component(self):
        """Setup ffmpeg component."""
        with assert_setup_component(2):
            setup_component(self.hass, ffmpeg.DOMAIN, {ffmpeg.DOMAIN: {}})

        assert self.hass.data[ffmpeg.DATA_FFMPEG].binary == 'ffmpeg'
 def test_pushbullet_config_bad(self):
     """Test set up the platform with bad/missing configuration."""
     config = {notify.DOMAIN: {"platform": "pushbullet"}}
     with assert_setup_component(0) as handle_config:
         assert setup_component(self.hass, notify.DOMAIN, config)
     assert not handle_config[notify.DOMAIN]
Esempio n. 50
0
    def test_validate_platform_config(self):
        """Test validating platform configuration."""
        platform_schema = PLATFORM_SCHEMA.extend({
            'hello': str,
        })
        loader.set_component(
            'platform_conf',
            MockModule('platform_conf', platform_schema=platform_schema))

        loader.set_component(
            'platform_conf.whatever', MockPlatform('whatever'))

        with assert_setup_component(0):
            assert bootstrap.setup_component(self.hass, 'platform_conf', {
                'platform_conf': {
                    'hello': 'world',
                    'invalid': 'extra',
                }
            })

        self.hass.config.components.remove('platform_conf')

        with assert_setup_component(1):
            assert bootstrap.setup_component(self.hass, 'platform_conf', {
                'platform_conf': {
                    'platform': 'whatever',
                    'hello': 'world',
                },
                'platform_conf 2': {
                    'invalid': True
                }
            })

        self.hass.config.components.remove('platform_conf')

        with assert_setup_component(0):
            assert bootstrap.setup_component(self.hass, 'platform_conf', {
                'platform_conf': {
                    'platform': 'not_existing',
                    'hello': 'world',
                }
            })

        self.hass.config.components.remove('platform_conf')

        with assert_setup_component(1):
            assert bootstrap.setup_component(self.hass, 'platform_conf', {
                'platform_conf': {
                    'platform': 'whatever',
                    'hello': 'world',
                }
            })

        self.hass.config.components.remove('platform_conf')

        with assert_setup_component(1):
            assert bootstrap.setup_component(self.hass, 'platform_conf', {
                'platform_conf': [{
                    'platform': 'whatever',
                    'hello': 'world',
                }]
            })

        self.hass.config.components.remove('platform_conf')

        # Any falsey platform config will be ignored (None, {}, etc)
        with assert_setup_component(0) as config:
            assert bootstrap.setup_component(self.hass, 'platform_conf', {
                'platform_conf': None
            })
            assert 'platform_conf' in self.hass.config.components
            assert not config['platform_conf']  # empty

            assert bootstrap.setup_component(self.hass, 'platform_conf', {
                'platform_conf': {}
            })
            assert 'platform_conf' in self.hass.config.components
            assert not config['platform_conf']  # empty
Esempio n. 51
0
async def test_see_passive_zone_state(hass, mock_device_tracker_conf):
    """Test that the device tracker sets gps for passive trackers."""
    now = dt_util.utcnow()

    register_time = datetime(now.year + 1, 9, 15, 23, tzinfo=dt_util.UTC)
    scan_time = datetime(now.year + 1, 9, 15, 23, 1, tzinfo=dt_util.UTC)

    with assert_setup_component(1, zone.DOMAIN):
        zone_info = {
            "name": "Home",
            "latitude": 1,
            "longitude": 2,
            "radius": 250,
            "passive": False,
        }

        await async_setup_component(hass, zone.DOMAIN, {"zone": zone_info})

    scanner = getattr(hass.components, "test.device_tracker").SCANNER
    scanner.reset()
    scanner.come_home("dev1")

    with patch(
            "homeassistant.components.device_tracker.legacy.dt_util.utcnow",
            return_value=register_time,
    ), assert_setup_component(1, device_tracker.DOMAIN):
        assert await async_setup_component(
            hass,
            device_tracker.DOMAIN,
            {
                device_tracker.DOMAIN: {
                    CONF_PLATFORM: "test",
                    device_tracker.CONF_CONSIDER_HOME: 59,
                }
            },
        )
        await hass.async_block_till_done()

    state = hass.states.get("device_tracker.dev1")
    attrs = state.attributes
    assert state.state == STATE_HOME
    assert state.object_id == "dev1"
    assert state.name == "dev1"
    assert attrs.get("friendly_name") == "dev1"
    assert attrs.get("latitude") == 1
    assert attrs.get("longitude") == 2
    assert attrs.get("gps_accuracy") == 0
    assert attrs.get("source_type") == device_tracker.SOURCE_TYPE_ROUTER

    scanner.leave_home("dev1")

    with patch(
            "homeassistant.components.device_tracker.legacy.dt_util.utcnow",
            return_value=scan_time,
    ):
        async_fire_time_changed(hass, scan_time)
        await hass.async_block_till_done()

    state = hass.states.get("device_tracker.dev1")
    attrs = state.attributes
    assert state.state == STATE_NOT_HOME
    assert state.object_id == "dev1"
    assert state.name == "dev1"
    assert attrs.get("friendly_name") == "dev1"
    assert attrs.get("latitude") is None
    assert attrs.get("longitude") is None
    assert attrs.get("gps_accuracy") is None
    assert attrs.get("source_type") == device_tracker.SOURCE_TYPE_ROUTER
Esempio n. 52
0
 def test_setup_component_wrong_api_key(self, mock_update):
     """Set up component without api key."""
     with assert_setup_component(0, mf.DOMAIN):
         setup_component(self.hass, mf.DOMAIN, {mf.DOMAIN: {}})
Esempio n. 53
0
def test_bad_platform(hass):
    """Test bad platform."""
    config = {'device_tracker': [{'platform': 'bad_platform'}]}
    with assert_setup_component(0, device_tracker.DOMAIN):
        assert (yield from device_tracker.async_setup(hass, config))
Esempio n. 54
0
async def test_bad_platform(hass):
    """Test bad platform."""
    config = {"device_tracker": [{"platform": "bad_platform"}]}
    with assert_setup_component(0, device_tracker.DOMAIN):
        assert await async_setup_component(hass, device_tracker.DOMAIN, config)
Esempio n. 55
0
 def test_setup_without_yaml_file(self):
     """Test with no YAML file."""
     with assert_setup_component(1, device_tracker.DOMAIN):
         assert setup_component(self.hass, device_tracker.DOMAIN,
                                TEST_PLATFORM)
Esempio n. 56
0
async def test_setup_without_yaml_file(hass):
    """Test with no YAML file."""
    with assert_setup_component(1, device_tracker.DOMAIN):
        assert await async_setup_component(hass, device_tracker.DOMAIN,
                                           TEST_PLATFORM)
    def test_setup(self, mock_feed):
        """Test the general setup of the platform."""
        # Set up some mock feed entries for this test.
        mock_entry_1 = self._generate_mock_feed_entry('1234', 'Title 1', 15.5,
                                                      (-31.0, 150.0),
                                                      'Category 1')
        mock_entry_2 = self._generate_mock_feed_entry('2345', 'Title 2', 20.5,
                                                      (-31.1, 150.1),
                                                      'Category 1')
        mock_feed.return_value.update.return_value = 'OK', [
            mock_entry_1, mock_entry_2
        ]

        utcnow = dt_util.utcnow()
        # Patching 'utcnow' to gain more control over the timed update.
        with patch('homeassistant.util.dt.utcnow', return_value=utcnow):
            with assert_setup_component(1, sensor.DOMAIN):
                assert setup_component(self.hass, sensor.DOMAIN, VALID_CONFIG)
                # Artificially trigger update.
                self.hass.bus.fire(EVENT_HOMEASSISTANT_START)
                # Collect events.
                self.hass.block_till_done()

                all_states = self.hass.states.all()
                assert len(all_states) == 1

                state = self.hass.states.get("sensor.event_service_any")
                assert state is not None
                assert state.name == "Event Service Any"
                assert int(state.state) == 2
                assert state.attributes == {
                    ATTR_FRIENDLY_NAME: "Event Service Any",
                    ATTR_UNIT_OF_MEASUREMENT: "Events",
                    ATTR_ICON: "mdi:alert",
                    "Title 1": "16km",
                    "Title 2": "20km"
                }

                # Simulate an update - empty data, but successful update,
                # so no changes to entities.
                mock_feed.return_value.update.return_value = 'OK_NO_DATA', None
                fire_time_changed(self.hass,
                                  utcnow + geo_rss_events.SCAN_INTERVAL)
                self.hass.block_till_done()

                all_states = self.hass.states.all()
                assert len(all_states) == 1
                state = self.hass.states.get("sensor.event_service_any")
                assert int(state.state) == 2

                # Simulate an update - empty data, removes all entities
                mock_feed.return_value.update.return_value = 'ERROR', None
                fire_time_changed(self.hass,
                                  utcnow + 2 * geo_rss_events.SCAN_INTERVAL)
                self.hass.block_till_done()

                all_states = self.hass.states.all()
                assert len(all_states) == 1
                state = self.hass.states.get("sensor.event_service_any")
                assert int(state.state) == 0
                assert state.attributes == {
                    ATTR_FRIENDLY_NAME: "Event Service Any",
                    ATTR_UNIT_OF_MEASUREMENT: "Events",
                    ATTR_ICON: "mdi:alert"
                }
Esempio n. 58
0
    def test_validate_platform_config(self, caplog):
        """Test validating platform configuration."""
        platform_schema = PLATFORM_SCHEMA.extend({"hello": str})
        platform_schema_base = PLATFORM_SCHEMA_BASE.extend({})
        mock_integration(
            self.hass,
            MockModule("platform_conf",
                       platform_schema_base=platform_schema_base),
        )
        mock_entity_platform(
            self.hass,
            "platform_conf.whatever",
            MockPlatform(platform_schema=platform_schema),
        )

        with assert_setup_component(0):
            assert setup.setup_component(
                self.hass,
                "platform_conf",
                {
                    "platform_conf": {
                        "platform": "not_existing",
                        "hello": "world"
                    }
                },
            )

        self.hass.data.pop(setup.DATA_SETUP)
        self.hass.config.components.remove("platform_conf")

        with assert_setup_component(1):
            assert setup.setup_component(
                self.hass,
                "platform_conf",
                {"platform_conf": {
                    "platform": "whatever",
                    "hello": "world"
                }},
            )

        self.hass.data.pop(setup.DATA_SETUP)
        self.hass.config.components.remove("platform_conf")

        with assert_setup_component(1):
            assert setup.setup_component(
                self.hass,
                "platform_conf",
                {
                    "platform_conf": [{
                        "platform": "whatever",
                        "hello": "world"
                    }]
                },
            )

        self.hass.data.pop(setup.DATA_SETUP)
        self.hass.config.components.remove("platform_conf")

        # Any falsey platform config will be ignored (None, {}, etc)
        with assert_setup_component(0) as config:
            assert setup.setup_component(self.hass, "platform_conf",
                                         {"platform_conf": None})
            assert "platform_conf" in self.hass.config.components
            assert not config["platform_conf"]  # empty

            assert setup.setup_component(self.hass, "platform_conf",
                                         {"platform_conf": {}})
            assert "platform_conf" in self.hass.config.components
            assert not config["platform_conf"]  # empty
Esempio n. 59
0
async def test_sending_mqtt_commands_and_optimistic(hass, mqtt_mock):
    """Test the sending of command in optimistic mode."""
    config = {light.DOMAIN: {
        'platform': 'mqtt',
        'name': 'test',
        'command_topic': 'test_light_rgb/set',
        'brightness_command_topic': 'test_light_rgb/brightness/set',
        'rgb_command_topic': 'test_light_rgb/rgb/set',
        'color_temp_command_topic': 'test_light_rgb/color_temp/set',
        'effect_command_topic': 'test_light_rgb/effect/set',
        'hs_command_topic': 'test_light_rgb/hs/set',
        'white_value_command_topic': 'test_light_rgb/white_value/set',
        'xy_command_topic': 'test_light_rgb/xy/set',
        'effect_list': ['colorloop', 'random'],
        'qos': 2,
        'payload_on': 'on',
        'payload_off': 'off'
    }}
    fake_state = ha.State('light.test', 'on', {'brightness': 95,
                                               'hs_color': [100, 100],
                                               'effect': 'random',
                                               'color_temp': 100,
                                               'white_value': 50})
    with patch('homeassistant.helpers.restore_state.RestoreEntity'
               '.async_get_last_state',
               return_value=mock_coro(fake_state)):
        with assert_setup_component(1, light.DOMAIN):
            assert await async_setup_component(hass, light.DOMAIN, config)

    state = hass.states.get('light.test')
    assert STATE_ON == state.state
    assert 95 == state.attributes.get('brightness')
    assert (100, 100) == state.attributes.get('hs_color')
    assert 'random' == state.attributes.get('effect')
    assert 100 == state.attributes.get('color_temp')
    assert 50 == state.attributes.get('white_value')
    assert state.attributes.get(ATTR_ASSUMED_STATE)

    common.async_turn_on(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with(
        'test_light_rgb/set', 'on', 2, False)
    mqtt_mock.async_publish.reset_mock()
    state = hass.states.get('light.test')
    assert STATE_ON == state.state

    common.async_turn_off(hass, 'light.test')
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_called_once_with(
        'test_light_rgb/set', 'off', 2, False)
    mqtt_mock.async_publish.reset_mock()
    state = hass.states.get('light.test')
    assert STATE_OFF == state.state

    mqtt_mock.reset_mock()
    common.async_turn_on(hass, 'light.test',
                         brightness=50, xy_color=[0.123, 0.123])
    common.async_turn_on(hass, 'light.test',
                         brightness=50, hs_color=[359, 78])
    common.async_turn_on(hass, 'light.test', rgb_color=[255, 128, 0],
                         white_value=80)
    await hass.async_block_till_done()

    mqtt_mock.async_publish.assert_has_calls([
        mock.call('test_light_rgb/set', 'on', 2, False),
        mock.call('test_light_rgb/rgb/set', '255,128,0', 2, False),
        mock.call('test_light_rgb/brightness/set', 50, 2, False),
        mock.call('test_light_rgb/hs/set', '359.0,78.0', 2, False),
        mock.call('test_light_rgb/white_value/set', 80, 2, False),
        mock.call('test_light_rgb/xy/set', '0.14,0.131', 2, False),
    ], any_order=True)

    state = hass.states.get('light.test')
    assert STATE_ON == state.state
    assert (255, 128, 0) == state.attributes['rgb_color']
    assert 50 == state.attributes['brightness']
    assert (30.118, 100) == state.attributes['hs_color']
    assert 80 == state.attributes['white_value']
    assert (0.611, 0.375) == state.attributes['xy_color']
Esempio n. 60
0
    def test_see_passive_zone_state(self):
        """Test that the device tracker sets gps for passive trackers."""
        register_time = datetime(2015, 9, 15, 23, tzinfo=dt_util.UTC)
        scan_time = datetime(2015, 9, 15, 23, 1, tzinfo=dt_util.UTC)

        with assert_setup_component(1, zone.DOMAIN):
            zone_info = {
                'name': 'Home',
                'latitude': 1,
                'longitude': 2,
                'radius': 250,
                'passive': False
            }

            setup_component(self.hass, zone.DOMAIN, {'zone': zone_info})

        scanner = get_component(self.hass, 'device_tracker.test').SCANNER
        scanner.reset()
        scanner.come_home('dev1')

        with patch('homeassistant.components.device_tracker.dt_util.utcnow',
                   return_value=register_time):
            with assert_setup_component(1, device_tracker.DOMAIN):
                assert setup_component(
                    self.hass, device_tracker.DOMAIN, {
                        device_tracker.DOMAIN: {
                            CONF_PLATFORM: 'test',
                            device_tracker.CONF_CONSIDER_HOME: 59,
                        }
                    })
                self.hass.block_till_done()

        state = self.hass.states.get('device_tracker.dev1')
        attrs = state.attributes
        self.assertEqual(STATE_HOME, state.state)
        self.assertEqual(state.object_id, 'dev1')
        self.assertEqual(state.name, 'dev1')
        self.assertEqual(attrs.get('friendly_name'), 'dev1')
        self.assertEqual(attrs.get('latitude'), 1)
        self.assertEqual(attrs.get('longitude'), 2)
        self.assertEqual(attrs.get('gps_accuracy'), 0)
        self.assertEqual(attrs.get('source_type'),
                         device_tracker.SOURCE_TYPE_ROUTER)

        scanner.leave_home('dev1')

        with patch('homeassistant.components.device_tracker.dt_util.utcnow',
                   return_value=scan_time):
            fire_time_changed(self.hass, scan_time)
            self.hass.block_till_done()

        state = self.hass.states.get('device_tracker.dev1')
        attrs = state.attributes
        self.assertEqual(STATE_NOT_HOME, state.state)
        self.assertEqual(state.object_id, 'dev1')
        self.assertEqual(state.name, 'dev1')
        self.assertEqual(attrs.get('friendly_name'), 'dev1')
        self.assertEqual(attrs.get('latitude'), None)
        self.assertEqual(attrs.get('longitude'), None)
        self.assertEqual(attrs.get('gps_accuracy'), None)
        self.assertEqual(attrs.get('source_type'),
                         device_tracker.SOURCE_TYPE_ROUTER)