Hoist mqtt name property and add icon support to MqttEntity (#47165)
* hoist common MqttEntity properties * remove default name for MqttEntity Default naming is sensible enough * disable overriding common MqttEntity schema * merge common MqttEntity schemas into MQTT_ENTITY_COMMON_SCHEMA
This commit is contained in:
parent
724574d336
commit
9e487eb260
17 changed files with 235 additions and 565 deletions
|
@ -6,7 +6,7 @@ from typing import Optional
|
|||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.const import CONF_DEVICE, CONF_NAME, CONF_UNIQUE_ID
|
||||
from homeassistant.const import CONF_DEVICE, CONF_ICON, CONF_NAME, CONF_UNIQUE_ID
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.dispatcher import (
|
||||
|
@ -134,10 +134,13 @@ MQTT_ENTITY_DEVICE_INFO_SCHEMA = vol.All(
|
|||
validate_device_has_at_least_one_identifier,
|
||||
)
|
||||
|
||||
MQTT_JSON_ATTRS_SCHEMA = vol.Schema(
|
||||
MQTT_ENTITY_COMMON_SCHEMA = MQTT_AVAILABILITY_SCHEMA.extend(
|
||||
{
|
||||
vol.Optional(CONF_DEVICE): MQTT_ENTITY_DEVICE_INFO_SCHEMA,
|
||||
vol.Optional(CONF_ICON): cv.icon,
|
||||
vol.Optional(CONF_JSON_ATTRS_TOPIC): valid_subscribe_topic,
|
||||
vol.Optional(CONF_JSON_ATTRS_TEMPLATE): cv.template,
|
||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
@ -527,11 +530,12 @@ class MqttEntity(
|
|||
def __init__(self, hass, config, config_entry, discovery_data):
|
||||
"""Init the MQTT Entity."""
|
||||
self.hass = hass
|
||||
self._config = config
|
||||
self._unique_id = config.get(CONF_UNIQUE_ID)
|
||||
self._sub_state = None
|
||||
|
||||
# Load config
|
||||
self._setup_from_config(config)
|
||||
self._setup_from_config(self._config)
|
||||
|
||||
# Initialize mixin classes
|
||||
MqttAttributes.__init__(self, config)
|
||||
|
@ -547,7 +551,8 @@ class MqttEntity(
|
|||
async def discovery_update(self, discovery_payload):
|
||||
"""Handle updated discovery message."""
|
||||
config = self.config_schema()(discovery_payload)
|
||||
self._setup_from_config(config)
|
||||
self._config = config
|
||||
self._setup_from_config(self._config)
|
||||
await self.attributes_discovery_update(config)
|
||||
await self.availability_discovery_update(config)
|
||||
await self.device_info_discovery_update(config)
|
||||
|
@ -575,6 +580,16 @@ class MqttEntity(
|
|||
async def _subscribe_topics(self):
|
||||
"""(Re)Subscribe to topics."""
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return icon of the entity if any."""
|
||||
return self._config.get(CONF_ICON)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the device if any."""
|
||||
return self._config.get(CONF_NAME)
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""No polling needed."""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue