2019-04-03 15:40:03 +00:00
|
|
|
"""Camera that loads a picture from an MQTT topic."""
|
2017-04-16 09:06:57 +00:00
|
|
|
|
|
|
|
import asyncio
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.components import camera, mqtt
|
|
|
|
from homeassistant.components.camera import PLATFORM_SCHEMA, Camera
|
|
|
|
from homeassistant.const import CONF_NAME
|
|
|
|
from homeassistant.core import callback
|
2017-04-16 09:06:57 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2019-01-27 17:54:52 +00:00
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
from . import ATTR_DISCOVERY_HASH, CONF_UNIQUE_ID, MqttDiscoveryUpdate, subscription
|
2019-03-21 05:56:46 +00:00
|
|
|
from .discovery import MQTT_DISCOVERY_NEW, clear_discovery_hash
|
|
|
|
|
2017-04-16 09:06:57 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_TOPIC = "topic"
|
|
|
|
DEFAULT_NAME = "MQTT Camera"
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Required(CONF_TOPIC): mqtt.valid_subscribe_topic,
|
|
|
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2017-04-16 09:06:57 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(
|
|
|
|
hass: HomeAssistantType, config: ConfigType, async_add_entities, discovery_info=None
|
|
|
|
):
|
2018-09-28 14:57:17 +00:00
|
|
|
"""Set up MQTT camera through configuration.yaml."""
|
2019-01-29 00:21:38 +00:00
|
|
|
await _async_setup_entity(config, async_add_entities)
|
2018-09-28 14:57:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
|
|
|
"""Set up MQTT camera dynamically through MQTT discovery."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2018-09-28 14:57:17 +00:00
|
|
|
async def async_discover(discovery_payload):
|
|
|
|
"""Discover and add a MQTT camera."""
|
2019-01-29 00:21:38 +00:00
|
|
|
try:
|
2019-02-05 05:52:19 +00:00
|
|
|
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
|
2019-01-29 00:21:38 +00:00
|
|
|
config = PLATFORM_SCHEMA(discovery_payload)
|
2019-07-31 19:25:30 +00:00
|
|
|
await _async_setup_entity(config, async_add_entities, discovery_hash)
|
2019-01-29 00:21:38 +00:00
|
|
|
except Exception:
|
|
|
|
if discovery_hash:
|
|
|
|
clear_discovery_hash(hass, discovery_hash)
|
|
|
|
raise
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2018-09-28 14:57:17 +00:00
|
|
|
async_dispatcher_connect(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass, MQTT_DISCOVERY_NEW.format(camera.DOMAIN, "mqtt"), async_discover
|
|
|
|
)
|
2018-09-28 14:57:17 +00:00
|
|
|
|
|
|
|
|
2019-01-29 00:21:38 +00:00
|
|
|
async def _async_setup_entity(config, async_add_entities, discovery_hash=None):
|
2018-09-28 14:57:17 +00:00
|
|
|
"""Set up the MQTT Camera."""
|
2019-01-29 00:21:38 +00:00
|
|
|
async_add_entities([MqttCamera(config, discovery_hash)])
|
2017-04-16 09:06:57 +00:00
|
|
|
|
|
|
|
|
2019-01-29 00:21:38 +00:00
|
|
|
class MqttCamera(MqttDiscoveryUpdate, Camera):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""representation of a MQTT camera."""
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2019-01-29 00:21:38 +00:00
|
|
|
def __init__(self, config, discovery_hash):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Initialize the MQTT Camera."""
|
2019-01-29 00:21:38 +00:00
|
|
|
self._config = config
|
|
|
|
self._unique_id = config.get(CONF_UNIQUE_ID)
|
|
|
|
self._sub_state = None
|
2017-04-16 09:06:57 +00:00
|
|
|
|
|
|
|
self._qos = 0
|
|
|
|
self._last_image = None
|
|
|
|
|
2019-01-29 00:21:38 +00:00
|
|
|
Camera.__init__(self)
|
2019-07-31 19:25:30 +00:00
|
|
|
MqttDiscoveryUpdate.__init__(self, discovery_hash, self.discovery_update)
|
2019-01-29 00:21:38 +00:00
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Subscribe MQTT events."""
|
|
|
|
await super().async_added_to_hass()
|
|
|
|
await self._subscribe_topics()
|
|
|
|
|
|
|
|
async def discovery_update(self, discovery_payload):
|
|
|
|
"""Handle updated discovery message."""
|
|
|
|
config = PLATFORM_SCHEMA(discovery_payload)
|
|
|
|
self._config = config
|
|
|
|
await self._subscribe_topics()
|
2019-03-12 21:46:48 +00:00
|
|
|
self.async_write_ha_state()
|
2019-01-29 00:21:38 +00:00
|
|
|
|
|
|
|
async def _subscribe_topics(self):
|
|
|
|
"""(Re)Subscribe to topics."""
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2019-01-29 00:21:38 +00:00
|
|
|
@callback
|
2019-03-13 19:58:20 +00:00
|
|
|
def message_received(msg):
|
2019-01-29 00:21:38 +00:00
|
|
|
"""Handle new MQTT messages."""
|
2019-03-13 19:58:20 +00:00
|
|
|
self._last_image = msg.payload
|
2019-01-29 00:21:38 +00:00
|
|
|
|
|
|
|
self._sub_state = await subscription.async_subscribe_topics(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass,
|
|
|
|
self._sub_state,
|
|
|
|
{
|
|
|
|
"state_topic": {
|
|
|
|
"topic": self._config[CONF_TOPIC],
|
|
|
|
"msg_callback": message_received,
|
|
|
|
"qos": self._qos,
|
|
|
|
"encoding": None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-01-29 00:21:38 +00:00
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self):
|
|
|
|
"""Unsubscribe when removed."""
|
|
|
|
self._sub_state = await subscription.async_unsubscribe_topics(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.hass, self._sub_state
|
|
|
|
)
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2017-04-16 09:06:57 +00:00
|
|
|
@asyncio.coroutine
|
|
|
|
def async_camera_image(self):
|
|
|
|
"""Return image response."""
|
|
|
|
return self._last_image
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of this camera."""
|
2019-04-07 14:11:45 +00:00
|
|
|
return self._config[CONF_NAME]
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2018-09-21 11:09:54 +00:00
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Return a unique ID."""
|
|
|
|
return self._unique_id
|