async def test_entity_and_device_attributes(opp, device_factory): """Test the attributes of the entity are correct.""" # Arrange device = device_factory( "Fan 1", capabilities=[Capability.switch, Capability.fan_speed], status={ Attribute.switch: "on", Attribute.fan_speed: 2 }, ) # Act await setup_platform(opp, FAN_DOMAIN, devices=[device]) entity_registry = er.async_get(opp) device_registry = dr.async_get(opp) # Assert entry = entity_registry.async_get("fan.fan_1") assert entry assert entry.unique_id == device.device_id entry = device_registry.async_get_device({(DOMAIN, device.device_id)}) assert entry assert entry.name == device.label assert entry.model == device.device_type_name assert entry.manufacturer == "Unavailable"
async def test_exception_no_triggers(opp, mock_devices, calls, caplog): """Test for exception on event triggers firing.""" _, zha_device = mock_devices ieee_address = str(zha_device.ieee) ha_device_registry = dr.async_get(opp) reg_device = ha_device_registry.async_get_device({("zha", ieee_address)}) await async_setup_component( opp, automation.DOMAIN, { automation.DOMAIN: [{ "trigger": { "device_id": reg_device.id, "domain": "zha", "platform": "device", "type": "junk", "subtype": "junk", }, "action": { "service": "test.automation", "data": { "message": "service called" }, }, }] }, ) await opp.async_block_till_done() assert "Invalid config for [automation]" in caplog.text
async def test_services(opp): """Test service calls.""" client_mock = await init_integration(opp) device_registry = dr.async_get(opp) reg_device = device_registry.async_get_device(identifiers={ (DOMAIN, "JM000000000000000") }, ) device_id = reg_device.id for service in SERVICES: service_data = {"device_id": device_id} if service == "send_poi": service_data["latitude"] = 1.2345 service_data["longitude"] = 2.3456 service_data["poi_name"] = "Work" await opp.services.async_call(DOMAIN, service, service_data, blocking=True) await opp.async_block_till_done() api_method = getattr(client_mock, service) if service == "send_poi": api_method.assert_called_once_with(12345, 1.2345, 2.3456, "Work") else: api_method.assert_called_once_with(12345)
async def test_simpleconnect_cover_setup(opp): """Test that a velux gateway can be correctly setup in HA.""" accessories = await setup_accessories_from_file(opp, "velux_gateway.json") config_entry, pairing = await setup_test_accessories(opp, accessories) entity_registry = er.async_get(opp) # Check that the cover is correctly found and set up cover_id = "cover.velux_window" cover = entity_registry.async_get(cover_id) assert cover.unique_id == "homekit-1111111a114a111a-8" cover_helper = Helper( opp, cover_id, pairing, accessories[0], config_entry, ) cover_state = await cover_helper.poll_and_get_state() assert cover_state.attributes["friendly_name"] == "VELUX Window" assert cover_state.state == "closed" assert cover_state.attributes["supported_features"] == ( SUPPORT_CLOSE | SUPPORT_SET_POSITION | SUPPORT_OPEN ) # Check that one of the sensors is correctly found and set up sensor_id = "sensor.velux_sensor_temperature" sensor = entity_registry.async_get(sensor_id) assert sensor.unique_id == "homekit-a11b111-8" sensor_helper = Helper( opp, sensor_id, pairing, accessories[0], config_entry, ) sensor_state = await sensor_helper.poll_and_get_state() assert sensor_state.attributes["friendly_name"] == "VELUX Sensor Temperature" assert sensor_state.state == "18.9" # The cover and sensor are different devices (accessories) attached to the same bridge assert cover.device_id != sensor.device_id device_registry = dr.async_get(opp) device = device_registry.async_get(cover.device_id) assert device.manufacturer == "VELUX" assert device.name == "VELUX Window" assert device.model == "VELUX Window" assert device.sw_version == "48" bridge = device_registry.async_get(device.via_device_id) assert bridge.manufacturer == "VELUX" assert bridge.name == "VELUX Gateway" assert bridge.model == "VELUX Gateway" assert bridge.sw_version == "70"
async def async_configure_accessories(self): """Configure accessories for the included states.""" dev_reg = device_registry.async_get(self.opp) ent_reg = entity_registry.async_get(self.opp) device_lookup = ent_reg.async_get_device_class_lookup({ (BINARY_SENSOR_DOMAIN, DEVICE_CLASS_BATTERY_CHARGING), (BINARY_SENSOR_DOMAIN, DEVICE_CLASS_MOTION), (BINARY_SENSOR_DOMAIN, DEVICE_CLASS_OCCUPANCY), (SENSOR_DOMAIN, DEVICE_CLASS_BATTERY), (SENSOR_DOMAIN, DEVICE_CLASS_HUMIDITY), }) entity_states = [] for state in self.opp.states.async_all(): entity_id = state.entity_id if not self._filter(entity_id): continue ent_reg_ent = ent_reg.async_get(entity_id) if ent_reg_ent: await self._async_set_device_info_attributes( ent_reg_ent, dev_reg, entity_id) self._async_configure_linked_sensors(ent_reg_ent, device_lookup, state) entity_states.append(state) return entity_states
async def test_init_gatebox(gatebox, opp, config): """Test cover default state.""" _, entity_id = gatebox entry = await async_setup_entity(opp, config, entity_id) assert entry.unique_id == "BleBox-gateBox-1afe34db9437-position" state = opp.states.get(entity_id) assert state.name == "gateBox-position" assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_DOOR supported_features = state.attributes[ATTR_SUPPORTED_FEATURES] assert supported_features & SUPPORT_OPEN assert supported_features & SUPPORT_CLOSE # Not available during init since requires fetching state to detect assert not supported_features & SUPPORT_STOP assert not supported_features & SUPPORT_SET_POSITION assert ATTR_CURRENT_POSITION not in state.attributes assert state.state == STATE_UNKNOWN device_registry = dr.async_get(opp) device = device_registry.async_get(entry.device_id) assert device.name == "My gatebox" assert device.identifiers == {("blebox", "abcd0123ef5678")} assert device.manufacturer == "BleBox" assert device.model == "gateBox" assert device.sw_version == "1.23"
async def test_state(opp): """Test state of the entity.""" mocked_device = _create_mocked_device() entry = MockConfigEntry(domain=songpal.DOMAIN, data=CONF_DATA) entry.add_to_opp(opp) with _patch_media_player_device(mocked_device): await opp.config_entries.async_setup(entry.entry_id) await opp.async_block_till_done() state = opp.states.get(ENTITY_ID) assert state.name == FRIENDLY_NAME assert state.state == STATE_ON attributes = state.as_dict()["attributes"] assert attributes["volume_level"] == 0.5 assert attributes["is_volume_muted"] is False assert attributes["source_list"] == ["title1", "title2"] assert attributes["source"] == "title2" assert attributes["supported_features"] == SUPPORT_SONGPAL device_registry = dr.async_get(opp) device = device_registry.async_get_device(identifiers={(songpal.DOMAIN, MAC)}) assert device.connections == {(dr.CONNECTION_NETWORK_MAC, MAC)} assert device.manufacturer == "Sony Corporation" assert device.name == FRIENDLY_NAME assert device.sw_version == SW_VERSION assert device.model == MODEL entity_registry = er.async_get(opp) entity = entity_registry.async_get(ENTITY_ID) assert entity.unique_id == MAC
async def help_test_entity_device_info_update(opp, mqtt_mock, domain, config): """Test device registry update. This is a test helper for the MqttDiscoveryUpdate mixin. """ # Add device settings to config config = copy.deepcopy(config[domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" registry = dr.async_get(opp) data = json.dumps(config) async_fire_mqtt_message(opp, f"openpeerpower/{domain}/bla/config", data) await opp.async_block_till_done() device = registry.async_get_device({("mqtt", "helloworld")}) assert device is not None assert device.name == "Beer" config["device"]["name"] = "Milk" data = json.dumps(config) async_fire_mqtt_message(opp, f"openpeerpower/{domain}/bla/config", data) await opp.async_block_till_done() device = registry.async_get_device({("mqtt", "helloworld")}) assert device is not None assert device.name == "Milk"
async def help_test_entity_debug_info(opp, mqtt_mock, domain, config): """Test debug_info. This is a test helper for MQTT debug_info. """ # Add device settings to config config = copy.deepcopy(config[domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" registry = dr.async_get(opp) data = json.dumps(config) async_fire_mqtt_message(opp, f"openpeerpower/{domain}/bla/config", data) await opp.async_block_till_done() device = registry.async_get_device({("mqtt", "helloworld")}) assert device is not None debug_info_data = await debug_info.info_for_device(opp, device.id) assert len(debug_info_data["entities"]) == 1 assert (debug_info_data["entities"][0]["discovery_data"]["topic"] == f"openpeerpower/{domain}/bla/config") assert debug_info_data["entities"][0]["discovery_data"][ "payload"] == config assert len(debug_info_data["entities"][0]["subscriptions"]) == 1 assert { "topic": "test-topic", "messages": [] } in debug_info_data["entities"][0]["subscriptions"] assert len(debug_info_data["triggers"]) == 0
async def help_test_entity_device_info_with_connection(opp, mqtt_mock, domain, config): """Test device registry integration. This is a test helper for the MqttDiscoveryUpdate mixin. """ # Add device settings to config config = copy.deepcopy(config[domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_MAC) config["unique_id"] = "veryunique" registry = dr.async_get(opp) data = json.dumps(config) async_fire_mqtt_message(opp, f"openpeerpower/{domain}/bla/config", data) await opp.async_block_till_done() device = registry.async_get_device( set(), {(dr.CONNECTION_NETWORK_MAC, "02:5b:26:a8:dc:12")}) assert device is not None assert device.connections == {(dr.CONNECTION_NETWORK_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" assert device.suggested_area == "default_area"
async def help_test_entity_device_info_remove(opp, mqtt_mock, domain, config): """Test device registry remove.""" # Add device settings to config config = copy.deepcopy(config[domain]) config["device"] = copy.deepcopy(DEFAULT_CONFIG_DEVICE_INFO_ID) config["unique_id"] = "veryunique" dev_registry = dr.async_get(opp) ent_registry = er.async_get(opp) data = json.dumps(config) async_fire_mqtt_message(opp, f"openpeerpower/{domain}/bla/config", data) await opp.async_block_till_done() device = dev_registry.async_get_device({("mqtt", "helloworld")}) assert device is not None assert ent_registry.async_get_entity_id(domain, mqtt.DOMAIN, "veryunique") async_fire_mqtt_message(opp, f"openpeerpower/{domain}/bla/config", "") await opp.async_block_till_done() device = dev_registry.async_get_device({("mqtt", "helloworld")}) assert device is None assert not ent_registry.async_get_entity_id(domain, mqtt.DOMAIN, "veryunique")
async def test_removed_device(opp, client, multiple_devices, integration): """Test that the device registry gets updated when a device gets removed.""" nodes = multiple_devices # Verify how many nodes are available assert len(client.driver.controller.nodes) == 2 # Make sure there are the same number of devices dev_reg = dr.async_get(opp) device_entries = dr.async_entries_for_config_entry(dev_reg, integration.entry_id) assert len(device_entries) == 2 # Check how many entities there are ent_reg = er.async_get(opp) entity_entries = er.async_entries_for_config_entry(ent_reg, integration.entry_id) assert len(entity_entries) == 24 # Remove a node and reload the entry old_node = nodes.pop(13) await opp.config_entries.async_reload(integration.entry_id) await opp.async_block_till_done() # Assert that the node and all of it's entities were removed from the device and # entity registry device_entries = dr.async_entries_for_config_entry(dev_reg, integration.entry_id) assert len(device_entries) == 1 entity_entries = er.async_entries_for_config_entry(ent_reg, integration.entry_id) assert len(entity_entries) == 15 assert dev_reg.async_get_device({get_device_id(client, old_node)}) is None
async def test_existing_node_not_ready(opp, client, multisensor_6): """Test we handle a non ready node that exists during integration setup.""" dev_reg = dr.async_get(opp) node = multisensor_6 node.data = deepcopy(node.data) # Copy to allow modification in tests. node.data["ready"] = False event = {"node": node} air_temperature_device_id = f"{client.driver.controller.home_id}-{node.node_id}" entry = MockConfigEntry(domain="zwave_js", data={"url": "ws://test.org"}) entry.add_to_opp(opp) await opp.config_entries.async_setup(entry.entry_id) await opp.async_block_till_done() state = opp.states.get(AIR_TEMPERATURE_SENSOR) assert not state # entity not yet added assert dev_reg.async_get_device( # device should be added identifiers={(DOMAIN, air_temperature_device_id)}) node.data["ready"] = True node.emit("ready", event) await opp.async_block_till_done() state = opp.states.get(AIR_TEMPERATURE_SENSOR) assert state # entity and device added assert state.state != STATE_UNAVAILABLE assert dev_reg.async_get_device(identifiers={(DOMAIN, air_temperature_device_id)})
async def test_on_node_added_not_ready(opp, multisensor_6_state, client, integration): """Test we handle a non ready node added event.""" dev_reg = dr.async_get(opp) node_data = deepcopy( multisensor_6_state) # Copy to allow modification in tests. node = Node(client, node_data) node.data["ready"] = False event = {"node": node} air_temperature_device_id = f"{client.driver.controller.home_id}-{node.node_id}" state = opp.states.get(AIR_TEMPERATURE_SENSOR) assert not state # entity and device not yet added assert not dev_reg.async_get_device( identifiers={(DOMAIN, air_temperature_device_id)}) client.driver.controller.emit("node added", event) await opp.async_block_till_done() state = opp.states.get(AIR_TEMPERATURE_SENSOR) assert not state # entity not yet added but device added in registry assert dev_reg.async_get_device(identifiers={(DOMAIN, air_temperature_device_id)}) node.data["ready"] = True node.emit("ready", event) await opp.async_block_till_done() state = opp.states.get(AIR_TEMPERATURE_SENSOR) assert state # entity added assert state.state != STATE_UNAVAILABLE
async def test_hmip_multi_area_device(opp, default_mock_hap_factory): """Test multi area device. Check if devices are created and referenced.""" entity_id = "binary_sensor.wired_eingangsmodul_32_fach_channel5" entity_name = "Wired Eingangsmodul – 32-fach Channel5" device_model = "HmIPW-DRI32" mock_hap = await default_mock_hap_factory.async_get_mock_hap( test_devices=["Wired Eingangsmodul – 32-fach"] ) ha_state, hmip_device = get_and_check_entity_basics( opp, mock_hap, entity_id, entity_name, device_model ) assert ha_state # get the entity entity_registry = er.async_get(opp) entity = entity_registry.async_get(ha_state.entity_id) assert entity # get the device device_registry = dr.async_get(opp) device = device_registry.async_get(entity.device_id) assert device.name == "Wired Eingangsmodul – 32-fach" # get the hap hap_device = device_registry.async_get(device.via_device_id) assert hap_device.name == "Home"
async def test_init(saunabox, opp, config): """Test default state.""" _, entity_id = saunabox entry = await async_setup_entity(opp, config, entity_id) assert entry.unique_id == "BleBox-saunaBox-1afe34db9437-thermostat" state = opp.states.get(entity_id) assert state.name == "saunaBox-thermostat" supported_features = state.attributes[ATTR_SUPPORTED_FEATURES] assert supported_features & SUPPORT_TARGET_TEMPERATURE assert state.attributes[ATTR_HVAC_MODES] == [HVAC_MODE_OFF, HVAC_MODE_HEAT] assert ATTR_DEVICE_CLASS not in state.attributes assert ATTR_HVAC_MODE not in state.attributes assert ATTR_HVAC_ACTION not in state.attributes assert state.attributes[ATTR_MIN_TEMP] == -54.3 assert state.attributes[ATTR_MAX_TEMP] == 124.3 assert state.attributes[ATTR_TEMPERATURE] is None assert state.attributes[ATTR_CURRENT_TEMPERATURE] is None assert state.state == STATE_UNKNOWN device_registry = dr.async_get(opp) device = device_registry.async_get(entry.device_id) assert device.name == "My sauna" assert device.identifiers == {("blebox", "abcd0123ef5678")} assert device.manufacturer == "BleBox" assert device.model == "saunaBox" assert device.sw_version == "1.23"
async def test_hmip_remove_device(opp, default_mock_hap_factory): """Test Remove of hmip device.""" entity_id = "light.treppe_ch" entity_name = "Treppe CH" device_model = "HmIP-BSL" mock_hap = await default_mock_hap_factory.async_get_mock_hap( test_devices=["Treppe"] ) ha_state, hmip_device = get_and_check_entity_basics( opp, mock_hap, entity_id, entity_name, device_model ) assert ha_state.state == STATE_ON assert hmip_device device_registry = dr.async_get(opp) entity_registry = er.async_get(opp) pre_device_count = len(device_registry.devices) pre_entity_count = len(entity_registry.entities) pre_mapping_count = len(mock_hap.hmip_device_by_entity_id) hmip_device.fire_remove_event() await opp.async_block_till_done() assert len(device_registry.devices) == pre_device_count - 1 assert len(entity_registry.entities) == pre_entity_count - 3 assert len(mock_hap.hmip_device_by_entity_id) == pre_mapping_count - 3
async def test_init(airsensor, opp, config): """Test airSensor default state.""" _, entity_id = airsensor entry = await async_setup_entity(opp, config, entity_id) assert entry.unique_id == "BleBox-airSensor-1afe34db9437-0.air" state = opp.states.get(entity_id) assert state.name == "airSensor-0.air" assert ATTR_PM_0_1 not in state.attributes assert ATTR_PM_2_5 not in state.attributes assert ATTR_PM_10 not in state.attributes assert state.attributes[ATTR_ICON] == "mdi:blur" assert state.state == STATE_UNKNOWN device_registry = dr.async_get(opp) device = device_registry.async_get(entry.device_id) assert device.name == "My air sensor" assert device.identifiers == {("blebox", "abcd0123ef5678")} assert device.manufacturer == "BleBox" assert device.model == "airSensor" assert device.sw_version == "1.23"
async def test_init_gatecontroller(gatecontroller, opp, config): """Test gateController default state.""" _, entity_id = gatecontroller entry = await async_setup_entity(opp, config, entity_id) assert entry.unique_id == "BleBox-gateController-2bee34e750b8-position" state = opp.states.get(entity_id) assert state.name == "gateController-position" assert state.attributes[ATTR_DEVICE_CLASS] == DEVICE_CLASS_GATE supported_features = state.attributes[ATTR_SUPPORTED_FEATURES] assert supported_features & SUPPORT_OPEN assert supported_features & SUPPORT_CLOSE assert supported_features & SUPPORT_STOP assert supported_features & SUPPORT_SET_POSITION assert ATTR_CURRENT_POSITION not in state.attributes assert state.state == STATE_UNKNOWN device_registry = dr.async_get(opp) device = device_registry.async_get(entry.device_id) assert device.name == "My gate controller" assert device.identifiers == {("blebox", "abcd0123ef5678")} assert device.manufacturer == "BleBox" assert device.model == "gateController" assert device.sw_version == "1.23"
async def test_entity_device_info_with_hub(opp, mqtt_mock): """Test MQTT sensor device registry integration.""" registry = dr.async_get(opp) hub = registry.async_get_or_create( config_entry_id="123", connections=set(), identifiers={("mqtt", "hub-id")}, manufacturer="manufacturer", model="hub", ) data = json.dumps({ "platform": "mqtt", "name": "Test 1", "state_topic": "test-topic", "device": { "identifiers": ["helloworld"], "via_device": "hub-id" }, "unique_id": "veryunique", }) async_fire_mqtt_message(opp, "openpeerpower/sensor/bla/config", data) await opp.async_block_till_done() device = registry.async_get_device({("mqtt", "helloworld")}) assert device is not None assert device.via_device_id == hub.id
async def test_device_info(opp: OpenPeerPower) -> None: """Verify device information includes expected details.""" client = create_mock_client() register_test_entity( opp, LIGHT_DOMAIN, TYPE_HYPERION_PRIORITY_LIGHT, TEST_PRIORITY_LIGHT_ENTITY_ID_1, ) await setup_test_config_entry(opp, hyperion_client=client) device_id = get_hyperion_device_id(TEST_SYSINFO_ID, TEST_INSTANCE) device_registry = dr.async_get(opp) device = device_registry.async_get_device({(DOMAIN, device_id)}) assert device assert device.config_entries == {TEST_CONFIG_ENTRY_ID} assert device.identifiers == {(DOMAIN, device_id)} assert device.manufacturer == HYPERION_MANUFACTURER_NAME assert device.model == HYPERION_MODEL_NAME assert device.name == TEST_INSTANCE_1["friendly_name"] entity_registry = await er.async_get_registry(opp) entities_from_device = [ entry.entity_id for entry in er.async_entries_for_device(entity_registry, device.id) ] assert TEST_PRIORITY_LIGHT_ENTITY_ID_1 in entities_from_device assert TEST_ENTITY_ID_1 in entities_from_device
async def test_lennox_e30_setup(opp): """Test that a Lennox E30 can be correctly setup in HA.""" accessories = await setup_accessories_from_file(opp, "lennox_e30.json") config_entry, pairing = await setup_test_accessories(opp, accessories) entity_registry = er.async_get(opp) climate = entity_registry.async_get("climate.lennox") assert climate.unique_id == "homekit-XXXXXXXX-100" climate_helper = Helper(opp, "climate.lennox", pairing, accessories[0], config_entry) climate_state = await climate_helper.poll_and_get_state() assert climate_state.attributes["friendly_name"] == "Lennox" assert climate_state.attributes["supported_features"] == ( SUPPORT_TARGET_TEMPERATURE | SUPPORT_TARGET_TEMPERATURE_RANGE) device_registry = dr.async_get(opp) device = device_registry.async_get(climate.device_id) assert device.manufacturer == "Lennox" assert device.name == "Lennox" assert device.model == "E30 2B" assert device.sw_version == "3.40.XX" # The fixture contains a single accessory - so its a single device # and no bridge assert device.via_device_id is None
async def test_simpleconnect_fan_setup(opp): """Test that a SIMPLEconnect fan can be correctly setup in HA.""" accessories = await setup_accessories_from_file(opp, "simpleconnect_fan.json") config_entry, pairing = await setup_test_accessories(opp, accessories) entity_registry = er.async_get(opp) # Check that the fan is correctly found and set up fan_id = "fan.simpleconnect_fan_06f674" fan = entity_registry.async_get(fan_id) assert fan.unique_id == "homekit-1234567890abcd-8" fan_helper = Helper( opp, "fan.simpleconnect_fan_06f674", pairing, accessories[0], config_entry, ) fan_state = await fan_helper.poll_and_get_state() assert fan_state.attributes["friendly_name"] == "SIMPLEconnect Fan-06F674" assert fan_state.state == "off" assert fan_state.attributes["supported_features"] == (SUPPORT_DIRECTION | SUPPORT_SET_SPEED) device_registry = dr.async_get(opp) device = device_registry.async_get(fan.device_id) assert device.manufacturer == "Hunter Fan" assert device.name == "SIMPLEconnect Fan-06F674" assert device.model == "SIMPLEconnect" assert device.sw_version == "" assert device.via_device_id is None
async def test_koogeek_ls1_setup(opp): """Test that a Koogeek LS1 can be correctly setup in HA.""" accessories = await setup_accessories_from_file(opp, "koogeek_ls1.json") config_entry, pairing = await setup_test_accessories(opp, accessories) entity_registry = er.async_get(opp) # Assert that the entity is correctly added to the entity registry entry = entity_registry.async_get("light.koogeek_ls1_20833f") assert entry.unique_id == "homekit-AAAA011111111111-7" helper = Helper(opp, "light.koogeek_ls1_20833f", pairing, accessories[0], config_entry) state = await helper.poll_and_get_state() # Assert that the friendly name is detected correctly assert state.attributes["friendly_name"] == "Koogeek-LS1-20833F" # Assert that all optional features the LS1 supports are detected assert state.attributes["supported_features"] == (SUPPORT_BRIGHTNESS | SUPPORT_COLOR) device_registry = dr.async_get(opp) device = device_registry.async_get(entry.device_id) assert device.manufacturer == "Koogeek" assert device.name == "Koogeek-LS1-20833F" assert device.model == "LS1" assert device.sw_version == "2.2.15" assert device.via_device_id is None
def _async_register_bridge(self): """Register the bridge as a device so homekit_controller and exclude it from discovery.""" dev_reg = device_registry.async_get(self.opp) formatted_mac = device_registry.format_mac(self.driver.state.mac) # Connections and identifiers are both used here. # # connections exists so homekit_controller can know the # virtual mac address of the bridge and know to not offer # it via discovery. # # identifiers is used as well since the virtual mac may change # because it will not survive manual pairing resets (deleting state file) # which we have trained users to do over the past few years # because this was the way you had to fix homekit when pairing # failed. # connection = (device_registry.CONNECTION_NETWORK_MAC, formatted_mac) identifier = (DOMAIN, self._entry_id, BRIDGE_SERIAL_NUMBER) self._async_purge_old_bridges(dev_reg, identifier, connection) is_accessory_mode = self._homekit_mode == HOMEKIT_MODE_ACCESSORY hk_mode_name = "Accessory" if is_accessory_mode else "Bridge" dev_reg.async_get_or_create( config_entry_id=self._entry_id, identifiers={identifier}, connections={connection}, manufacturer=MANUFACTURER, name=accessory_friendly_name(self._entry_title, self.driver.accessory), model=f"HomeKit {hk_mode_name}", entry_type="service", )
async def test_ws_api(opp, opp_ws_client): """Test WS API.""" assert await async_setup_component(opp, "search", {}) area_reg = ar.async_get(opp) device_reg = dr.async_get(opp) kitchen_area = area_reg.async_create("Kitchen") hue_config_entry = MockConfigEntry(domain="hue") hue_config_entry.add_to_opp(opp) hue_device = device_reg.async_get_or_create( config_entry_id=hue_config_entry.entry_id, name="Light Strip", identifiers=({"hue", "hue-1"}), ) device_reg.async_update_device(hue_device.id, area_id=kitchen_area.id) client = await opp_ws_client(opp) await client.send_json({ "id": 1, "type": "search/related", "item_type": "device", "item_id": hue_device.id, }) response = await client.receive_json() assert response["success"] assert response["result"] == { "config_entry": [hue_config_entry.entry_id], "area": [kitchen_area.id], }
async def _create_entries( opp: OpenPeerPower, client=None) -> tuple[RegistryEntry, DeviceEntry, ClientMock]: client = ClientMock() if client is None else client def get_client_mock(client, _): return client with patch("twinkly_client.TwinklyClient", side_effect=get_client_mock): config_entry = MockConfigEntry( domain=TWINKLY_DOMAIN, data={ CONF_ENTRY_HOST: client, CONF_ENTRY_ID: client.id, CONF_ENTRY_NAME: TEST_NAME_ORIGINAL, CONF_ENTRY_MODEL: TEST_MODEL, }, entry_id=client.id, ) config_entry.add_to_opp(opp) assert await opp.config_entries.async_setup(client.id) await opp.async_block_till_done() device_registry = dr.async_get(opp) entity_registry = er.async_get(opp) entity_id = entity_registry.async_get_entity_id("light", TWINKLY_DOMAIN, client.id) entity = entity_registry.async_get(entity_id) device = device_registry.async_get_device({(TWINKLY_DOMAIN, client.id)}) assert entity is not None assert device is not None return entity, device, client
async def test_hmip_remove_group(opp, default_mock_hap_factory): """Test Remove of hmip group.""" entity_id = "switch.strom_group" entity_name = "Strom Group" device_model = None mock_hap = await default_mock_hap_factory.async_get_mock_hap(test_groups=["Strom"]) ha_state, hmip_device = get_and_check_entity_basics( opp, mock_hap, entity_id, entity_name, device_model ) assert ha_state.state == STATE_ON assert hmip_device device_registry = dr.async_get(opp) entity_registry = er.async_get(opp) pre_device_count = len(device_registry.devices) pre_entity_count = len(entity_registry.entities) pre_mapping_count = len(mock_hap.hmip_device_by_entity_id) hmip_device.fire_remove_event() await opp.async_block_till_done() assert len(device_registry.devices) == pre_device_count assert len(entity_registry.entities) == pre_entity_count - 1 assert len(mock_hap.hmip_device_by_entity_id) == pre_mapping_count - 1
async def test_enter_and_exit(opp, gpslogger_client, webhook_id): """Test when there is a known zone.""" url = f"/api/webhook/{webhook_id}" data = {"latitude": HOME_LATITUDE, "longitude": HOME_LONGITUDE, "device": "123"} # Enter the Home req = await gpslogger_client.post(url, data=data) await opp.async_block_till_done() assert req.status == HTTP_OK state_name = opp.states.get(f"{DEVICE_TRACKER_DOMAIN}.{data['device']}").state assert state_name == STATE_HOME # Enter Home again req = await gpslogger_client.post(url, data=data) await opp.async_block_till_done() assert req.status == HTTP_OK state_name = opp.states.get(f"{DEVICE_TRACKER_DOMAIN}.{data['device']}").state assert state_name == STATE_HOME data["longitude"] = 0 data["latitude"] = 0 # Enter Somewhere else req = await gpslogger_client.post(url, data=data) await opp.async_block_till_done() assert req.status == HTTP_OK state_name = opp.states.get(f"{DEVICE_TRACKER_DOMAIN}.{data['device']}").state assert state_name == STATE_NOT_HOME dev_reg = dr.async_get(opp) assert len(dev_reg.devices) == 1 ent_reg = er.async_get(opp) assert len(ent_reg.entities) == 1
async def test_enumerate_remote(opp, utcnow): """Test that remote is correctly enumerated.""" await setup_test_component(opp, create_remote) entity_registry = er.async_get(opp) entry = entity_registry.async_get("sensor.testdevice_battery") device_registry = dr.async_get(opp) device = device_registry.async_get(entry.device_id) expected = [ { "device_id": device.id, "domain": "sensor", "entity_id": "sensor.testdevice_battery", "platform": "device", "type": "battery_level", } ] for button in ("button1", "button2", "button3", "button4"): for subtype in ("single_press", "double_press", "long_press"): expected.append( { "device_id": device.id, "domain": "homekit_controller", "platform": "device", "type": button, "subtype": subtype, } ) triggers = await async_get_device_automations(opp, "trigger", device.id) assert_lists_same(triggers, expected)