2019-04-03 15:40:03 +00:00
|
|
|
"""Camera that loads a picture from an MQTT topic."""
|
2021-01-09 13:37:33 +00:00
|
|
|
import functools
|
2017-04-16 09:06:57 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-12-10 20:30:07 +00:00
|
|
|
from homeassistant.components import camera
|
2020-03-17 23:49:50 +00:00
|
|
|
from homeassistant.components.camera import Camera
|
2021-03-11 12:42:13 +00:00
|
|
|
from homeassistant.const import CONF_NAME
|
2021-04-22 21:53:37 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2017-04-16 09:06:57 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv
|
2020-09-03 01:16:21 +00:00
|
|
|
from homeassistant.helpers.reload import async_setup_reload_service
|
2021-04-22 21:53:37 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2021-01-08 23:47:17 +00:00
|
|
|
from . import CONF_QOS, DOMAIN, PLATFORMS, subscription
|
|
|
|
from .. import mqtt
|
|
|
|
from .debug_info import log_messages
|
2021-03-11 12:42:13 +00:00
|
|
|
from .mixins import MQTT_ENTITY_COMMON_SCHEMA, MqttEntity, async_setup_entry_helper
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONF_TOPIC = "topic"
|
|
|
|
DEFAULT_NAME = "MQTT Camera"
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2021-03-11 12:42:13 +00:00
|
|
|
PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
vol.Required(CONF_TOPIC): mqtt.valid_subscribe_topic,
|
|
|
|
}
|
|
|
|
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
|
2017-04-16 09:06:57 +00:00
|
|
|
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(
|
2021-04-22 21:53:37 +00:00
|
|
|
hass: HomeAssistant, config: ConfigType, async_add_entities, discovery_info=None
|
2019-07-31 19:25:30 +00:00
|
|
|
):
|
2018-09-28 14:57:17 +00:00
|
|
|
"""Set up MQTT camera through configuration.yaml."""
|
2020-09-03 01:16:21 +00:00
|
|
|
await async_setup_reload_service(hass, DOMAIN, PLATFORMS)
|
2021-01-09 13:37:33 +00:00
|
|
|
await _async_setup_entity(async_add_entities, config)
|
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."""
|
2021-01-09 13:37:33 +00:00
|
|
|
setup = functools.partial(
|
|
|
|
_async_setup_entity, async_add_entities, config_entry=config_entry
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-01-09 13:37:33 +00:00
|
|
|
await async_setup_entry_helper(hass, camera.DOMAIN, setup, PLATFORM_SCHEMA)
|
2018-09-28 14:57:17 +00:00
|
|
|
|
|
|
|
|
2019-09-04 14:15:40 +00:00
|
|
|
async def _async_setup_entity(
|
2021-01-09 13:37:33 +00:00
|
|
|
async_add_entities, config, config_entry=None, discovery_data=None
|
2019-09-04 14:15:40 +00:00
|
|
|
):
|
2018-09-28 14:57:17 +00:00
|
|
|
"""Set up the MQTT Camera."""
|
2020-02-25 04:46:02 +00:00
|
|
|
async_add_entities([MqttCamera(config, config_entry, discovery_data)])
|
2017-04-16 09:06:57 +00:00
|
|
|
|
|
|
|
|
2021-01-09 16:46:53 +00:00
|
|
|
class MqttCamera(MqttEntity, Camera):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""representation of a MQTT camera."""
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2020-02-25 04:46:02 +00:00
|
|
|
def __init__(self, config, config_entry, discovery_data):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Initialize the MQTT Camera."""
|
2017-04-16 09:06:57 +00:00
|
|
|
self._last_image = None
|
|
|
|
|
2019-01-29 00:21:38 +00:00
|
|
|
Camera.__init__(self)
|
2021-01-09 16:46:53 +00:00
|
|
|
MqttEntity.__init__(self, None, config, config_entry, discovery_data)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def config_schema():
|
|
|
|
"""Return the config schema."""
|
|
|
|
return PLATFORM_SCHEMA
|
|
|
|
|
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
|
2020-04-01 18:48:32 +00:00
|
|
|
@log_messages(self.hass, self.entity_id)
|
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,
|
2020-03-17 23:49:50 +00:00
|
|
|
"qos": self._config[CONF_QOS],
|
2019-07-31 19:25:30 +00:00
|
|
|
"encoding": None,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2020-01-29 21:59:45 +00:00
|
|
|
async def async_camera_image(self):
|
2017-04-16 09:06:57 +00:00
|
|
|
"""Return image response."""
|
|
|
|
return self._last_image
|