async def test_import_already_configured(opp): """Test we handle already configured from import.""" await setup.async_setup_component(opp, "persistent_notification", {}) entry = MockConfigEntry( domain=config_flow.DOMAIN, data=VALID_CONFIG, ) entry.add_to_opp(opp) with patch("openpeerpower.components.foscam.config_flow.FoscamCamera", ) as mock_foscam_camera: setup_mock_foscam_camera(mock_foscam_camera) result = await opp.config_entries.flow.async_init( config_flow.DOMAIN, context={"source": config_entries.SOURCE_IMPORT}, data=VALID_CONFIG, ) await opp.async_block_till_done() assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured"
async def test_discovery_update_light(hass, mqtt_mock, caplog): """Test removal of discovered light.""" entry = MockConfigEntry(domain=mqtt.DOMAIN) await async_start(hass, 'homeassistant', {}, entry) data1 = ( '{ "name": "Beer",' ' "schema": "json",' ' "status_topic": "test_topic",' ' "command_topic": "test_topic" }' ) data2 = ( '{ "name": "Milk",' ' "schema": "json",' ' "status_topic": "test_topic",' ' "command_topic": "test_topic" }' ) async_fire_mqtt_message(hass, 'homeassistant/light/bla/config', data1) await hass.async_block_till_done() state = hass.states.get('light.beer') assert state is not None assert state.name == 'Beer' async_fire_mqtt_message(hass, 'homeassistant/light/bla/config', data2) await hass.async_block_till_done() await hass.async_block_till_done() state = hass.states.get('light.beer') assert state is not None assert state.name == 'Milk' state = hass.states.get('light.milk') assert state is None
async def test_user_input_device_already_existing(hass, mock_get_device_info_valid): """Test when user specifies an existing device.""" mock_entry = MockConfigEntry( domain=DOMAIN, unique_id="1234567890", data={ CONF_HOST: "192.168.188.18", "model": "MC20", "serial": "1234567890" }, ) mock_entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}) result2 = await hass.config_entries.flow.async_configure( result["flow_id"], {"host": "192.168.188.18"}, ) assert result2["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result2["reason"] == "already_configured"
async def test_duplicate_error(hass, mock_async_from_auth): """Test that errors are shown when duplicates are added.""" MockConfigEntry( domain=DOMAIN, unique_id="12345", data={ CONF_USER_ID: "12345", CONF_TOKEN: "token123", }, ).add_to_hass(hass) with patch("homeassistant.components.simplisafe.async_setup_entry", return_value=True): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}) assert result["step_id"] == "user" assert result["type"] == data_entry_flow.RESULT_TYPE_FORM result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={}) result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_AUTH_CODE: "code123"}) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured"
async def test_setup_discovery(hass: HomeAssistant): """Test setting up Yeelight by discovery.""" config_entry = MockConfigEntry(domain=DOMAIN, data=CONFIG_ENTRY_DATA) config_entry.add_to_hass(hass) mocked_bulb = _mocked_bulb() with _patch_discovery(MODULE), patch(f"{MODULE}.Bulb", return_value=mocked_bulb): assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() assert hass.states.get(ENTITY_BINARY_SENSOR) is not None assert hass.states.get(ENTITY_LIGHT) is not None # Unload assert await hass.config_entries.async_unload(config_entry.entry_id) assert hass.states.get(ENTITY_BINARY_SENSOR).state == STATE_UNAVAILABLE assert hass.states.get(ENTITY_LIGHT).state == STATE_UNAVAILABLE # Remove assert await hass.config_entries.async_remove(config_entry.entry_id) await hass.async_block_till_done() assert hass.states.get(ENTITY_BINARY_SENSOR) is None assert hass.states.get(ENTITY_LIGHT) is None
async def test_addon_options_changed( hass, client, addon_installed, addon_running, install_addon, addon_options, start_addon, old_device, new_device, old_network_key, new_network_key, ): """Test update config entry data on entry setup if add-on options changed.""" addon_options["device"] = new_device addon_options["network_key"] = new_network_key entry = MockConfigEntry( domain=DOMAIN, title="Z-Wave JS", data={ "url": "ws://host1:3001", "use_addon": True, "usb_path": old_device, "network_key": old_network_key, }, ) entry.add_to_hass(hass) await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() assert entry.state == ConfigEntryState.LOADED assert entry.data["usb_path"] == new_device assert entry.data["network_key"] == new_network_key assert install_addon.call_count == 0 assert start_addon.call_count == 0
async def test_get_conditions_hidden_auxiliary( hass, device_reg, entity_reg, hidden_by, entity_category, ): """Test we get the expected conditions from a hidden or auxiliary entity.""" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) device_entry = device_reg.async_get_or_create( config_entry_id=config_entry.entry_id, connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) entity_reg.async_get_or_create( DOMAIN, "test", "5678", device_id=device_entry.id, entity_category=entity_category, hidden_by=hidden_by, ) expected_conditions = [{ "condition": "device", "domain": DOMAIN, "type": condition, "device_id": device_entry.id, "entity_id": f"{DOMAIN}.test_5678", "metadata": { "secondary": True }, } for condition in ["is_hvac_mode"]] conditions = await async_get_device_automations( hass, DeviceAutomationType.CONDITION, device_entry.id) assert_lists_same(conditions, expected_conditions)
async def test_entity_device_info_with_identifier(hass, mqtt_mock): """Test MQTT light device registry integration.""" entry = MockConfigEntry(domain=mqtt.DOMAIN) entry.add_to_hass(hass) await async_start(hass, 'homeassistant', {}, entry) registry = await hass.helpers.device_registry.async_get_registry() data = json.dumps({ 'platform': 'mqtt', 'name': 'Test 1', 'state_topic': 'test-topic', 'command_topic': 'test-topic', 'device': { 'identifiers': ['helloworld'], 'connections': [ ["mac", "02:5b:26:a8:dc:12"], ], 'manufacturer': 'Whatever', 'name': 'Beer', 'model': 'Glass', 'sw_version': '0.1-beta', }, 'unique_id': 'veryunique' }) async_fire_mqtt_message(hass, 'homeassistant/light/bla/config', data) await hass.async_block_till_done() device = registry.async_get_device({('mqtt', 'helloworld')}, set()) assert device is not None assert device.identifiers == {('mqtt', 'helloworld')} assert device.connections == {('mac', "02:5b:26:a8:dc:12")} assert device.manufacturer == 'Whatever' assert device.name == 'Beer' assert device.model == 'Glass' assert device.sw_version == '0.1-beta'
async def test_duplicate_abort(hass): """Test that Flow aborts when found devices already configured.""" MockConfigEntry(domain=ps4.DOMAIN, data=MOCK_DATA).add_to_hass(hass) with patch("pyps4_2ndscreen.Helper.port_bind", return_value=None): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "creds" with patch("pyps4_2ndscreen.Helper.get_creds", return_value=MOCK_CREDS): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "mode" with patch("pyps4_2ndscreen.Helper.has_devices", return_value=[{ "host-ip": MOCK_HOST }]): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=MOCK_AUTO) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured"
async def test_set_up_local(opp, aioclient_mock): """Test we do not set up Almond to connect to OPP if we use local.""" # Set up an internal URL, as Almond won't be set up if there is no URL available await async_process_op_core_config( opp, {"internal_url": "https://192.168.0.1"}, ) entry = MockConfigEntry( domain="almond", data={ "type": const.TYPE_LOCAL, "host": "http://localhost:9999" }, ) entry.add_to_opp(opp) with patch( "pyalmond.WebAlmondAPI.async_create_device") as mock_create_device: assert await async_setup_component(opp, "almond", {}) assert entry.state is config_entries.ConfigEntryState.LOADED assert len(mock_create_device.mock_calls) == 1
async def test_set_up_oauth_no_external_url(opp, aioclient_mock): """Test we do not set up Almond to connect to OPP if we have no external url.""" entry = MockConfigEntry( domain="almond", data={ "type": const.TYPE_OAUTH2, "auth_implementation": "local", "host": "http://localhost:9999", "token": { "expires_at": time() + 1000, "access_token": "abcd" }, }, ) entry.add_to_opp(opp) with patch( "openpeerpower.helpers.config_entry_oauth2_flow.async_get_config_entry_implementation", ), patch( "pyalmond.WebAlmondAPI.async_create_device") as mock_create_device: assert await async_setup_component(opp, "almond", {}) assert entry.state is config_entries.ConfigEntryState.LOADED assert len(mock_create_device.mock_calls) == 0
async def test_form_homekit_and_dhcp_cannot_connect(hass, source, discovery_info): """Test we get the form with homekit and dhcp source.""" await setup.async_setup_component(hass, "persistent_notification", {}) ignored_config_entry = MockConfigEntry(domain=DOMAIN, data={}, source=config_entries.SOURCE_IGNORE) ignored_config_entry.add_to_hass(hass) mock_powerview_userdata = _get_mock_powerview_userdata( get_resources=asyncio.TimeoutError) with patch( "homeassistant.components.hunterdouglas_powerview.UserData", return_value=mock_powerview_userdata, ): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": source}, data=discovery_info, ) assert result["type"] == "abort" assert result["reason"] == "cannot_connect"
async def test_get_conditions_no_state(hass, device_reg, entity_reg): """Test we get the expected conditions from a sensor.""" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) device_entry = device_reg.async_get_or_create( config_entry_id=config_entry.entry_id, connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) entity_ids = {} for device_class in DEVICE_CLASSES: entity_ids[device_class] = entity_reg.async_get_or_create( DOMAIN, "test", f"5678_{device_class}", device_id=device_entry.id, device_class=device_class, unit_of_measurement=UNITS_OF_MEASUREMENT.get(device_class), ).entity_id await hass.async_block_till_done() expected_conditions = [ { "condition": "device", "domain": DOMAIN, "type": condition["type"], "device_id": device_entry.id, "entity_id": entity_ids[device_class], } for device_class in DEVICE_CLASSES if device_class in UNITS_OF_MEASUREMENT for condition in ENTITY_CONDITIONS[device_class] if device_class != "none" ] conditions = await async_get_device_automations(hass, "condition", device_entry.id) assert conditions == expected_conditions
async def test_get_condition_capabilities_none( hass, device_reg, entity_reg, enable_custom_integrations ): """Test we get the expected capabilities from a sensor condition.""" platform = getattr(hass.components, f"test.{DOMAIN}") platform.init() config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) assert await async_setup_component(hass, DOMAIN, {DOMAIN: {CONF_PLATFORM: "test"}}) await hass.async_block_till_done() conditions = [ { "condition": "device", "device_id": "8770c43885354d5fa27604db6817f63f", "domain": "sensor", "entity_id": "sensor.beer", "type": "is_battery_level", }, { "condition": "device", "device_id": "8770c43885354d5fa27604db6817f63f", "domain": "sensor", "entity_id": platform.ENTITIES["none"].entity_id, "type": "is_battery_level", }, ] expected_capabilities = {} for condition in conditions: capabilities = await async_get_device_automation_capabilities( hass, "condition", condition ) assert capabilities == expected_capabilities
async def test_form_ssdp_aborts_if_host_already_exists(hass): """Test we abort if the host is already configured.""" entry = MockConfigEntry( domain=UNIFI_DOMAIN, data={"host": "192.168.208.1", "site": "site_id"}, ) entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( UNIFI_DOMAIN, context={"source": config_entries.SOURCE_SSDP}, data=ssdp.SsdpServiceInfo( ssdp_usn="mock_usn", ssdp_st="mock_st", ssdp_location="http://192.168.208.1:41417/rootDesc.xml", upnp={ "friendlyName": "UniFi Dream Machine", "modelDescription": "UniFi Dream Machine Pro", "serialNumber": "e0:63:da:20:14:a9", }, ), ) assert result["type"] == "abort" assert result["reason"] == "already_configured"
async def test_setup_min(hass, mock_zeroconf): """Test async_setup with min config options.""" entry = MockConfigEntry( domain=DOMAIN, data={CONF_NAME: BRIDGE_NAME, CONF_PORT: DEFAULT_PORT}, options={}, ) entry.add_to_hass(hass) with patch(f"{PATH_HOMEKIT}.HomeKit") as mock_homekit: mock_homekit.return_value = homekit = Mock() type(homekit).async_start = AsyncMock() assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() mock_homekit.assert_any_call( hass, BRIDGE_NAME, DEFAULT_PORT, None, ANY, ANY, {}, HOMEKIT_MODE_BRIDGE, None, entry.entry_id, entry.title, ) assert mock_homekit().setup.called is True # Test auto start enabled mock_homekit.reset_mock() hass.bus.async_fire(EVENT_HOMEASSISTANT_STARTED) await hass.async_block_till_done() mock_homekit().async_start.assert_called()
async def test_get_actions(hass, device_reg, entity_reg): """Test we get the expected actions from a switch.""" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) device_entry = device_reg.async_get_or_create( config_entry_id=config_entry.entry_id, connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id) expected_actions = [ { "domain": DOMAIN, "type": "turn_off", "device_id": device_entry.id, "entity_id": f"{DOMAIN}.test_5678", }, { "domain": DOMAIN, "type": "turn_on", "device_id": device_entry.id, "entity_id": f"{DOMAIN}.test_5678", }, { "domain": DOMAIN, "type": "toggle", "device_id": device_entry.id, "entity_id": f"{DOMAIN}.test_5678", }, ] actions = await async_get_device_automations(hass, "action", device_entry.id) assert actions == expected_actions
async def test_homekit_add_accessory(hass, mock_zeroconf): """Add accessory if config exists and get_acc returns an accessory.""" entry = MockConfigEntry( domain=DOMAIN, data={CONF_NAME: "mock_name", CONF_PORT: 12345} ) entry.add_to_hass(hass) homekit = _mock_homekit(hass, entry, HOMEKIT_MODE_BRIDGE) homekit.driver = "driver" homekit.bridge = mock_bridge = Mock() homekit.bridge.accessories = range(10) homekit.async_start = AsyncMock() with patch(f"{PATH_HOMEKIT}.HomeKit", return_value=homekit): assert await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() mock_acc = Mock(category="any") with patch(f"{PATH_HOMEKIT}.get_accessory") as mock_get_acc: mock_get_acc.side_effect = [None, mock_acc, None] state = State("light.demo", "on") homekit.add_bridge_accessory(state) mock_get_acc.assert_called_with(hass, ANY, ANY, 1403373688, {}) assert not mock_bridge.add_accessory.called state = State("demo.test", "on") homekit.add_bridge_accessory(state) mock_get_acc.assert_called_with(hass, ANY, ANY, 600325356, {}) assert mock_bridge.add_accessory.called state = State("demo.test_2", "on") homekit.add_bridge_accessory(state) mock_get_acc.assert_called_with(hass, ANY, ANY, 1467253281, {}) assert mock_bridge.add_accessory.called
async def test_none(hass, mock_simple_nws): """Test with None as observation and forecast.""" instance = mock_simple_nws.return_value instance.observation = None instance.forecast = None entry = MockConfigEntry( domain=nws.DOMAIN, data=NWS_CONFIG, ) entry.add_to_hass(hass) await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() state = hass.states.get("weather.abc_daynight") assert state assert state.state == STATE_UNKNOWN data = state.attributes for key in EXPECTED_OBSERVATION_IMPERIAL: assert data.get(key) is None forecast = data.get(ATTR_FORECAST) assert forecast is None
async def test_abort_if_already_setup(hass: HomeAssistant, test_api: str) -> None: """Test we abort if the site_id is already setup.""" MockConfigEntry( domain="solaredge", data={ CONF_NAME: DEFAULT_NAME, CONF_SITE_ID: SITE_ID, CONF_API_KEY: API_KEY }, ).add_to_hass(hass) # user: Should fail, same SITE_ID result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data={ CONF_NAME: "test", CONF_SITE_ID: SITE_ID, CONF_API_KEY: "test" }, ) assert result.get("type") == data_entry_flow.FlowResultType.FORM assert result.get("errors") == {CONF_SITE_ID: "already_configured"}
async def test_update_group_unique_id_no_legacy_group_id(hass): """Test migration doesn't trigger without old legacy group id in entry data.""" old_unique_id = "123" new_unique_id = "1234" entry = MockConfigEntry( domain=DECONZ_DOMAIN, unique_id=new_unique_id, data={}, ) registry = er.async_get(hass) # Create entity entry to migrate to new unique ID registry.async_get_or_create( LIGHT_DOMAIN, DECONZ_DOMAIN, f"{old_unique_id}-OLD", suggested_object_id="old", config_entry=entry, ) await async_update_group_unique_id(hass, entry) assert registry.async_get( f"{LIGHT_DOMAIN}.old").unique_id == f"{old_unique_id}-OLD"
async def test_form_host_already_exists(hass: HomeAssistant) -> None: """Test host already exists.""" config_entry = MockConfigEntry( domain=DOMAIN, data={ "host": "1.1.1.1", "name": "Envoy", "username": "******", "password": "******", }, title="Envoy", ) config_entry.add_to_hass(hass) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER} ) assert result["type"] == "form" assert result["errors"] == {} with patch( "homeassistant.components.enphase_envoy.config_flow.EnvoyReader.getData", return_value=True, ): result2 = await hass.config_entries.flow.async_configure( result["flow_id"], { "host": "1.1.1.1", "username": "******", "password": "******", }, ) await hass.async_block_till_done() assert result2["type"] == "abort" assert result2["reason"] == "already_configured"
async def _setup( hass: HomeAssistant, sensors: list[str], config: dict[str, Any] ) -> State: """Set up entry and return entity state.""" with patch( "homeassistant.util.dt.utcnow", return_value=datetime(2021, 3, 6, 23, 59, 59, tzinfo=dt_util.UTC), ): data = _get_config_schema(hass, SOURCE_USER)(config) data[CONF_NAME] = DEFAULT_NAME config_entry = MockConfigEntry( domain=DOMAIN, data=data, options={CONF_TIMESTEP: DEFAULT_TIMESTEP}, unique_id=_get_unique_id(hass, data), version=1, ) config_entry.add_to_hass(hass) assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() for entity_name in sensors: _enable_entity(hass, CC_SENSOR_ENTITY_ID.format(entity_name)) await hass.async_block_till_done() assert len(hass.states.async_entity_ids(SENSOR_DOMAIN)) == len(sensors)
async def test_options_flow_incomplete(hass): """Test specifying incomplete settings using options flow.""" config_entry = MockConfigEntry( domain=const.DOMAIN, unique_id=TEST_GATEWAY_ID, data={ const.CONF_CLOUD_USERNAME: None, const.CONF_CLOUD_PASSWORD: None, const.CONF_CLOUD_COUNTRY: None, const.CONF_FLOW_TYPE: const.CONF_GATEWAY, CONF_HOST: TEST_HOST, CONF_TOKEN: TEST_TOKEN, const.CONF_MODEL: TEST_MODEL, const.CONF_MAC: TEST_MAC, }, title=TEST_NAME, ) config_entry.add_to_hass(hass) assert await hass.config_entries.async_setup(config_entry.entry_id) await hass.async_block_till_done() result = await hass.config_entries.options.async_init(config_entry.entry_id) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" result = await hass.config_entries.options.async_configure( result["flow_id"], user_input={ const.CONF_CLOUD_SUBDEVICES: True, }, ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "init" assert result["errors"] == {"base": "cloud_credentials_incomplete"}
async def test_system_health(opp, aioclient_mock): """Test system health.""" aioclient_mock.get(f"http://{MOCK_HOSTNAME}{ISY_URL_POSTFIX}", text="") opp.config.components.add(DOMAIN) assert await async_setup_component(opp, "system_health", {}) MockConfigEntry( domain=DOMAIN, entry_id=MOCK_ENTRY_ID, data={ CONF_HOST: f"http://{MOCK_HOSTNAME}" }, unique_id=MOCK_UUID, ).add_to_opp(opp) opp.data[DOMAIN] = {} opp.data[DOMAIN][MOCK_ENTRY_ID] = {} opp.data[DOMAIN][MOCK_ENTRY_ID][ISY994_ISY] = Mock( connected=True, websocket=Mock( last_heartbeat=MOCK_HEARTBEAT, status=MOCK_CONNECTED, ), ) info = await get_system_health_info(opp, DOMAIN) for key, val in info.items(): if asyncio.iscoroutine(val): info[key] = await val assert info["host_reachable"] == "ok" assert info["device_connected"] assert info["last_heartbeat"] == MOCK_HEARTBEAT assert info["websocket_status"] == MOCK_CONNECTED
async def test_get_conditions(hass, device_reg, entity_reg): """Test we get the expected conditions from a climate.""" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_hass(hass) device_entry = device_reg.async_get_or_create( config_entry_id=config_entry.entry_id, connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id) hass.states.async_set( f"{DOMAIN}.test_5678", const.HVAC_MODE_COOL, { const.ATTR_HVAC_MODE: const.HVAC_MODE_COOL, const.ATTR_PRESET_MODE: const.PRESET_AWAY, const.ATTR_PRESET_MODES: [const.PRESET_HOME, const.PRESET_AWAY], }, ) expected_conditions = [ { "condition": "device", "domain": DOMAIN, "type": "is_hvac_mode", "device_id": device_entry.id, "entity_id": f"{DOMAIN}.test_5678", }, { "condition": "device", "domain": DOMAIN, "type": "is_preset_mode", "device_id": device_entry.id, "entity_id": f"{DOMAIN}.test_5678", }, ] conditions = await async_get_device_automations(hass, "condition", device_entry.id) assert_lists_same(conditions, expected_conditions)
async def test_get_trigger_capabilities(opp, device_reg, entity_reg): """Test we get the expected capabilities from a device_tracker trigger.""" config_entry = MockConfigEntry(domain="test", data={}) config_entry.add_to_opp(opp) device_entry = device_reg.async_get_or_create( config_entry_id=config_entry.entry_id, connections={(device_registry.CONNECTION_NETWORK_MAC, "12:34:56:AB:CD:EF")}, ) entity_reg.async_get_or_create(DOMAIN, "test", "5678", device_id=device_entry.id) capabilities = await device_trigger.async_get_trigger_capabilities( opp, { "platform": "device", "domain": DOMAIN, "type": "enters", "device_id": device_entry.id, "entity_id": f"{DOMAIN}.test_5678", }, ) assert capabilities and "extra_fields" in capabilities assert voluptuous_serialize.convert( capabilities["extra_fields"], custom_serializer=cv.custom_serializer) == [{ "name": "zone", "required": True, "type": "select", "options": [("zone.test", "test"), ("zone.home", "test home")], }]
async def test_setup_raise_not_ready(hass, caplog): """Test a setup raising not ready.""" entry = MockConfigEntry(domain='test') mock_setup_entry = MagicMock(side_effect=ConfigEntryNotReady) loader.set_component( hass, 'test', MockModule('test', async_setup_entry=mock_setup_entry)) with patch('homeassistant.helpers.event.async_call_later') as mock_call: await entry.async_setup(hass) assert len(mock_call.mock_calls) == 1 assert 'Config entry for test not ready yet' in caplog.text p_hass, p_wait_time, p_setup = mock_call.mock_calls[0][1] assert p_hass is hass assert p_wait_time == 5 assert entry.state == config_entries.ENTRY_STATE_SETUP_RETRY mock_setup_entry.side_effect = None mock_setup_entry.return_value = mock_coro(True) await p_setup(None) assert entry.state == config_entries.ENTRY_STATE_LOADED
async def test_dimmer_turn_on_fix(hass: HomeAssistant) -> None: """Test a light.""" already_migrated_config_entry = MockConfigEntry( domain=DOMAIN, data={}, unique_id=MAC_ADDRESS ) already_migrated_config_entry.add_to_hass(hass) bulb = _mocked_bulb() bulb.is_dimmer = True bulb.is_on = False with _patch_discovery(device=bulb), _patch_single_discovery(device=bulb): await async_setup_component(hass, tplink.DOMAIN, {tplink.DOMAIN: {}}) await hass.async_block_till_done() entity_id = "light.my_bulb" state = hass.states.get(entity_id) assert state.state == "off" await hass.services.async_call( LIGHT_DOMAIN, "turn_on", {ATTR_ENTITY_ID: entity_id}, blocking=True ) bulb.turn_on.assert_called_once_with(transition=1) bulb.turn_on.reset_mock()
async def test_setup_unload_entry(hass): """Test if config entry is unloaded.""" mock_entry = MockConfigEntry(domain=DOMAIN, unique_id=MOCK_CONFIG_DATA[CONF_HOST], data=MOCK_CONFIG_DATA) mock_entry.add_to_hass(hass) mock_remote = get_mock_remote() with patch( "homeassistant.components.panasonic_viera.Remote", return_value=mock_remote, ): await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() await hass.config_entries.async_unload(mock_entry.entry_id) assert mock_entry.state == ENTRY_STATE_NOT_LOADED state = hass.states.get("media_player.panasonic_viera_tv") assert state is None