diff --git a/homeassistant/components/camera/push.py b/homeassistant/components/camera/push.py index 211edc376f0..b1b815f4566 100644 --- a/homeassistant/components/camera/push.py +++ b/homeassistant/components/camera/push.py @@ -104,6 +104,11 @@ class PushCamera(Camera): self._timeout = timeout self.queue = deque([], buffer_size) + self.queue.append(self._blank_image()) + self._current_image = self.queue[0] + + @classmethod + def _blank_image(cls): from PIL import Image import io @@ -112,8 +117,7 @@ class PushCamera(Camera): imgbuf = io.BytesIO() image.save(imgbuf, "JPEG") - self.queue.append(imgbuf.getvalue()) - self._current_image = imgbuf.getvalue() + return imgbuf.getvalue() @property def state(self): @@ -144,6 +148,7 @@ class PushCamera(Camera): self._expired = async_track_point_in_utc_time( self.hass, reset_state, dt_util.utcnow() + self._timeout) + self._current_image = self.queue[-1] self.async_schedule_update_ha_state() async def async_camera_image(self): diff --git a/tests/components/camera/test_push.py b/tests/components/camera/test_push.py index c0ab2d85784..4cddcdf2720 100644 --- a/tests/components/camera/test_push.py +++ b/tests/components/camera/test_push.py @@ -2,7 +2,7 @@ import io from datetime import timedelta -from unittest import mock +from unittest.mock import patch from homeassistant import core as ha from homeassistant.setup import async_setup_component @@ -10,14 +10,15 @@ from homeassistant.util import dt as dt_util from tests.components.auth import async_setup_auth -@mock.patch("PIL.Image.new") async def test_bad_posting(aioclient_mock, hass, aiohttp_client): """Test that posting to wrong api endpoint fails.""" - await async_setup_component(hass, 'camera', { - 'camera': { - 'platform': 'push', - 'name': 'config_test', - }}) + with patch('homeassistant.components.camera.push.PushCamera._blank_image', + return_value=io.BytesIO(b'fakeinit')): + await async_setup_component(hass, 'camera', { + 'camera': { + 'platform': 'push', + 'name': 'config_test', + }}) client = await async_setup_auth(hass, aiohttp_client) @@ -32,14 +33,15 @@ async def test_bad_posting(aioclient_mock, hass, aiohttp_client): assert resp.status == 400 -@mock.patch("PIL.Image.new") async def test_posting_url(aioclient_mock, hass, aiohttp_client): """Test that posting to api endpoint works.""" - await async_setup_component(hass, 'camera', { - 'camera': { - 'platform': 'push', - 'name': 'config_test', - }}) + with patch('homeassistant.components.camera.push.PushCamera._blank_image', + return_value=io.BytesIO(b'fakeinit')): + await async_setup_component(hass, 'camera', { + 'camera': { + 'platform': 'push', + 'name': 'config_test', + }}) client = await async_setup_auth(hass, aiohttp_client) files = {'image': io.BytesIO(b'fake')}