diff --git a/homeassistant/components/camera/mqtt.py b/homeassistant/components/camera/mqtt.py new file mode 100755 index 00000000000..39fbdc7fd9f --- /dev/null +++ b/homeassistant/components/camera/mqtt.py @@ -0,0 +1,74 @@ +""" +Camera that loads a picture from an MQTT topic. + +For more details about this platform, please refer to the documentation at +https://home-assistant.io/components/camera.mqtt/ +""" + +import asyncio +import logging + +import voluptuous as vol + +from homeassistant.core import callback +import homeassistant.components.mqtt as mqtt +from homeassistant.const import CONF_NAME +from homeassistant.components.camera import Camera, PLATFORM_SCHEMA +from homeassistant.helpers import config_validation as cv + +_LOGGER = logging.getLogger(__name__) + +CONF_TOPIC = 'topic' + +DEFAULT_NAME = 'MQTT Camera' + +DEPENDENCIES = ['mqtt'] + +PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ + vol.Required(CONF_TOPIC): mqtt.valid_subscribe_topic, + vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string +}) + + +@asyncio.coroutine +def async_setup_platform(hass, config, async_add_devices, discovery_info=None): + """Setup the Camera.""" + topic = config[CONF_TOPIC] + + async_add_devices([MqttCamera(config[CONF_NAME], topic)]) + + +class MqttCamera(Camera): + """MQTT camera.""" + + def __init__(self, name, topic): + """Initialize Local File Camera component.""" + super().__init__() + + self._name = name + self._topic = topic + self._qos = 0 + self._last_image = None + + @asyncio.coroutine + def async_camera_image(self): + """Return image response.""" + return self._last_image + + @property + def name(self): + """Return the name of this camera.""" + return self._name + + def async_added_to_hass(self): + """Subscribe mqtt events. + + This method must be run in the event loop and returns a coroutine. + """ + @callback + def message_received(topic, payload, qos): + """A new MQTT message has been received.""" + self._last_image = payload + + return mqtt.async_subscribe( + self.hass, self._topic, message_received, self._qos, None) diff --git a/tests/components/camera/test_mqtt.py b/tests/components/camera/test_mqtt.py new file mode 100644 index 00000000000..35552ca805e --- /dev/null +++ b/tests/components/camera/test_mqtt.py @@ -0,0 +1,28 @@ +"""The tests for mqtt camera component.""" +import asyncio + +from homeassistant.setup import async_setup_component +import homeassistant.components.mqtt as mqtt + + +@asyncio.coroutine +def test_run_camera_setup(hass, test_client): + """Test that it fetches the given dispatcher data.""" + topic = 'test/camera' + yield from async_setup_component(hass, 'camera', { + 'camera': { + 'platform': 'mqtt', + 'topic': topic, + 'name': 'Test Camera', + }}) + + client = yield from test_client(hass.http.app) + + mqtt.publish(hass, topic, 0xFFD8FF) + yield from hass.async_block_till_done() + + resp = yield from client.get('/api/camera_proxy/camera.test_camera') + + assert resp.status == 200 + body = yield from resp.text() + assert body == '16767231'