hass-core/homeassistant/components/camera/demo.py

41 lines
1.1 KiB
Python
Raw Normal View History

2015-11-19 19:27:10 +01:00
"""
Demo camera platform that has a fake camera.
For more details about this platform, please refer to the documentation
https://home-assistant.io/components/demo/
2015-11-19 19:27:10 +01:00
"""
import os
2016-02-18 21:27:50 -08:00
2015-11-28 23:16:20 -08:00
import homeassistant.util.dt as dt_util
2016-02-18 21:27:50 -08:00
from homeassistant.components.camera import Camera
2015-11-19 19:27:10 +01:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Setup the Demo camera platform."""
2015-11-19 19:27:10 +01:00
add_devices([
DemoCamera('Demo camera')
])
class DemoCamera(Camera):
"""The representation of a Demo camera."""
2016-03-07 20:29:54 +01:00
2015-11-19 19:27:10 +01:00
def __init__(self, name):
2016-03-07 20:29:54 +01:00
"""Initialize demo camera component."""
2015-11-19 19:27:10 +01:00
super().__init__()
self._name = name
def camera_image(self):
"""Return a faked still image response."""
2015-11-28 23:16:20 -08:00
now = dt_util.utcnow()
2015-11-19 19:27:10 +01:00
image_path = os.path.join(os.path.dirname(__file__),
2015-11-28 23:16:20 -08:00
'demo_{}.jpg'.format(now.second % 4))
2015-11-19 19:27:10 +01:00
with open(image_path, 'rb') as file:
2015-11-28 18:59:59 -08:00
return file.read()
2015-11-19 19:27:10 +01:00
@property
def name(self):
"""Return the name of this camera."""
2015-11-19 19:27:10 +01:00
return self._name