async def test_is_onboarded(): """Test the is onboarded function.""" hass = Mock() hass.data = {} assert onboarding.async_is_onboarded(hass) hass.data[onboarding.DOMAIN] = True assert onboarding.async_is_onboarded(hass) hass.data[onboarding.DOMAIN] = False assert not onboarding.async_is_onboarded(hass)
async def test_is_onboarded(): """Test the is onboarded function.""" hass = Mock() hass.data = {} assert onboarding.async_is_onboarded(hass) hass.data[onboarding.DOMAIN] = True assert onboarding.async_is_onboarded(hass) hass.data[onboarding.DOMAIN] = {"done": []} assert not onboarding.async_is_onboarded(hass)
async def test_is_onboarded(): """Test the is onboarded function.""" hass = Mock() hass.data = {} with patch('homeassistant.auth.AuthManager.active', return_value=False): assert onboarding.async_is_onboarded(hass) with patch('homeassistant.auth.AuthManager.active', return_value=True): assert onboarding.async_is_onboarded(hass) hass.data[onboarding.DOMAIN] = True assert onboarding.async_is_onboarded(hass) hass.data[onboarding.DOMAIN] = False assert not onboarding.async_is_onboarded(hass)
async def async_step_confirm(self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Confirm setup.""" if user_input is None and onboarding.async_is_onboarded(self.hass): self._set_confirm_only() return self.async_show_form(step_id="confirm") if self.source == config_entries.SOURCE_USER: # Get current discovered entries. in_progress = self._async_in_progress() if not (has_devices := bool(in_progress)): has_devices = await cast( "asyncio.Future[bool]", self.hass.async_add_job(self._discovery_function, self.hass), ) if not has_devices: return self.async_abort(reason="no_devices_found") # Cancel the discovered one. for flow in in_progress: self.hass.config_entries.flow.async_abort(flow["flow_id"])
def _need_auth(hass, path: str) -> bool: """Return if a path need authentication.""" if not async_is_onboarded(hass) and NO_AUTH_ONBOARDING.match(path): return False if NO_AUTH.match(path): return False return True
async def async_step_single_adapter(self, user_input: dict[str, Any] | None = None) -> FlowResult: """Select an adapter.""" adapter = self._adapter details = self._details assert adapter is not None assert details is not None address = details[ADAPTER_ADDRESS] if user_input is not None or not onboarding.async_is_onboarded( self.hass): await self.async_set_unique_id(address, raise_on_progress=False) self._abort_if_unique_id_configured() return self.async_create_entry(title=adapter_unique_name( adapter, address), data={}) return self.async_show_form( step_id="single_adapter", description_placeholders={ "name": adapter_human_name(adapter, address) }, )
async def test_setup_views_if_not_onboarded(hass): """Test if onboarding is not done, we setup views.""" with patch('homeassistant.components.onboarding.views.async_setup', return_value=mock_coro()) as mock_setup: assert await async_setup_component(hass, 'onboarding', {}) assert len(mock_setup.mock_calls) == 1 assert onboarding.DOMAIN in hass.data assert not onboarding.async_is_onboarded(hass)
async def test_migration(hass, hass_storage): """Test migrating onboarding to new version.""" hass_storage[onboarding.STORAGE_KEY] = { 'version': 1, 'data': { 'done': ["user"] } } assert await async_setup_component(hass, 'onboarding', {}) assert onboarding.async_is_onboarded(hass)
async def test_migration(hass, hass_storage): """Test migrating onboarding to new version.""" hass_storage[onboarding.STORAGE_KEY] = { "version": 1, "data": { "done": ["user"] } } assert await async_setup_component(hass, "onboarding", {}) assert onboarding.async_is_onboarded(hass)
async def test_not_setup_views_if_onboarded(hass, hass_storage): """Test if onboarding is done, we don't setup views.""" mock_storage(hass_storage, {"done": onboarding.STEPS}) with patch("homeassistant.components.onboarding.views.async_setup" ) as mock_setup: assert await async_setup_component(hass, "onboarding", {}) assert len(mock_setup.mock_calls) == 0 assert onboarding.DOMAIN not in hass.data assert onboarding.async_is_onboarded(hass)
async def test_setup_views_if_not_onboarded(hass): """Test if onboarding is not done, we setup views.""" with patch( 'homeassistant.components.onboarding.views.async_setup', return_value=mock_coro() ) as mock_setup: assert await async_setup_component(hass, 'onboarding', {}) assert len(mock_setup.mock_calls) == 1 assert onboarding.DOMAIN in hass.data assert not onboarding.async_is_onboarded(hass)
async def async_step_bluetooth_confirm( self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Confirm discovery.""" if user_input is not None or not onboarding.async_is_onboarded(self.hass): return self._async_get_or_create_entry() self._set_confirm_only() return self.async_show_form( step_id="bluetooth_confirm", description_placeholders=self.context["title_placeholders"], )
async def test_not_setup_views_if_onboarded(hass, hass_storage): """Test if onboarding is done, we don't setup views.""" mock_storage(hass_storage, { 'done': onboarding.STEPS }) with patch( 'homeassistant.components.onboarding.views.async_setup' ) as mock_setup: assert await async_setup_component(hass, 'onboarding', {}) assert len(mock_setup.mock_calls) == 0 assert onboarding.DOMAIN not in hass.data assert onboarding.async_is_onboarded(hass)
async def test_having_owner_finishes_user_step(hass, hass_storage): """If owner user already exists, mark user step as complete.""" MockUser(is_owner=True).add_to_hass(hass) with patch("homeassistant.components.onboarding.views.async_setup" ) as mock_setup, patch.object(onboarding, "STEPS", [onboarding.STEP_USER]): assert await async_setup_component(hass, "onboarding", {}) assert len(mock_setup.mock_calls) == 0 assert onboarding.DOMAIN not in hass.data assert onboarding.async_is_onboarded(hass) done = hass_storage[onboarding.STORAGE_KEY]["data"]["done"] assert onboarding.STEP_USER in done
async def test_having_owner_finishes_user_step(hass, hass_storage): """If owner user already exists, mark user step as complete.""" MockUser(is_owner=True).add_to_hass(hass) with patch( 'homeassistant.components.onboarding.views.async_setup' ) as mock_setup, patch.object(onboarding, 'STEPS', [onboarding.STEP_USER]): assert await async_setup_component(hass, 'onboarding', {}) assert len(mock_setup.mock_calls) == 0 assert onboarding.DOMAIN not in hass.data assert onboarding.async_is_onboarded(hass) done = hass_storage[onboarding.STORAGE_KEY]['data']['done'] assert onboarding.STEP_USER in done
async def async_step_zeroconf_confirm(self, user_input: dict[str, Any] | None = None) -> FlowResult: """Handle a flow initiated by zeroconf.""" if user_input is not None or not onboarding.async_is_onboarded( self.hass): return self.async_create_entry( title=self.discovered_device.info.name, data={ CONF_HOST: self.discovered_host, }, ) return self.async_show_form( step_id="zeroconf_confirm", description_placeholders={ "name": self.discovered_device.info.name }, )
async def get(self, request: web.Request) -> web.Response: """Serve the index page for panel pages.""" hass = request.app["hass"] if not onboarding.async_is_onboarded(hass): return web.Response(status=302, headers={"location": "/onboarding.html"}) template = self._template_cache or await hass.async_add_executor_job( self.get_template) return web.Response( text=_async_render_index_cached( template, theme_color=MANIFEST_JSON["theme_color"], extra_modules=hass.data[DATA_EXTRA_MODULE_URL].urls, extra_js_es5=hass.data[DATA_EXTRA_JS_URL_ES5].urls, ), content_type="text/html", )
async def async_step_zeroconf( self, discovery_info: zeroconf.ZeroconfServiceInfo ) -> FlowResult: """Handle zeroconf discovery.""" self.host = discovery_info.host self.mac = discovery_info.properties.get("id") self.port = discovery_info.port or 9123 try: await self._get_elgato_serial_number() except ElgatoError: return self.async_abort(reason="cannot_connect") if not onboarding.async_is_onboarded(self.hass): return self._async_create_entry() self._set_confirm_only() return self.async_show_form( step_id="zeroconf_confirm", description_placeholders={"serial_number": self.serial_number}, )
async def async_step_discovery_confirm( self, user_input: dict[str, Any] | None = None ) -> FlowResult: """Confirm discovery.""" ip_address = self._discovered_device.ip_address if user_input is not None or not onboarding.async_is_onboarded(self.hass): # Make sure the device is still there and # update the name if the firmware has auto # updated since discovery await self._async_connect_discovered_or_abort() return self.async_create_entry( title=self._name, data={CONF_HOST: ip_address}, ) self._set_confirm_only() placeholders = {"name": self._name, "host": ip_address} self.context["title_placeholders"] = placeholders return self.async_show_form( step_id="discovery_confirm", description_placeholders=placeholders, )
async def async_step_discovery_confirm(self, user_input=None): """Confirm discovery.""" if user_input is not None or not onboarding.async_is_onboarded( self.hass): return self.async_create_entry( title=async_format_model_id(self._discovered_model, self.unique_id), data={ CONF_ID: self.unique_id, CONF_HOST: self._discovered_ip, CONF_MODEL: self._discovered_model, }, ) self._set_confirm_only() placeholders = { "id": async_format_id(self.unique_id), "model": async_format_model(self._discovered_model), "host": self._discovered_ip, } self.context["title_placeholders"] = placeholders return self.async_show_form(step_id="discovery_confirm", description_placeholders=placeholders)