Add optional base64 decoding of mqtt camera image (#71223)
Add unittest for b64 decoding of camera, fix linting
This commit is contained in:
parent
1a45e54324
commit
0fcdca2d71
2 changed files with 35 additions and 2 deletions
|
@ -1,6 +1,7 @@
|
||||||
"""Camera that loads a picture from an MQTT topic."""
|
"""Camera that loads a picture from an MQTT topic."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from base64 import b64decode
|
||||||
import functools
|
import functools
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
@ -16,7 +17,7 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
|
|
||||||
from . import subscription
|
from . import subscription
|
||||||
from .. import mqtt
|
from .. import mqtt
|
||||||
from .const import CONF_QOS, CONF_TOPIC
|
from .const import CONF_ENCODING, CONF_QOS, CONF_TOPIC
|
||||||
from .debug_info import log_messages
|
from .debug_info import log_messages
|
||||||
from .mixins import (
|
from .mixins import (
|
||||||
MQTT_ENTITY_COMMON_SCHEMA,
|
MQTT_ENTITY_COMMON_SCHEMA,
|
||||||
|
@ -102,7 +103,10 @@ class MqttCamera(MqttEntity, Camera):
|
||||||
@log_messages(self.hass, self.entity_id)
|
@log_messages(self.hass, self.entity_id)
|
||||||
def message_received(msg):
|
def message_received(msg):
|
||||||
"""Handle new MQTT messages."""
|
"""Handle new MQTT messages."""
|
||||||
self._last_image = msg.payload
|
if self._config[CONF_ENCODING] == "b64":
|
||||||
|
self._last_image = b64decode(msg.payload)
|
||||||
|
else:
|
||||||
|
self._last_image = msg.payload
|
||||||
|
|
||||||
self._sub_state = subscription.async_prepare_subscribe_topics(
|
self._sub_state = subscription.async_prepare_subscribe_topics(
|
||||||
self.hass,
|
self.hass,
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
"""The tests for mqtt camera component."""
|
"""The tests for mqtt camera component."""
|
||||||
|
from base64 import b64encode
|
||||||
from http import HTTPStatus
|
from http import HTTPStatus
|
||||||
import json
|
import json
|
||||||
from unittest.mock import patch
|
from unittest.mock import patch
|
||||||
|
@ -64,6 +65,34 @@ async def test_run_camera_setup(hass, hass_client_no_auth, mqtt_mock):
|
||||||
assert body == "beer"
|
assert body == "beer"
|
||||||
|
|
||||||
|
|
||||||
|
async def test_run_camera_b64_encoded(hass, hass_client_no_auth, mqtt_mock):
|
||||||
|
"""Test that it fetches the given encoded payload."""
|
||||||
|
topic = "test/camera"
|
||||||
|
await async_setup_component(
|
||||||
|
hass,
|
||||||
|
"camera",
|
||||||
|
{
|
||||||
|
"camera": {
|
||||||
|
"platform": "mqtt",
|
||||||
|
"topic": topic,
|
||||||
|
"name": "Test Camera",
|
||||||
|
"encoding": "b64",
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
await hass.async_block_till_done()
|
||||||
|
|
||||||
|
url = hass.states.get("camera.test_camera").attributes["entity_picture"]
|
||||||
|
|
||||||
|
async_fire_mqtt_message(hass, topic, b64encode(b"grass"))
|
||||||
|
|
||||||
|
client = await hass_client_no_auth()
|
||||||
|
resp = await client.get(url)
|
||||||
|
assert resp.status == HTTPStatus.OK
|
||||||
|
body = await resp.text()
|
||||||
|
assert body == "grass"
|
||||||
|
|
||||||
|
|
||||||
async def test_availability_when_connection_lost(hass, mqtt_mock):
|
async def test_availability_when_connection_lost(hass, mqtt_mock):
|
||||||
"""Test availability after MQTT disconnection."""
|
"""Test availability after MQTT disconnection."""
|
||||||
await help_test_availability_when_connection_lost(
|
await help_test_availability_when_connection_lost(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue