Skip to content

Commit

Permalink
Add scan_tag webhook to mobile app (#38721)
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob authored and frenck committed Aug 12, 2020
1 parent e3335ee commit db64614
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
12 changes: 12 additions & 0 deletions homeassistant/components/mobile_app/webhook.py
Expand Up @@ -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()
25 changes: 25 additions & 0 deletions tests/components/mobile_app/test_webhook.py
Expand Up @@ -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"

0 comments on commit db64614

Please sign in to comment.