"""Einrichten, Entladen und das Geraet in der Registrierung.""" from __future__ import annotations from unittest.mock import MagicMock from homeassistant.config_entries import ConfigEntryState from homeassistant.const import CONF_TOKEN from homeassistant.core import HomeAssistant from homeassistant.data_entry_flow import FlowResultType from homeassistant.helpers import device_registry as dr from homeassistant.helpers import issue_registry as ir from pytest_homeassistant_custom_component.common import MockConfigEntry from custom_components.ninstrip.api import NinstripAuthError, NinstripConnectionError from custom_components.ninstrip.const import ( CONF_DEVICE_BASE_URL, CONF_SCAN_INTERVAL, DEFAULT_TOKEN, DOMAIN, ) from tests_unit.samples import MAC_K1Y async def test_einrichten_und_entladen( hass: HomeAssistant, init_integration: MockConfigEntry ) -> None: assert init_integration.state is ConfigEntryState.LOADED assert await hass.config_entries.async_unload(init_integration.entry_id) await hass.async_block_till_done() assert init_integration.state is ConfigEntryState.NOT_LOADED async def test_geraet_traegt_mac_modell_und_version( hass: HomeAssistant, init_integration: MockConfigEntry ) -> None: geraet = dr.async_get(hass).async_get_device_by_identifier( (DOMAIN, MAC_K1Y), init_integration.entry_id ) assert geraet is not None assert geraet.manufacturer == "ninstrip" # Das Modell beschreibt den Strip, nicht das Produkt - das steht in model_id. assert geraet.model == "sk6812_rgbw, 60 Pixel" assert geraet.model_id == "ninstrip" assert geraet.sw_version == "0.1.0-12-g71a57" assert (dr.CONNECTION_NETWORK_MAC, MAC_K1Y) in geraet.connections async def test_unerreichbares_geraet_wird_spaeter_erneut_versucht( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_client: MagicMock ) -> None: mock_client.get_state.side_effect = NinstripConnectionError("weg") mock_config_entry.add_to_hass(hass) assert not await hass.config_entries.async_setup(mock_config_entry.entry_id) assert mock_config_entry.state is ConfigEntryState.SETUP_RETRY async def test_abgelehntes_token_verlangt_neue_anmeldung( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_client: MagicMock ) -> None: mock_client.get_state.side_effect = NinstripAuthError("abgelehnt") mock_config_entry.add_to_hass(hass) assert not await hass.config_entries.async_setup(mock_config_entry.entry_id) assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR assert any( flow["context"]["source"] == "reauth" for flow in hass.config_entries.flow.async_progress() ) async def test_einstellungen_lassen_sich_am_laufenden_eintrag_sichern( hass: HomeAssistant, init_integration: MockConfigEntry ) -> None: """Am wirklich eingerichteten Eintrag - hier klemmte es. Der Eintrag hatte zusaetzlich einen add_update_listener zum Neuladen, und zusammen mit einem OptionsFlowWithReload weist Home Assistant das Speichern rundheraus ab ("Config entry update listeners should not be used with OptionsFlowWithReload"). In ha-dev liess sich dadurch keine einzige Einstellung sichern. Die Tests in test_config_flow.py sehen das nicht: dort ist async_setup_entry durchweg ersetzt, der Eintrag wird also nie wirklich aufgesetzt und meldet auch keinen Listener an. """ ergebnis = await hass.config_entries.options.async_init(init_integration.entry_id) ergebnis = await hass.config_entries.options.async_configure( ergebnis["flow_id"], {CONF_SCAN_INTERVAL: 45, CONF_DEVICE_BASE_URL: "http://192.168.1.131:8124"}, ) await hass.async_block_till_done() assert ergebnis["type"] is FlowResultType.CREATE_ENTRY assert init_integration.options == { CONF_SCAN_INTERVAL: 45, CONF_DEVICE_BASE_URL: "http://192.168.1.131:8124", } async def test_werks_token_gibt_einen_hinweis( hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_client: MagicMock ) -> None: """Wer im selben Netz ist, kann ein Geraet mit Werks-Token vollstaendig uebernehmen - das soll man sehen, nicht raten muessen.""" mock_config_entry.add_to_hass(hass) hass.config_entries.async_update_entry( mock_config_entry, data={**mock_config_entry.data, CONF_TOKEN: DEFAULT_TOKEN} ) assert await hass.config_entries.async_setup(mock_config_entry.entry_id) await hass.async_block_till_done() hinweis = ir.async_get(hass).async_get_issue( DOMAIN, f"default_token_{mock_config_entry.entry_id}" ) assert hinweis is not None assert hinweis.severity is ir.IssueSeverity.WARNING async def test_eigenes_token_gibt_keinen_hinweis( hass: HomeAssistant, init_integration: MockConfigEntry ) -> None: assert ( ir.async_get(hass).async_get_issue(DOMAIN, f"default_token_{init_integration.entry_id}") is None )