Initial Visquitto MQTT release
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
"""Support for MQTT images."""
|
||||
|
||||
from base64 import b64decode
|
||||
import binascii
|
||||
from collections.abc import Callable
|
||||
import logging
|
||||
from typing import TYPE_CHECKING, Any, override
|
||||
|
||||
import httpx
|
||||
import probatio
|
||||
|
||||
from homeassistant.components import image
|
||||
from homeassistant.components.image import DEFAULT_CONTENT_TYPE, ImageEntity
|
||||
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 AddConfigEntryEntitiesCallback
|
||||
from homeassistant.helpers.httpx_client import get_async_client
|
||||
from homeassistant.helpers.service_info.mqtt import ReceivePayloadType
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType, VolSchemaType
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import subscription
|
||||
from .config import MQTT_BASE_SCHEMA
|
||||
from .const import (
|
||||
CONF_CONTENT_TYPE,
|
||||
CONF_IMAGE_ENCODING,
|
||||
CONF_IMAGE_TOPIC,
|
||||
CONF_URL_TEMPLATE,
|
||||
CONF_URL_TOPIC,
|
||||
)
|
||||
from .entity import MqttEntity, async_setup_entity_entry_helper
|
||||
from .models import (
|
||||
DATA_MQTT,
|
||||
MqttValueTemplate,
|
||||
MqttValueTemplateException,
|
||||
ReceiveMessage,
|
||||
)
|
||||
from .schemas import MQTT_ENTITY_COMMON_SCHEMA
|
||||
from .util import valid_subscribe_topic
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PARALLEL_UPDATES = 0
|
||||
|
||||
DEFAULT_NAME = "MQTT Image"
|
||||
|
||||
GET_IMAGE_TIMEOUT = 10
|
||||
|
||||
|
||||
def validate_topic_required(config: ConfigType) -> ConfigType:
|
||||
"""Ensure at least one subscribe topic is configured."""
|
||||
if CONF_IMAGE_TOPIC not in config and CONF_URL_TOPIC not in config:
|
||||
raise probatio.Invalid("Expected one of [`image_topic`, `url_topic`], got none")
|
||||
if CONF_CONTENT_TYPE in config and CONF_URL_TOPIC in config:
|
||||
raise probatio.Invalid(
|
||||
"Option `content_type` can not be used together with `url_topic`"
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
PLATFORM_SCHEMA_BASE = MQTT_BASE_SCHEMA.extend(
|
||||
{
|
||||
probatio.Optional(CONF_CONTENT_TYPE): cv.string,
|
||||
probatio.Optional(CONF_NAME): probatio.Any(cv.string, None),
|
||||
probatio.Exclusive(CONF_URL_TOPIC, "image_topic"): valid_subscribe_topic,
|
||||
probatio.Exclusive(CONF_IMAGE_TOPIC, "image_topic"): valid_subscribe_topic,
|
||||
probatio.Optional(CONF_IMAGE_ENCODING): probatio.In({"b64", "raw"}),
|
||||
probatio.Optional(CONF_URL_TEMPLATE): cv.template,
|
||||
}
|
||||
).extend(MQTT_ENTITY_COMMON_SCHEMA.schema)
|
||||
|
||||
PLATFORM_SCHEMA_MODERN = probatio.All(
|
||||
PLATFORM_SCHEMA_BASE.schema, validate_topic_required
|
||||
)
|
||||
|
||||
DISCOVERY_SCHEMA = probatio.All(
|
||||
PLATFORM_SCHEMA_BASE.extend({}, extra=probatio.REMOVE_EXTRA),
|
||||
validate_topic_required,
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddConfigEntryEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up MQTT image through YAML and through MQTT discovery."""
|
||||
async_setup_entity_entry_helper(
|
||||
hass,
|
||||
config_entry,
|
||||
MqttImage,
|
||||
image.DOMAIN,
|
||||
async_add_entities,
|
||||
DISCOVERY_SCHEMA,
|
||||
PLATFORM_SCHEMA_MODERN,
|
||||
)
|
||||
|
||||
|
||||
class MqttImage(MqttEntity, ImageEntity):
|
||||
"""representation of a MQTT image."""
|
||||
|
||||
_default_name = DEFAULT_NAME
|
||||
_entity_id_format: str = image.ENTITY_ID_FORMAT
|
||||
_last_image: bytes | None = None
|
||||
_client: httpx.AsyncClient
|
||||
_url_template: Callable[[ReceivePayloadType], ReceivePayloadType]
|
||||
_topic: dict[str, Any]
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
config_entry: ConfigEntry,
|
||||
discovery_data: DiscoveryInfoType | None,
|
||||
) -> None:
|
||||
"""Initialize the MQTT Image."""
|
||||
self._client = get_async_client(hass)
|
||||
ImageEntity.__init__(self, hass)
|
||||
MqttEntity.__init__(self, hass, config, config_entry, discovery_data)
|
||||
|
||||
@staticmethod
|
||||
@override
|
||||
def config_schema() -> VolSchemaType:
|
||||
"""Return the config schema."""
|
||||
return DISCOVERY_SCHEMA
|
||||
|
||||
@override
|
||||
def _setup_from_config(self, config: ConfigType) -> None:
|
||||
"""(Re)Setup the entity."""
|
||||
self._topic = {
|
||||
key: config.get(key)
|
||||
for key in (
|
||||
CONF_IMAGE_TOPIC,
|
||||
CONF_URL_TOPIC,
|
||||
)
|
||||
}
|
||||
if CONF_IMAGE_TOPIC in config:
|
||||
self._attr_content_type = config.get(
|
||||
CONF_CONTENT_TYPE, DEFAULT_CONTENT_TYPE
|
||||
)
|
||||
if CONF_URL_TOPIC in config:
|
||||
self._attr_image_url = None
|
||||
self._url_template = MqttValueTemplate(
|
||||
config.get(CONF_URL_TEMPLATE), entity=self
|
||||
).async_render_with_possible_json_value
|
||||
|
||||
@callback
|
||||
def _image_data_received(self, msg: ReceiveMessage) -> None:
|
||||
"""Handle new MQTT messages."""
|
||||
try:
|
||||
if self._config.get(CONF_IMAGE_ENCODING) == "b64":
|
||||
self._last_image = b64decode(msg.payload)
|
||||
else:
|
||||
if TYPE_CHECKING:
|
||||
assert isinstance(msg.payload, bytes)
|
||||
self._last_image = msg.payload
|
||||
except (binascii.Error, ValueError, AssertionError) as err:
|
||||
_LOGGER.error(
|
||||
"Error processing image data received at topic %s: %s",
|
||||
msg.topic,
|
||||
err,
|
||||
)
|
||||
self._last_image = None
|
||||
self._attr_image_last_updated = dt_util.utcnow()
|
||||
self.hass.data[DATA_MQTT].state_write_requests.write_state_request(self)
|
||||
|
||||
@callback
|
||||
def _image_from_url_request_received(self, msg: ReceiveMessage) -> None:
|
||||
"""Handle new MQTT messages."""
|
||||
try:
|
||||
url = cv.url(self._url_template(msg.payload))
|
||||
self._attr_image_url = url
|
||||
except MqttValueTemplateException as exc:
|
||||
_LOGGER.warning(exc)
|
||||
return
|
||||
except probatio.Invalid:
|
||||
_LOGGER.error(
|
||||
"Invalid image URL '%s' received at topic %s",
|
||||
msg.payload,
|
||||
msg.topic,
|
||||
)
|
||||
self._attr_image_last_updated = dt_util.utcnow()
|
||||
self._cached_image = None
|
||||
self.hass.data[DATA_MQTT].state_write_requests.write_state_request(self)
|
||||
|
||||
@callback
|
||||
@override
|
||||
def _prepare_subscribe_topics(self) -> None:
|
||||
"""(Re)Subscribe to topics."""
|
||||
self.add_subscription(
|
||||
CONF_IMAGE_TOPIC, self._image_data_received, None, disable_encoding=True
|
||||
)
|
||||
self.add_subscription(
|
||||
CONF_URL_TOPIC, self._image_from_url_request_received, None
|
||||
)
|
||||
|
||||
@override
|
||||
async def _subscribe_topics(self) -> None:
|
||||
"""(Re)Subscribe to topics."""
|
||||
subscription.async_subscribe_topics_internal(self.hass, self._sub_state)
|
||||
|
||||
@override
|
||||
async def async_image(self) -> bytes | None:
|
||||
"""Return bytes of image."""
|
||||
if CONF_IMAGE_TOPIC in self._config:
|
||||
return self._last_image
|
||||
return await super().async_image()
|
||||
Reference in New Issue
Block a user