Improve performance of image streams (#112810)
This commit is contained in:
parent
2a6de1c335
commit
d0d1af8991
2 changed files with 31 additions and 19 deletions
|
@ -336,13 +336,12 @@ async def async_get_still_stream(
|
||||||
# given the low frequency of image updates, it is acceptable.
|
# given the low frequency of image updates, it is acceptable.
|
||||||
frame.extend(frame)
|
frame.extend(frame)
|
||||||
await response.write(frame)
|
await response.write(frame)
|
||||||
# Drain to ensure that the latest frame is available to the client
|
|
||||||
await response.drain()
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
event = asyncio.Event()
|
event = asyncio.Event()
|
||||||
|
|
||||||
async def image_state_update(_event: Event[EventStateChangedData]) -> None:
|
@callback
|
||||||
|
def _async_image_state_update(_event: Event[EventStateChangedData]) -> None:
|
||||||
"""Write image to stream."""
|
"""Write image to stream."""
|
||||||
event.set()
|
event.set()
|
||||||
|
|
||||||
|
@ -350,7 +349,7 @@ async def async_get_still_stream(
|
||||||
remove = async_track_state_change_event(
|
remove = async_track_state_change_event(
|
||||||
hass,
|
hass,
|
||||||
image_entity.entity_id,
|
image_entity.entity_id,
|
||||||
image_state_update,
|
_async_image_state_update,
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
|
|
|
@ -306,22 +306,35 @@ async def test_image_stream(
|
||||||
|
|
||||||
client = await hass_client()
|
client = await hass_client()
|
||||||
|
|
||||||
with patch.object(mock_image, "async_image", return_value=b""):
|
close_future = hass.loop.create_future()
|
||||||
resp = await client.get("/api/image_proxy_stream/image.test")
|
original_get_still_stream = image.async_get_still_stream
|
||||||
assert not resp.closed
|
|
||||||
assert resp.status == HTTPStatus.OK
|
|
||||||
|
|
||||||
mock_image.image_last_updated = datetime.datetime.now()
|
async def _wrap_async_get_still_stream(*args, **kwargs):
|
||||||
mock_image.async_write_ha_state()
|
result = await original_get_still_stream(*args, **kwargs)
|
||||||
# Two blocks to ensure the frame is written
|
hass.loop.call_soon(close_future.set_result, None)
|
||||||
await hass.async_block_till_done()
|
return result
|
||||||
await hass.async_block_till_done()
|
|
||||||
|
|
||||||
with patch.object(mock_image, "async_image", return_value=None):
|
with patch(
|
||||||
mock_image.image_last_updated = datetime.datetime.now()
|
"homeassistant.components.image.async_get_still_stream",
|
||||||
mock_image.async_write_ha_state()
|
_wrap_async_get_still_stream,
|
||||||
# Two blocks to ensure the frame is written
|
):
|
||||||
await hass.async_block_till_done()
|
with patch.object(mock_image, "async_image", return_value=b""):
|
||||||
await hass.async_block_till_done()
|
resp = await client.get("/api/image_proxy_stream/image.test")
|
||||||
|
assert not resp.closed
|
||||||
|
assert resp.status == HTTPStatus.OK
|
||||||
|
|
||||||
|
mock_image.image_last_updated = datetime.datetime.now()
|
||||||
|
mock_image.async_write_ha_state()
|
||||||
|
# Two blocks to ensure the frame is written
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
with patch.object(mock_image, "async_image", return_value=None):
|
||||||
|
mock_image.image_last_updated = datetime.datetime.now()
|
||||||
|
mock_image.async_write_ha_state()
|
||||||
|
# Two blocks to ensure the frame is written
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
await close_future
|
||||||
assert resp.closed
|
assert resp.closed
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue