Add device to mqtt camera (#26238)
* Add device to mqtt camera * Support discovery device info update and add tests
This commit is contained in:
1 parent
2c65e02491
commit
c4c21d3e99
2 files changed
+99
-7
No files matched your search
@@ -7,13 +7,19 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant.components import camera, mqtt
|
||||
from homeassistant.components.camera import PLATFORM_SCHEMA, Camera
|
||||
from homeassistant.const import CONF_NAME
|
||||
from homeassistant.const import CONF_NAME, CONF_DEVICE
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||
|
||||
from . import ATTR_DISCOVERY_HASH, CONF_UNIQUE_ID, MqttDiscoveryUpdate, subscription
|
||||
from . import (
|
||||
ATTR_DISCOVERY_HASH,
|
||||
CONF_UNIQUE_ID,
|
||||
MqttDiscoveryUpdate,
|
||||
MqttEntityDeviceInfo,
|
||||
subscription,
|
||||
)
|
||||
from .discovery import MQTT_DISCOVERY_NEW, clear_discovery_hash
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -26,6 +32,7 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Required(CONF_TOPIC): mqtt.valid_subscribe_topic,
|
||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||
vol.Optional(CONF_DEVICE): mqtt.MQTT_ENTITY_DEVICE_INFO_SCHEMA,
|
||||
}
|
||||
)
|
||||
|
||||
@@ -45,7 +52,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
try:
|
||||
discovery_hash = discovery_payload.pop(ATTR_DISCOVERY_HASH)
|
||||
config = PLATFORM_SCHEMA(discovery_payload)
|
||||
await _async_setup_entity(config, async_add_entities, discovery_hash)
|
||||
await _async_setup_entity(
|
||||
config, async_add_entities, config_entry, discovery_hash
|
||||
)
|
||||
except Exception:
|
||||
if discovery_hash:
|
||||
clear_discovery_hash(hass, discovery_hash)
|
||||
@@ -56,15 +65,17 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||
)
|
||||
|
||||
|
||||
async def _async_setup_entity(config, async_add_entities, discovery_hash=None):
|
||||
async def _async_setup_entity(
|
||||
config, async_add_entities, config_entry=None, discovery_hash=None
|
||||
):
|
||||
"""Set up the MQTT Camera."""
|
||||
async_add_entities([MqttCamera(config, discovery_hash)])
|
||||
async_add_entities([MqttCamera(config, config_entry, discovery_hash)])
|
||||
|
||||
|
||||
class MqttCamera(MqttDiscoveryUpdate, Camera):
|
||||
class MqttCamera(MqttDiscoveryUpdate, MqttEntityDeviceInfo, Camera):
|
||||
"""representation of a MQTT camera."""
|
||||
|
||||
def __init__(self, config, discovery_hash):
|
||||
def __init__(self, config, config_entry, discovery_hash):
|
||||
"""Initialize the MQTT Camera."""
|
||||
self._config = config
|
||||
self._unique_id = config.get(CONF_UNIQUE_ID)
|
||||
@@ -73,8 +84,11 @@ class MqttCamera(MqttDiscoveryUpdate, Camera):
|
||||
self._qos = 0
|
||||
self._last_image = None
|
||||
|
||||
device_config = config.get(CONF_DEVICE)
|
||||
|
||||
Camera.__init__(self)
|
||||
MqttDiscoveryUpdate.__init__(self, discovery_hash, self.discovery_update)
|
||||
MqttEntityDeviceInfo.__init__(self, device_config, config_entry)
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Subscribe MQTT events."""
|
||||
@@ -85,6 +99,7 @@ class MqttCamera(MqttDiscoveryUpdate, Camera):
|
||||
"""Handle updated discovery message."""
|
||||
config = PLATFORM_SCHEMA(discovery_payload)
|
||||
self._config = config
|
||||
await self.device_info_discovery_update(config)
|
||||
await self._subscribe_topics()
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
Reference in new issue
Block a user