From 7dff44b3e97c77da9efc77cc932913021afcda7b Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 28 Nov 2022 08:35:57 -0500 Subject: [PATCH] Fix mobile app passing incorrect device ID when scanning tag (#82820) --- .../components/mobile_app/webhook.py | 3 ++- tests/components/mobile_app/test_webhook.py | 19 ++++++++----------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/mobile_app/webhook.py b/homeassistant/components/mobile_app/webhook.py index 7fecf461b13..cd54c4216b5 100644 --- a/homeassistant/components/mobile_app/webhook.py +++ b/homeassistant/components/mobile_app/webhook.py @@ -95,6 +95,7 @@ from .const import ( CONF_SECRET, DATA_CONFIG_ENTRIES, DATA_DELETED_IDS, + DATA_DEVICES, DOMAIN, ERR_ENCRYPTION_ALREADY_ENABLED, ERR_ENCRYPTION_NOT_AVAILABLE, @@ -722,7 +723,7 @@ async def webhook_scan_tag( await tag.async_scan_tag( hass, data["tag_id"], - config_entry.data[ATTR_DEVICE_ID], + hass.data[DOMAIN][DATA_DEVICES][config_entry.data[CONF_WEBHOOK_ID]].id, registration_context(config_entry.data), ) return empty_okay_response() diff --git a/tests/components/mobile_app/test_webhook.py b/tests/components/mobile_app/test_webhook.py index 30036c0aba7..0794aab0fda 100644 --- a/tests/components/mobile_app/test_webhook.py +++ b/tests/components/mobile_app/test_webhook.py @@ -6,7 +6,8 @@ from unittest.mock import patch import pytest from homeassistant.components.camera import CameraEntityFeature -from homeassistant.components.mobile_app.const import CONF_SECRET +from homeassistant.components.mobile_app.const import CONF_SECRET, DOMAIN +from homeassistant.components.tag import EVENT_TAG_SCANNED from homeassistant.components.zone import DOMAIN as ZONE_DOMAIN from homeassistant.const import ( CONF_WEBHOOK_ID, @@ -16,11 +17,11 @@ from homeassistant.const import ( ) from homeassistant.core import callback from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import entity_registry as er +from homeassistant.helpers import device_registry as dr, entity_registry as er from .const import CALL_SERVICE, FIRE_EVENT, REGISTER_CLEARTEXT, RENDER_TEMPLATE, UPDATE -from tests.common import async_mock_service +from tests.common import async_capture_events, async_mock_service def encrypt_payload(secret_key, payload, encode_json=True): @@ -840,14 +841,10 @@ async def test_webhook_camera_stream_stream_available_but_errors( async def test_webhook_handle_scan_tag(hass, create_registrations, webhook_client): """Test that we can scan tags.""" - events = [] + device = dr.async_get(hass).async_get_device({(DOMAIN, "mock-device-id")}) + assert device is not None - @callback - def store_event(event): - """Help store events.""" - events.append(event) - - hass.bus.async_listen("tag_scanned", store_event) + events = async_capture_events(hass, EVENT_TAG_SCANNED) resp = await webhook_client.post( "/api/webhook/{}".format(create_registrations[1]["webhook_id"]), @@ -860,7 +857,7 @@ async def test_webhook_handle_scan_tag(hass, create_registrations, webhook_clien assert len(events) == 1 assert events[0].data["tag_id"] == "mock-tag-id" - assert events[0].data["device_id"] == "mock-device-id" + assert events[0].data["device_id"] == device.id async def test_register_sensor_limits_state_class(