176 lines
5.4 KiB
Python
176 lines
5.4 KiB
Python
"""Camera that loads a picture from an MQTT topic."""
|
|
from __future__ import annotations
|
|
|
|
from base64 import b64decode
|
|
import functools
|
|
import logging
|
|
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components import camera
|
|
from homeassistant.components.camera import Camera
|
|
from homeassistant.config_entries import ConfigEntry
|
|
from homeassistant.const import CONF_NAME
|
|
from homeassistant.core import HomeAssistant, callback
|
|
from homeassistant.helpers import config_validation as cv
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
|
|
from . import subscription
|
|
from .config import MQTT_BASE_SCHEMA
|
|
from .const import CONF_ENCODING, CONF_QOS, CONF_TOPIC, DEFAULT_ENCODING
|
|
from .debug_info import log_messages
|
|
from .mixins import (
|
|
MQTT_ENTITY_COMMON_SCHEMA,
|
|
MqttEntity,
|
|
async_setup_entry_helper,
|
|
async_setup_platform_helper,
|
|
warn_for_legacy_schema,
|
|
)
|
|
from .util import valid_subscribe_topic
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
CONF_IMAGE_ENCODING = "image_encoding"
|
|
|
|
DEFAULT_NAME = "MQTT Camera"
|
|
|
|
MQTT_CAMERA_ATTRIBUTES_BLOCKED = frozenset(
|
|
{
|
|
"access_token",
|
|
"brand",
|
|
"model_name",
|
|
"motion_detection",
|
|
}
|
|
)
|
|
|
|
|
|
# Using CONF_ENCODING to set b64 encoding for images is deprecated as of Home Assistant 2022.9
|
|
# use CONF_IMAGE_ENCODING instead, support for the work-a-round will be removed with Home Assistant 2022.11
|
|
def repair_legacy_encoding(config: ConfigType) -> ConfigType:
|
|
"""Check incorrect deprecated config of image encoding."""
|
|
if config[CONF_ENCODING] == "b64":
|
|
config[CONF_IMAGE_ENCODING] = "b64"
|
|
config[CONF_ENCODING] = DEFAULT_ENCODING
|
|
_LOGGER.warning(
|
|
"Using the `encoding` parameter to set image encoding has been deprecated, use `image_encoding` instead"
|
|
)
|
|
return config
|
|
|
|
|
|
PLATFORM_SCHEMA_BASE = MQTT_BASE_SCHEMA.extend(
|
|
{
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
vol.Required(CONF_TOPIC): valid_subscribe_topic,
|
|
vol.Optional(CONF_IMAGE_ENCODING): "b64",
|
|
}
|
|
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
|
|
|
|
PLATFORM_SCHEMA_MODERN = vol.All(
|
|
PLATFORM_SCHEMA_BASE.schema,
|
|
repair_legacy_encoding,
|
|
)
|
|
|
|
# Configuring MQTT Camera under the camera platform key is deprecated in HA Core 2022.6
|
|
PLATFORM_SCHEMA = vol.All(
|
|
cv.PLATFORM_SCHEMA.extend(PLATFORM_SCHEMA_BASE.schema),
|
|
warn_for_legacy_schema(camera.DOMAIN),
|
|
repair_legacy_encoding,
|
|
)
|
|
|
|
DISCOVERY_SCHEMA = PLATFORM_SCHEMA_BASE.extend({}, extra=vol.REMOVE_EXTRA)
|
|
|
|
|
|
async def async_setup_platform(
|
|
hass: HomeAssistant,
|
|
config: ConfigType,
|
|
async_add_entities: AddEntitiesCallback,
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
) -> None:
|
|
"""Set up MQTT camera configured under the camera platform key (deprecated)."""
|
|
# Deprecated in HA Core 2022.6
|
|
await async_setup_platform_helper(
|
|
hass,
|
|
camera.DOMAIN,
|
|
discovery_info or config,
|
|
async_add_entities,
|
|
_async_setup_entity,
|
|
)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up MQTT camera through configuration.yaml and dynamically through MQTT discovery."""
|
|
setup = functools.partial(
|
|
_async_setup_entity, hass, async_add_entities, config_entry=config_entry
|
|
)
|
|
await async_setup_entry_helper(hass, camera.DOMAIN, setup, DISCOVERY_SCHEMA)
|
|
|
|
|
|
async def _async_setup_entity(
|
|
hass: HomeAssistant,
|
|
async_add_entities: AddEntitiesCallback,
|
|
config: ConfigType,
|
|
config_entry: ConfigEntry | None = None,
|
|
discovery_data: dict | None = None,
|
|
) -> None:
|
|
"""Set up the MQTT Camera."""
|
|
async_add_entities([MqttCamera(hass, config, config_entry, discovery_data)])
|
|
|
|
|
|
class MqttCamera(MqttEntity, Camera):
|
|
"""representation of a MQTT camera."""
|
|
|
|
_entity_id_format = camera.ENTITY_ID_FORMAT
|
|
_attributes_extra_blocked = MQTT_CAMERA_ATTRIBUTES_BLOCKED
|
|
|
|
def __init__(self, hass, config, config_entry, discovery_data):
|
|
"""Initialize the MQTT Camera."""
|
|
self._last_image = None
|
|
|
|
Camera.__init__(self)
|
|
MqttEntity.__init__(self, hass, config, config_entry, discovery_data)
|
|
|
|
@staticmethod
|
|
def config_schema():
|
|
"""Return the config schema."""
|
|
return DISCOVERY_SCHEMA
|
|
|
|
def _prepare_subscribe_topics(self):
|
|
"""(Re)Subscribe to topics."""
|
|
|
|
@callback
|
|
@log_messages(self.hass, self.entity_id)
|
|
def message_received(msg):
|
|
"""Handle new MQTT messages."""
|
|
if CONF_IMAGE_ENCODING in self._config:
|
|
self._last_image = b64decode(msg.payload)
|
|
else:
|
|
self._last_image = msg.payload
|
|
|
|
self._sub_state = subscription.async_prepare_subscribe_topics(
|
|
self.hass,
|
|
self._sub_state,
|
|
{
|
|
"state_topic": {
|
|
"topic": self._config[CONF_TOPIC],
|
|
"msg_callback": message_received,
|
|
"qos": self._config[CONF_QOS],
|
|
"encoding": None,
|
|
}
|
|
},
|
|
)
|
|
|
|
async def _subscribe_topics(self):
|
|
"""(Re)Subscribe to topics."""
|
|
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
|
|
|
async def async_camera_image(
|
|
self, width: int | None = None, height: int | None = None
|
|
) -> bytes | None:
|
|
"""Return image response."""
|
|
return self._last_image
|