mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Refactor MQTT scene to inherit MqttEntity (#68883)
* MQTT scene inherrits MqttEntity * Default payload_on matches documentation * remove CONF_ENABLED_BY_DEFAULT from schema return True as default * Add to the schema, remove not overrided properties * remove default payload_on * Update homeassistant/components/mqtt/scene.py Co-authored-by: Erik Montnemery <erik@montnemery.com>
This commit is contained in:
parent
91f6e58e9a
commit
4db2270f3d
@ -17,13 +17,12 @@ from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|||||||
from .. import mqtt
|
from .. import mqtt
|
||||||
from .const import CONF_COMMAND_TOPIC, CONF_ENCODING, CONF_QOS, CONF_RETAIN
|
from .const import CONF_COMMAND_TOPIC, CONF_ENCODING, CONF_QOS, CONF_RETAIN
|
||||||
from .mixins import (
|
from .mixins import (
|
||||||
|
CONF_ENABLED_BY_DEFAULT,
|
||||||
CONF_OBJECT_ID,
|
CONF_OBJECT_ID,
|
||||||
MQTT_AVAILABILITY_SCHEMA,
|
MQTT_AVAILABILITY_SCHEMA,
|
||||||
MqttAvailability,
|
MqttEntity,
|
||||||
MqttDiscoveryUpdate,
|
|
||||||
async_setup_entry_helper,
|
async_setup_entry_helper,
|
||||||
async_setup_platform_helper,
|
async_setup_platform_helper,
|
||||||
init_entity_id_from_config,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
DEFAULT_NAME = "MQTT Scene"
|
DEFAULT_NAME = "MQTT Scene"
|
||||||
@ -38,6 +37,8 @@ PLATFORM_SCHEMA = mqtt.MQTT_BASE_PLATFORM_SCHEMA.extend(
|
|||||||
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
vol.Optional(CONF_UNIQUE_ID): cv.string,
|
||||||
vol.Optional(CONF_RETAIN, default=DEFAULT_RETAIN): cv.boolean,
|
vol.Optional(CONF_RETAIN, default=DEFAULT_RETAIN): cv.boolean,
|
||||||
vol.Optional(CONF_OBJECT_ID): cv.string,
|
vol.Optional(CONF_OBJECT_ID): cv.string,
|
||||||
|
# CONF_ENABLED_BY_DEFAULT is not added by default because we are not using the common schema here
|
||||||
|
vol.Optional(CONF_ENABLED_BY_DEFAULT, default=True): cv.boolean,
|
||||||
}
|
}
|
||||||
).extend(MQTT_AVAILABILITY_SCHEMA.schema)
|
).extend(MQTT_AVAILABILITY_SCHEMA.schema)
|
||||||
|
|
||||||
@ -76,8 +77,7 @@ async def _async_setup_entity(
|
|||||||
|
|
||||||
|
|
||||||
class MqttScene(
|
class MqttScene(
|
||||||
MqttAvailability,
|
MqttEntity,
|
||||||
MqttDiscoveryUpdate,
|
|
||||||
Scene,
|
Scene,
|
||||||
):
|
):
|
||||||
"""Representation of a scene that can be activated using MQTT."""
|
"""Representation of a scene that can be activated using MQTT."""
|
||||||
@ -86,62 +86,22 @@ class MqttScene(
|
|||||||
|
|
||||||
def __init__(self, hass, config, config_entry, discovery_data):
|
def __init__(self, hass, config, config_entry, discovery_data):
|
||||||
"""Initialize the MQTT scene."""
|
"""Initialize the MQTT scene."""
|
||||||
self.hass = hass
|
MqttEntity.__init__(self, hass, config, config_entry, discovery_data)
|
||||||
self._state = False
|
|
||||||
self._sub_state = None
|
|
||||||
|
|
||||||
self._unique_id = config.get(CONF_UNIQUE_ID)
|
@staticmethod
|
||||||
|
def config_schema():
|
||||||
# Load config
|
"""Return the config schema."""
|
||||||
self._setup_from_config(config)
|
return DISCOVERY_SCHEMA
|
||||||
|
|
||||||
# Initialize entity_id from config
|
|
||||||
self._init_entity_id()
|
|
||||||
|
|
||||||
MqttAvailability.__init__(self, config)
|
|
||||||
MqttDiscoveryUpdate.__init__(self, discovery_data, self.discovery_update)
|
|
||||||
|
|
||||||
def _init_entity_id(self):
|
|
||||||
"""Set entity_id from object_id if defined in config."""
|
|
||||||
init_entity_id_from_config(
|
|
||||||
self.hass, self, self._config, self._entity_id_format
|
|
||||||
)
|
|
||||||
|
|
||||||
async def async_added_to_hass(self):
|
|
||||||
"""Subscribe to MQTT events."""
|
|
||||||
await super().async_added_to_hass()
|
|
||||||
self.async_send_discovery_done()
|
|
||||||
|
|
||||||
async def discovery_update(self, discovery_payload):
|
|
||||||
"""Handle updated discovery message."""
|
|
||||||
config = DISCOVERY_SCHEMA(discovery_payload)
|
|
||||||
self._setup_from_config(config)
|
|
||||||
await self.availability_discovery_update(config)
|
|
||||||
self.async_write_ha_state()
|
|
||||||
|
|
||||||
def _setup_from_config(self, config):
|
def _setup_from_config(self, config):
|
||||||
"""(Re)Setup the entity."""
|
"""(Re)Setup the entity."""
|
||||||
self._config = config
|
self._config = config
|
||||||
|
|
||||||
async def async_will_remove_from_hass(self):
|
def _prepare_subscribe_topics(self):
|
||||||
"""Unsubscribe when removed."""
|
"""(Re)Subscribe to topics."""
|
||||||
await MqttAvailability.async_will_remove_from_hass(self)
|
|
||||||
await MqttDiscoveryUpdate.async_will_remove_from_hass(self)
|
|
||||||
|
|
||||||
@property
|
async def _subscribe_topics(self):
|
||||||
def name(self):
|
"""(Re)Subscribe to topics."""
|
||||||
"""Return the name of the scene."""
|
|
||||||
return self._config[CONF_NAME]
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self):
|
|
||||||
"""Return a unique ID."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon."""
|
|
||||||
return self._config.get(CONF_ICON)
|
|
||||||
|
|
||||||
async def async_activate(self, **kwargs):
|
async def async_activate(self, **kwargs):
|
||||||
"""Activate the scene.
|
"""Activate the scene.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user