mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Use native _attr_
properties for MqttEntity (#83639)
This commit is contained in:
parent
6b43c457ff
commit
b4984ea255
@ -46,7 +46,6 @@ from homeassistant.helpers.entity import (
|
|||||||
ENTITY_CATEGORIES_SCHEMA,
|
ENTITY_CATEGORIES_SCHEMA,
|
||||||
DeviceInfo,
|
DeviceInfo,
|
||||||
Entity,
|
Entity,
|
||||||
EntityCategory,
|
|
||||||
async_generate_entity_id,
|
async_generate_entity_id,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
@ -1011,10 +1010,11 @@ class MqttEntity(
|
|||||||
"""Init the MQTT Entity."""
|
"""Init the MQTT Entity."""
|
||||||
self.hass = hass
|
self.hass = hass
|
||||||
self._config: ConfigType = config
|
self._config: ConfigType = config
|
||||||
self._unique_id: str | None = config.get(CONF_UNIQUE_ID)
|
self._attr_unique_id = config.get(CONF_UNIQUE_ID)
|
||||||
self._sub_state: dict[str, EntitySubscription] = {}
|
self._sub_state: dict[str, EntitySubscription] = {}
|
||||||
|
|
||||||
# Load config
|
# Load config
|
||||||
|
self._setup_common_attributes_from_config(self._config)
|
||||||
self._setup_from_config(self._config)
|
self._setup_from_config(self._config)
|
||||||
|
|
||||||
# Initialize entity_id from config
|
# Initialize entity_id from config
|
||||||
@ -1052,6 +1052,7 @@ class MqttEntity(
|
|||||||
"""Handle updated discovery message."""
|
"""Handle updated discovery message."""
|
||||||
config: DiscoveryInfoType = self.config_schema()(discovery_payload)
|
config: DiscoveryInfoType = self.config_schema()(discovery_payload)
|
||||||
self._config = config
|
self._config = config
|
||||||
|
self._setup_common_attributes_from_config(self._config)
|
||||||
self._setup_from_config(self._config)
|
self._setup_from_config(self._config)
|
||||||
|
|
||||||
# Prepare MQTT subscriptions
|
# Prepare MQTT subscriptions
|
||||||
@ -1100,6 +1101,15 @@ class MqttEntity(
|
|||||||
def config_schema() -> vol.Schema:
|
def config_schema() -> vol.Schema:
|
||||||
"""Return the config schema."""
|
"""Return the config schema."""
|
||||||
|
|
||||||
|
def _setup_common_attributes_from_config(self, config: ConfigType) -> None:
|
||||||
|
"""(Re)Setup the common attributes for the entity."""
|
||||||
|
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
||||||
|
self._attr_entity_registry_enabled_default = bool(
|
||||||
|
config.get(CONF_ENABLED_BY_DEFAULT)
|
||||||
|
)
|
||||||
|
self._attr_icon = config.get(CONF_ICON)
|
||||||
|
self._attr_name = config.get(CONF_NAME)
|
||||||
|
|
||||||
def _setup_from_config(self, config: ConfigType) -> None:
|
def _setup_from_config(self, config: ConfigType) -> None:
|
||||||
"""(Re)Setup the entity."""
|
"""(Re)Setup the entity."""
|
||||||
|
|
||||||
@ -1111,31 +1121,6 @@ class MqttEntity(
|
|||||||
async def _subscribe_topics(self) -> None:
|
async def _subscribe_topics(self) -> None:
|
||||||
"""(Re)Subscribe to topics."""
|
"""(Re)Subscribe to topics."""
|
||||||
|
|
||||||
@property
|
|
||||||
def entity_registry_enabled_default(self) -> bool:
|
|
||||||
"""Return if the entity should be enabled when first added to the entity registry."""
|
|
||||||
return bool(self._config[CONF_ENABLED_BY_DEFAULT])
|
|
||||||
|
|
||||||
@property
|
|
||||||
def entity_category(self) -> EntityCategory | None:
|
|
||||||
"""Return the entity category if any."""
|
|
||||||
return self._config.get(CONF_ENTITY_CATEGORY)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self) -> str | None:
|
|
||||||
"""Return icon of the entity if any."""
|
|
||||||
return self._config.get(CONF_ICON)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str | None:
|
|
||||||
"""Return the name of the device if any."""
|
|
||||||
return self._config.get(CONF_NAME)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self) -> str | None:
|
|
||||||
"""Return a unique ID."""
|
|
||||||
return self._unique_id
|
|
||||||
|
|
||||||
|
|
||||||
def update_device(
|
def update_device(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user