Add scan_tag webhook to mobile app (#38721)
parent
3d0ea42ac0
commit
52729e9dc8
|
@ -538,3 +538,15 @@ async def webhook_get_config(hass, config_entry, data):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
return webhook_response(resp, registration=config_entry.data)
|
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()
|
||||||
|
|
|
@ -406,3 +406,28 @@ async def test_webhook_camera_stream_stream_available_but_errors(
|
||||||
webhook_json = await resp.json()
|
webhook_json = await resp.json()
|
||||||
assert webhook_json["hls_path"] is None
|
assert webhook_json["hls_path"] is None
|
||||||
assert webhook_json["mjpeg_path"] == "/api/camera_proxy_stream/camera.stream_camera"
|
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"
|
||||||
|
|
Loading…
Reference in New Issue