def test_home_assistant_core_config_validation(self): """Test if we pass in wrong information for HA conf.""" # Extensive HA conf validation testing is done in test_config.py assert None is bootstrap.from_config_dict( {'homeassistant': { 'latitude': 'some string' }})
def main(): """ Starts Home Assistant. """ validate_python() validate_dependencies() bootstrap = ensure_path_and_load_bootstrap() validate_git_submodules() args = get_arguments() config_dir = os.path.join(os.getcwd(), args.config) config_path = ensure_config_path(config_dir) if args.demo_mode: from homeassistant.components import http, demo # Demo mode only requires http and demo components. hass = bootstrap.from_config_dict({http.DOMAIN: {}, demo.DOMAIN: {}}) else: hass = bootstrap.from_config_file(config_path) if args.open_ui: from homeassistant.const import EVENT_HOMEASSISTANT_START def open_browser(event): """ Open the webinterface in a browser. """ if hass.local_api is not None: import webbrowser webbrowser.open(hass.local_api.base_url) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) hass.start() hass.block_till_stopped()
def main(): """ Starts Home Assistant. """ args = get_arguments() config_dir = os.path.join(os.getcwd(), args.config) ensure_config_path(config_dir) if args.demo_mode: hass = bootstrap.from_config_dict({ 'frontend': {}, 'demo': {} }, config_dir=config_dir) else: config_file = ensure_config_file(config_dir) hass = bootstrap.from_config_file(config_file) if args.open_ui: def open_browser(event): """ Open the webinterface in a browser. """ if hass.config.api is not None: import webbrowser webbrowser.open(hass.config.api.base_url) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) hass.start() hass.block_till_stopped()
def test_home_assistant_core_config_validation(self): """Test if we pass in wrong information for HA conf.""" # Extensive HA conf validation testing is done in test_config.py assert None is bootstrap.from_config_dict({ 'homeassistant': { 'latitude': 'some string' } })
def test_all_work_done_before_start(self): """Test all init work done till start.""" call_order = [] def component1_setup(hass, config): """Setup mock component.""" discovery.discover(hass, 'test_component2', component='test_component2') discovery.discover(hass, 'test_component3', component='test_component3') return True def component_track_setup(hass, config): """Setup mock component.""" call_order.append(1) return True loader.set_component( 'test_component1', MockModule('test_component1', setup=component1_setup)) loader.set_component( 'test_component2', MockModule('test_component2', setup=component_track_setup)) loader.set_component( 'test_component3', MockModule('test_component3', setup=component_track_setup)) @callback def track_start(event): """Track start event.""" call_order.append(2) self.hass.bus.listen_once(EVENT_HOMEASSISTANT_START, track_start) self.hass.loop.run_until_complete = \ lambda _: self.hass.block_till_done() bootstrap.from_config_dict({'test_component1': None}, self.hass) self.hass.start() assert call_order == [1, 1, 2]
def main(): """ Starts Home Assistant. """ validate_python() args = get_arguments() config_dir = os.path.join(os.getcwd(), args.config) ensure_config_path(config_dir) # os x launchd functions if args.install_osx: install_osx() return if args.uninstall_osx: uninstall_osx() return if args.restart_osx: uninstall_osx() install_osx() return # daemon functions if args.pid_file: check_pid(args.pid_file) if args.daemon: daemonize() if args.pid_file: write_pid(args.pid_file) if args.demo_mode: config = { 'frontend': {}, 'demo': {} } hass = bootstrap.from_config_dict( config, config_dir=config_dir, daemon=args.daemon, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) else: config_file = ensure_config_file(config_dir) print('Config directory:', config_dir) hass = bootstrap.from_config_file( config_file, daemon=args.daemon, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) if args.open_ui: def open_browser(event): """ Open the webinterface in a browser. """ if hass.config.api is not None: import webbrowser webbrowser.open(hass.config.api.base_url) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) hass.start() hass.block_till_stopped()
def test_1st_discovers_2nd_component(self, mock_signal): """Test that we don't break if one component discovers the other. If the first component fires a discovery event to setup the second component while the second component is about to be setup, it should not setup the second component twice. """ component_calls = [] def component1_setup(hass, config): """Setup mock component.""" discovery.discover(hass, 'test_component2', component='test_component2') return True def component2_setup(hass, config): """Setup mock component.""" component_calls.append(1) return True loader.set_component( 'test_component1', MockModule('test_component1', setup=component1_setup)) loader.set_component( 'test_component2', MockModule('test_component2', setup=component2_setup)) config = OrderedDict() config['test_component1'] = {} config['test_component2'] = {} self.hass.loop.run_until_complete = \ lambda _: self.hass.block_till_done() bootstrap.from_config_dict(config, self.hass) self.hass.block_till_done() # test_component will only be setup once assert len(component_calls) == 1
def test_1st_discovers_2nd_component(self): """Test that we don't break if one component discovers the other. If the first component fires a discovery event to setup the second component while the second component is about to be setup, it should not setup the second component twice. """ component_calls = [] def component1_setup(hass, config): """Setup mock component.""" discovery.discover(hass, 'test_component2', component='test_component2') return True def component2_setup(hass, config): """Setup mock component.""" component_calls.append(1) return True loader.set_component( 'test_component1', MockModule('test_component1', setup=component1_setup)) loader.set_component( 'test_component2', MockModule('test_component2', setup=component2_setup)) config = OrderedDict() config['test_component1'] = {} config['test_component2'] = {} self.hass.loop.run_until_complete = \ lambda _: self.hass.block_till_done() bootstrap.from_config_dict(config, self.hass) self.hass.block_till_done() # test_component will only be setup once assert len(component_calls) == 1
def setup_and_run_hass(config_dir: str, args: argparse.Namespace) -> Optional[int]: """Set up HASS and run.""" from homeassistant import bootstrap # Run a simple daemon runner process on Windows to handle restarts if os.name == 'nt' and '--runner' not in sys.argv: nt_args = cmdline() + ['--runner'] while True: try: subprocess.check_call(nt_args) sys.exit(0) except subprocess.CalledProcessError as exc: if exc.returncode != RESTART_EXIT_CODE: sys.exit(exc.returncode) if args.demo_mode: config = { 'frontend': {}, 'demo': {} } # type: Dict[str, Any] hass = bootstrap.from_config_dict( config, config_dir=config_dir, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days, log_file=args.log_file, log_no_color=args.log_no_color) else: config_file = ensure_config_file(config_dir) print('Config directory:', config_dir) hass = bootstrap.from_config_file( config_file, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days, log_file=args.log_file, log_no_color=args.log_no_color) if hass is None: return None if args.open_ui: # Imported here to avoid importing asyncio before monkey patch from homeassistant.util.async_ import run_callback_threadsafe def open_browser(event): """Open the webinterface in a browser.""" if hass.config.api is not None: import webbrowser webbrowser.open(hass.config.api.base_url) run_callback_threadsafe( hass.loop, hass.bus.async_listen_once, EVENT_HOMEASSISTANT_START, open_browser ) return hass.start()
def setup_and_run_hass(config_dir: str, args: argparse.Namespace) -> Optional[int]: """Set up HASS and run.""" from homeassistant import bootstrap # Run a simple daemon runner process on Windows to handle restarts if os.name == 'nt' and '--runner' not in sys.argv: nt_args = cmdline() + ['--runner'] while True: try: subprocess.check_call(nt_args) sys.exit(0) except subprocess.CalledProcessError as exc: if exc.returncode != RESTART_EXIT_CODE: sys.exit(exc.returncode) if args.demo_mode: config = { 'frontend': {}, 'demo': {} } hass = bootstrap.from_config_dict( config, config_dir=config_dir, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days, log_file=args.log_file, log_no_color=args.log_no_color) else: config_file = ensure_config_file(config_dir) print('Config directory:', config_dir) hass = bootstrap.from_config_file( config_file, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days, log_file=args.log_file, log_no_color=args.log_no_color) if hass is None: return None if args.open_ui: # Imported here to avoid importing asyncio before monkey patch from homeassistant.util.async_ import run_callback_threadsafe def open_browser(event): """Open the webinterface in a browser.""" if hass.config.api is not None: import webbrowser webbrowser.open(hass.config.api.base_url) run_callback_threadsafe( hass.loop, hass.bus.async_listen_once, EVENT_HOMEASSISTANT_START, open_browser ) return hass.start()
def test_circular_import(self): """Test we don't break doing circular import.""" component_calls = [] platform_calls = [] def component_setup(hass, config): """Setup mock component.""" discovery.load_platform(hass, 'switch', 'test_circular') component_calls.append(1) return True def setup_platform(hass, config, add_devices_callback, discovery_info=None): """Setup mock platform.""" platform_calls.append(1) loader.set_component( 'test_component', MockModule('test_component', setup=component_setup)) loader.set_component( 'switch.test_circular', MockPlatform(setup_platform, dependencies=['test_component'])) bootstrap.from_config_dict({ 'test_component': None, 'switch': [{ 'platform': 'test_circular', }], }, self.hass) self.hass.pool.block_till_done() assert 'test_component' in self.hass.config.components assert 'switch' in self.hass.config.components assert len(component_calls) == 1 assert len(platform_calls) == 2
def test_from_config_dict_not_mount_deps_folder(loop): """Test that we do not mount the deps folder inside from_config_dict.""" with patch('homeassistant.bootstrap.is_virtual_env', return_value=False), \ patch('homeassistant.core.HomeAssistant', return_value=Mock(loop=loop)), \ patch('homeassistant.bootstrap.async_mount_local_lib_path', return_value=mock_coro()) as mock_mount, \ patch('homeassistant.bootstrap.async_from_config_dict', return_value=mock_coro()): bootstrap.from_config_dict({}, config_dir='.') assert len(mock_mount.mock_calls) == 1 with patch('homeassistant.bootstrap.is_virtual_env', return_value=True), \ patch('homeassistant.core.HomeAssistant', return_value=Mock(loop=loop)), \ patch('homeassistant.bootstrap.async_mount_local_lib_path', return_value=mock_coro()) as mock_mount, \ patch('homeassistant.bootstrap.async_from_config_dict', return_value=mock_coro()): bootstrap.from_config_dict({}, config_dir='.') assert len(mock_mount.mock_calls) == 0
def setup_and_run_hass(config_dir: str, args: argparse.Namespace) -> Optional[int]: """Setup HASS and run.""" from homeassistant import bootstrap # Run a simple daemon runner process on Windows to handle restarts if os.name == 'nt' and '--runner' not in sys.argv: nt_args = cmdline() + ['--runner'] while True: try: subprocess.check_call(nt_args) sys.exit(0) except subprocess.CalledProcessError as exc: if exc.returncode != RESTART_EXIT_CODE: sys.exit(exc.returncode) if args.demo_mode: config = {'frontend': {}, 'demo': {}} hass = bootstrap.from_config_dict(config, config_dir=config_dir, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) else: config_file = ensure_config_file(config_dir) print('Config directory:', config_dir) hass = bootstrap.from_config_file(config_file, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) if hass is None: return None if args.open_ui: def open_browser(event): """Open the webinterface in a browser.""" if hass.config.api is not None: import webbrowser webbrowser.open(hass.config.api.base_url) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) hass.start() exit_code = int(hass.block_till_stopped()) return exit_code
def setup_and_run_hass(config_dir, args): """Setup HASS and run.""" from homeassistant import bootstrap # Run a simple daemon runner process on Windows to handle restarts if os.name == 'nt' and '--runner' not in sys.argv: args = cmdline() + ['--runner'] while True: try: subprocess.check_call(args) sys.exit(0) except subprocess.CalledProcessError as exc: if exc.returncode != RESTART_EXIT_CODE: sys.exit(exc.returncode) if args.demo_mode: config = { 'frontend': {}, 'demo': {} } hass = bootstrap.from_config_dict( config, config_dir=config_dir, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) else: config_file = ensure_config_file(config_dir) print('Config directory:', config_dir) hass = bootstrap.from_config_file( config_file, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) if hass is None: return if args.open_ui: def open_browser(event): """Open the webinterface in a browser.""" if hass.config.api is not None: import webbrowser webbrowser.open(hass.config.api.base_url) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) print('Starting Home-Assistant') hass.start() exit_code = int(hass.block_till_stopped()) return exit_code
def main(): """ Starts Home Assistant. """ validate_python() args = get_arguments() config_dir = os.path.join(os.getcwd(), args.config) ensure_config_path(config_dir) # daemon functions if args.pid_file: check_pid(args.pid_file) if args.daemon: daemonize() if args.pid_file: write_pid(args.pid_file) if args.demo_mode: config = { 'frontend': {}, 'demo': {} } hass = bootstrap.from_config_dict( config, config_dir=config_dir, daemon=args.daemon, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) else: config_file = ensure_config_file(config_dir) print('Config directory:', config_dir) hass = bootstrap.from_config_file( config_file, daemon=args.daemon, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) if args.open_ui: def open_browser(event): """ Open the webinterface in a browser. """ if hass.config.api is not None: import webbrowser webbrowser.open(hass.config.api.base_url) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) hass.start() hass.block_till_stopped()
def setup_and_run_hass(config_dir, args, top_process=False): """Setup HASS and run. Block until stopped. Will assume it is running in a subprocess unless top_process is set to true. """ from homeassistant import bootstrap if args.demo_mode: config = {'frontend': {}, 'demo': {}} hass = bootstrap.from_config_dict(config, config_dir=config_dir, daemon=args.daemon, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) else: config_file = ensure_config_file(config_dir) print('Config directory:', config_dir) hass = bootstrap.from_config_file(config_file, daemon=args.daemon, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) if hass is None: return if args.open_ui: def open_browser(event): """Open the webinterface in a browser.""" if hass.config.api is not None: import webbrowser webbrowser.open(hass.config.api.base_url) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) hass.start() exit_code = int(hass.block_till_stopped()) if not top_process: sys.exit(exit_code) return exit_code
def setup_and_run_hass(config_dir, args, top_process=False): """Setup HASS and run. Block until stopped. Will assume it is running in a subprocess unless top_process is set to true. """ from homeassistant import bootstrap if args.demo_mode: config = { 'frontend': {}, 'demo': {} } hass = bootstrap.from_config_dict( config, config_dir=config_dir, daemon=args.daemon, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) else: config_file = ensure_config_file(config_dir) print('Config directory:', config_dir) hass = bootstrap.from_config_file( config_file, daemon=args.daemon, verbose=args.verbose, skip_pip=args.skip_pip, log_rotate_days=args.log_rotate_days) if hass is None: return if args.open_ui: def open_browser(event): """Open the webinterface in a browser.""" if hass.config.api is not None: import webbrowser webbrowser.open(hass.config.api.base_url) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) hass.start() exit_code = int(hass.block_till_stopped()) if not top_process: sys.exit(exit_code) return exit_code
def main(): """ Starts Home Assistant. """ validate_python() validate_dependencies() # Windows needs this to pick up new modules importlib.invalidate_caches() bootstrap = ensure_path_and_load_bootstrap() validate_git_submodules() args = get_arguments() config_dir = os.path.join(os.getcwd(), args.config) config_path = ensure_config_path(config_dir) if args.demo_mode: from homeassistant.components import frontend, demo hass = bootstrap.from_config_dict({ frontend.DOMAIN: {}, demo.DOMAIN: {} }) else: hass = bootstrap.from_config_file(config_path) if args.open_ui: from homeassistant.const import EVENT_HOMEASSISTANT_START def open_browser(event): """ Open the webinterface in a browser. """ if hass.config.api is not None: import webbrowser webbrowser.open(hass.config.api.base_url) hass.bus.listen_once(EVENT_HOMEASSISTANT_START, open_browser) hass.start() hass.block_till_stopped()