Throttle camera stream to 2fps

This commit is contained in:
Paulus Schoutsen 2015-11-28 18:59:59 -08:00
parent 0df39b4df5
commit 546377e80a
2 changed files with 19 additions and 22 deletions

View File

@ -19,6 +19,7 @@ from homeassistant.const import (
) )
from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.entity_component import EntityComponent
import homeassistant.util.dt as dt_util
DOMAIN = 'camera' DOMAIN = 'camera'
@ -80,19 +81,21 @@ def setup(hass, config):
def _proxy_camera_image(handler, path_match, data): def _proxy_camera_image(handler, path_match, data):
""" Proxies the camera image via the HA server. """ """ Proxies the camera image via the HA server. """
entity_id = path_match.group(ATTR_ENTITY_ID) entity_id = path_match.group(ATTR_ENTITY_ID)
camera = component.entities.get(entity_id)
camera = None if camera is None:
if entity_id in component.entities.keys():
camera = component.entities[entity_id]
if camera:
response = camera.camera_image()
if response is not None:
handler.wfile.write(response)
else:
handler.send_response(HTTP_NOT_FOUND)
else:
handler.send_response(HTTP_NOT_FOUND) handler.send_response(HTTP_NOT_FOUND)
handler.end_headers()
return
response = camera.camera_image()
if response is None:
handler.send_response(HTTP_NOT_FOUND)
handler.end_headers()
return
handler.wfile.write(response)
hass.http.register_path( hass.http.register_path(
'GET', 'GET',
@ -108,12 +111,9 @@ def setup(hass, config):
stream even with only a still image URL available. stream even with only a still image URL available.
""" """
entity_id = path_match.group(ATTR_ENTITY_ID) entity_id = path_match.group(ATTR_ENTITY_ID)
camera = component.entities.get(entity_id)
camera = None if camera is None:
if entity_id in component.entities.keys():
camera = component.entities[entity_id]
if not camera:
handler.send_response(HTTP_NOT_FOUND) handler.send_response(HTTP_NOT_FOUND)
handler.end_headers() handler.end_headers()
return return
@ -131,7 +131,6 @@ def setup(hass, config):
# MJPEG_START_HEADER.format() # MJPEG_START_HEADER.format()
while True: while True:
img_bytes = camera.camera_image() img_bytes = camera.camera_image()
if img_bytes is None: if img_bytes is None:
continue continue
@ -148,12 +147,12 @@ def setup(hass, config):
handler.request.sendall( handler.request.sendall(
bytes('--jpgboundary\r\n', 'utf-8')) bytes('--jpgboundary\r\n', 'utf-8'))
time.sleep(0.5)
except (requests.RequestException, IOError): except (requests.RequestException, IOError):
camera.is_streaming = False camera.is_streaming = False
camera.update_ha_state() camera.update_ha_state()
camera.is_streaming = False
hass.http.register_path( hass.http.register_path(
'GET', 'GET',
re.compile( re.compile(

View File

@ -24,12 +24,10 @@ class DemoCamera(Camera):
def camera_image(self): def camera_image(self):
""" Return a faked still image response. """ """ Return a faked still image response. """
image_path = os.path.join(os.path.dirname(__file__), image_path = os.path.join(os.path.dirname(__file__),
'demo_{}.png'.format(randint(1, 5))) 'demo_{}.png'.format(randint(1, 5)))
with open(image_path, 'rb') as file: with open(image_path, 'rb') as file:
output = file.read() return file.read()
return output
@property @property
def name(self): def name(self):