Use native _attr_ properties for MqttEntity (#83639)

This commit is contained in:
Jan Bouwhuis 2022-12-09 13:37:26 +01:00 committed by GitHub
parent 6b43c457ff
commit b4984ea255
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -46,7 +46,6 @@ from homeassistant.helpers.entity import (
ENTITY_CATEGORIES_SCHEMA,
DeviceInfo,
Entity,
EntityCategory,
async_generate_entity_id,
)
from homeassistant.helpers.entity_platform import AddEntitiesCallback
@ -1011,10 +1010,11 @@ class MqttEntity(
"""Init the MQTT Entity."""
self.hass = hass
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] = {}
# Load config
self._setup_common_attributes_from_config(self._config)
self._setup_from_config(self._config)
# Initialize entity_id from config
@ -1052,6 +1052,7 @@ class MqttEntity(
"""Handle updated discovery message."""
config: DiscoveryInfoType = self.config_schema()(discovery_payload)
self._config = config
self._setup_common_attributes_from_config(self._config)
self._setup_from_config(self._config)
# Prepare MQTT subscriptions
@ -1100,6 +1101,15 @@ class MqttEntity(
def config_schema() -> vol.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:
"""(Re)Setup the entity."""
@ -1111,31 +1121,6 @@ class MqttEntity(
async def _subscribe_topics(self) -> None:
"""(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(
hass: HomeAssistant,