2019-04-03 15:40:03 +00:00
|
|
|
"""Camera that loads a picture from an MQTT topic."""
|
2021-08-11 00:33:06 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2022-05-09 19:40:15 +00:00
|
|
|
from base64 import b64decode
|
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
|
2022-01-03 07:53:52 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
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
|
2022-01-03 07:53:52 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2022-02-10 20:09:57 +00:00
|
|
|
from . import subscription
|
2021-01-08 23:47:17 +00:00
|
|
|
from .. import mqtt
|
2022-05-09 19:40:15 +00:00
|
|
|
from .const import CONF_ENCODING, CONF_QOS, CONF_TOPIC
|
2021-01-08 23:47:17 +00:00
|
|
|
from .debug_info import log_messages
|
2022-02-10 20:09:57 +00:00
|
|
|
from .mixins import (
|
|
|
|
MQTT_ENTITY_COMMON_SCHEMA,
|
|
|
|
MqttEntity,
|
|
|
|
async_setup_entry_helper,
|
|
|
|
async_setup_platform_helper,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "MQTT Camera"
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2021-06-29 10:14:02 +00:00
|
|
|
MQTT_CAMERA_ATTRIBUTES_BLOCKED = frozenset(
|
|
|
|
{
|
|
|
|
"access_token",
|
|
|
|
"brand",
|
|
|
|
"model_name",
|
|
|
|
"motion_detection",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2021-10-25 11:47:06 +00:00
|
|
|
DISCOVERY_SCHEMA = PLATFORM_SCHEMA.extend({}, extra=vol.REMOVE_EXTRA)
|
|
|
|
|
2017-04-16 09:06:57 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
async def async_setup_platform(
|
2022-01-03 07:53:52 +00:00
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2018-09-28 14:57:17 +00:00
|
|
|
"""Set up MQTT camera through configuration.yaml."""
|
2022-02-10 20:09:57 +00:00
|
|
|
await async_setup_platform_helper(
|
|
|
|
hass, camera.DOMAIN, config, async_add_entities, _async_setup_entity
|
|
|
|
)
|
2018-09-28 14:57:17 +00:00
|
|
|
|
|
|
|
|
2022-01-03 07:53:52 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2018-09-28 14:57:17 +00:00
|
|
|
"""Set up MQTT camera dynamically through MQTT discovery."""
|
2021-01-09 13:37:33 +00:00
|
|
|
setup = functools.partial(
|
2021-06-23 13:44:18 +00:00
|
|
|
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2021-10-25 11:47:06 +00:00
|
|
|
await async_setup_entry_helper(hass, camera.DOMAIN, setup, DISCOVERY_SCHEMA)
|
2018-09-28 14:57:17 +00:00
|
|
|
|
|
|
|
|
2019-09-04 14:15:40 +00:00
|
|
|
async def _async_setup_entity(
|
2021-06-23 13:44:18 +00:00
|
|
|
hass, 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."""
|
2021-06-23 13:44:18 +00:00
|
|
|
async_add_entities([MqttCamera(hass, 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
|
|
|
|
2021-11-08 13:02:18 +00:00
|
|
|
_entity_id_format = camera.ENTITY_ID_FORMAT
|
2021-06-29 10:14:02 +00:00
|
|
|
_attributes_extra_blocked = MQTT_CAMERA_ATTRIBUTES_BLOCKED
|
|
|
|
|
2021-06-23 13:44:18 +00:00
|
|
|
def __init__(self, hass, 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-06-23 13:44:18 +00:00
|
|
|
MqttEntity.__init__(self, hass, config, config_entry, discovery_data)
|
2021-01-09 16:46:53 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def config_schema():
|
|
|
|
"""Return the config schema."""
|
2021-10-25 11:47:06 +00:00
|
|
|
return DISCOVERY_SCHEMA
|
2021-01-09 16:46:53 +00:00
|
|
|
|
2022-02-03 01:12:22 +00:00
|
|
|
def _prepare_subscribe_topics(self):
|
2019-01-29 00:21:38 +00:00
|
|
|
"""(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."""
|
2022-05-09 19:40:15 +00:00
|
|
|
if self._config[CONF_ENCODING] == "b64":
|
|
|
|
self._last_image = b64decode(msg.payload)
|
|
|
|
else:
|
|
|
|
self._last_image = msg.payload
|
2019-01-29 00:21:38 +00:00
|
|
|
|
2022-02-03 01:12:22 +00:00
|
|
|
self._sub_state = subscription.async_prepare_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
|
|
|
|
2022-02-03 01:12:22 +00:00
|
|
|
async def _subscribe_topics(self):
|
|
|
|
"""(Re)Subscribe to topics."""
|
|
|
|
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
|
|
|
|
2021-08-11 00:33:06 +00:00
|
|
|
async def async_camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2017-04-16 09:06:57 +00:00
|
|
|
"""Return image response."""
|
|
|
|
return self._last_image
|