From 52729e9dc8ff54f1ad55b168f30f4e547488bf4a Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Mon, 10 Aug 2020 17:54:46 +0200 Subject: [PATCH] Add scan_tag webhook to mobile app (#38721) --- .../components/mobile_app/webhook.py | 12 +++++++++ tests/components/mobile_app/test_webhook.py | 25 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/homeassistant/components/mobile_app/webhook.py b/homeassistant/components/mobile_app/webhook.py index ca9c31011ed..d03505b0cb9 100644 --- a/homeassistant/components/mobile_app/webhook.py +++ b/homeassistant/components/mobile_app/webhook.py @@ -538,3 +538,15 @@ async def webhook_get_config(hass, config_entry, data): pass return webhook_response(resp, registration=config_entry.data) + + +@WEBHOOK_COMMANDS.register("scan_tag") +@validate_schema({vol.Required("tag_id"): cv.string}) +async def webhook_scan_tag(hass, config_entry, data): + """Handle a fire event webhook.""" + hass.bus.async_fire( + "tag_scanned", + {"tag_id": data["tag_id"], "device_id": config_entry.data[ATTR_DEVICE_ID]}, + context=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 195c60d830c..bd38bca535b 100644 --- a/tests/components/mobile_app/test_webhook.py +++ b/tests/components/mobile_app/test_webhook.py @@ -406,3 +406,28 @@ async def test_webhook_camera_stream_stream_available_but_errors( webhook_json = await resp.json() assert webhook_json["hls_path"] is None assert webhook_json["mjpeg_path"] == "/api/camera_proxy_stream/camera.stream_camera" + + +async def test_webhook_handle_scan_tag(hass, create_registrations, webhook_client): + """Test that we can scan tags.""" + events = [] + + @callback + def store_event(event): + """Helepr to store events.""" + events.append(event) + + hass.bus.async_listen("tag_scanned", store_event) + + resp = await webhook_client.post( + "/api/webhook/{}".format(create_registrations[1]["webhook_id"]), + json={"type": "scan_tag", "data": {"tag_id": "mock-tag-id"}}, + ) + + assert resp.status == 200 + json = await resp.json() + assert json == {} + + assert len(events) == 1 + assert events[0].data["tag_id"] == "mock-tag-id" + assert events[0].data["device_id"] == "mock-device-id"