Teach TemplateEntity unique_id (#64837)

This commit is contained in:
Erik Montnemery 2022-01-24 23:16:31 +01:00 committed by GitHub
parent 4f7dd821a5
commit e9a9b119c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 29 additions and 73 deletions

View File

@ -138,7 +138,9 @@ class AlarmControlPanelTemplate(TemplateEntity, AlarmControlPanelEntity):
unique_id,
):
"""Initialize the panel."""
super().__init__(hass, config=config, fallback_name=object_id)
super().__init__(
hass, config=config, fallback_name=object_id, unique_id=unique_id
)
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, object_id, hass=hass
)
@ -160,12 +162,6 @@ class AlarmControlPanelTemplate(TemplateEntity, AlarmControlPanelEntity):
self._arm_night_script = Script(hass, arm_night_action, name, DOMAIN)
self._state = None
self._unique_id = unique_id
@property
def unique_id(self):
"""Return the unique id of this alarm control panel."""
return self._unique_id
@property
def state(self):

View File

@ -196,7 +196,7 @@ class BinarySensorTemplate(TemplateEntity, BinarySensorEntity):
unique_id: str | None,
) -> None:
"""Initialize the Template binary sensor."""
super().__init__(hass, config=config)
super().__init__(hass, config=config, unique_id=unique_id)
if (object_id := config.get(CONF_OBJECT_ID)) is not None:
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, object_id, hass=hass
@ -210,7 +210,6 @@ class BinarySensorTemplate(TemplateEntity, BinarySensorEntity):
self._delay_on_raw = config.get(CONF_DELAY_ON)
self._delay_off = None
self._delay_off_raw = config.get(CONF_DELAY_OFF)
self._unique_id = unique_id
async def async_added_to_hass(self):
"""Register callbacks."""
@ -270,11 +269,6 @@ class BinarySensorTemplate(TemplateEntity, BinarySensorEntity):
# state with delay. Cancelled if template result changes.
self._delay_cancel = async_call_later(self.hass, delay, _set_state)
@property
def unique_id(self):
"""Return the unique id of this binary sensor."""
return self._unique_id
@property
def is_on(self):
"""Return true if sensor is on."""

View File

@ -85,10 +85,9 @@ class TemplateButtonEntity(TemplateEntity, ButtonEntity):
unique_id: str | None,
) -> None:
"""Initialize the button."""
super().__init__(hass, config=config)
super().__init__(hass, config=config, unique_id=unique_id)
self._command_press = Script(hass, config[CONF_PRESS], self._attr_name, DOMAIN)
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
self._attr_unique_id = unique_id
self._attr_state = None
async def async_press(self) -> None:

View File

@ -148,7 +148,9 @@ class CoverTemplate(TemplateEntity, CoverEntity):
unique_id,
):
"""Initialize the Template cover."""
super().__init__(hass, config=config, fallback_name=object_id)
super().__init__(
hass, config=config, fallback_name=object_id, unique_id=unique_id
)
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, object_id, hass=hass
)
@ -182,7 +184,6 @@ class CoverTemplate(TemplateEntity, CoverEntity):
self._is_opening = False
self._is_closing = False
self._tilt_value = None
self._unique_id = unique_id
async def async_added_to_hass(self):
"""Register callbacks."""
@ -271,11 +272,6 @@ class CoverTemplate(TemplateEntity, CoverEntity):
else:
self._tilt_value = state
@property
def unique_id(self):
"""Return the unique id of this cover."""
return self._unique_id
@property
def is_closed(self):
"""Return if the cover is closed."""

View File

@ -138,7 +138,9 @@ class TemplateFan(TemplateEntity, FanEntity):
unique_id,
):
"""Initialize the fan."""
super().__init__(hass, config=config, fallback_name=object_id)
super().__init__(
hass, config=config, fallback_name=object_id, unique_id=unique_id
)
self.hass = hass
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, object_id, hass=hass
@ -206,13 +208,6 @@ class TemplateFan(TemplateEntity, FanEntity):
if self._direction_template:
self._supported_features |= SUPPORT_DIRECTION
self._unique_id = unique_id
@property
def unique_id(self):
"""Return the unique id of this fan."""
return self._unique_id
@property
def supported_features(self) -> int:
"""Flag supported features."""

View File

@ -140,7 +140,9 @@ class LightTemplate(TemplateEntity, LightEntity):
unique_id,
):
"""Initialize the light."""
super().__init__(hass, config=config, fallback_name=object_id)
super().__init__(
hass, config=config, fallback_name=object_id, unique_id=unique_id
)
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, object_id, hass=hass
)
@ -187,7 +189,6 @@ class LightTemplate(TemplateEntity, LightEntity):
self._max_mireds = None
self._min_mireds = None
self._supports_transition = False
self._unique_id = unique_id
@property
def brightness(self):
@ -235,11 +236,6 @@ class LightTemplate(TemplateEntity, LightEntity):
"""Return the effect list."""
return self._effect_list
@property
def unique_id(self):
"""Return the unique id of this light."""
return self._unique_id
@property
def supported_features(self):
"""Flag supported features."""

View File

@ -77,25 +77,21 @@ class TemplateLock(TemplateEntity, LockEntity):
unique_id,
):
"""Initialize the lock."""
super().__init__(hass, config=config, fallback_name=DEFAULT_NAME)
super().__init__(
hass, config=config, fallback_name=DEFAULT_NAME, unique_id=unique_id
)
self._state = None
name = self._attr_name
self._state_template = config.get(CONF_VALUE_TEMPLATE)
self._command_lock = Script(hass, config[CONF_LOCK], name, DOMAIN)
self._command_unlock = Script(hass, config[CONF_UNLOCK], name, DOMAIN)
self._optimistic = config.get(CONF_OPTIMISTIC)
self._unique_id = unique_id
@property
def assumed_state(self):
"""Return true if we do optimistic updates."""
return self._optimistic
@property
def unique_id(self):
"""Return the unique id of this lock."""
return self._unique_id
@property
def is_locked(self):
"""Return true if lock is locked."""

View File

@ -107,7 +107,7 @@ class TemplateNumber(TemplateEntity, NumberEntity):
unique_id: str | None,
) -> None:
"""Initialize the number."""
super().__init__(hass, config=config)
super().__init__(hass, config=config, unique_id=unique_id)
self._value_template = config[CONF_STATE]
self._command_set_value = Script(
hass, config[CONF_SET_VALUE], self._attr_name, DOMAIN
@ -116,7 +116,6 @@ class TemplateNumber(TemplateEntity, NumberEntity):
self._min_value_template = config[ATTR_MIN]
self._max_value_template = config[ATTR_MAX]
self._attr_assumed_state = self._optimistic = config[CONF_OPTIMISTIC]
self._attr_unique_id = unique_id
self._attr_value = None
self._attr_step = None
self._attr_min_value = None

View File

@ -101,14 +101,13 @@ class TemplateSelect(TemplateEntity, SelectEntity):
unique_id: str | None,
) -> None:
"""Initialize the select."""
super().__init__(hass, config=config)
super().__init__(hass, config=config, unique_id=unique_id)
self._value_template = config[CONF_STATE]
self._command_select_option = Script(
hass, config[CONF_SELECT_OPTION], self._attr_name, DOMAIN
)
self._options_template = config[ATTR_OPTIONS]
self._attr_assumed_state = self._optimistic = config[CONF_OPTIMISTIC]
self._attr_unique_id = unique_id
self._attr_options = None
self._attr_current_option = None

View File

@ -199,7 +199,7 @@ class SensorTemplate(TemplateEntity, SensorEntity):
unique_id: str | None,
) -> None:
"""Initialize the sensor."""
super().__init__(hass, config=config)
super().__init__(hass, config=config, unique_id=unique_id)
if (object_id := config.get(CONF_OBJECT_ID)) is not None:
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, object_id, hass=hass
@ -209,7 +209,6 @@ class SensorTemplate(TemplateEntity, SensorEntity):
self._template = config.get(CONF_STATE)
self._attr_device_class = config.get(CONF_DEVICE_CLASS)
self._attr_state_class = config.get(CONF_STATE_CLASS)
self._attr_unique_id = unique_id
async def async_added_to_hass(self):
"""Register callbacks."""

View File

@ -98,7 +98,9 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
unique_id,
):
"""Initialize the Template switch."""
super().__init__(hass, config=config, fallback_name=object_id)
super().__init__(
hass, config=config, fallback_name=object_id, unique_id=unique_id
)
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, object_id, hass=hass
)
@ -107,7 +109,6 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
self._on_script = Script(hass, config[ON_ACTION], friendly_name, DOMAIN)
self._off_script = Script(hass, config[OFF_ACTION], friendly_name, DOMAIN)
self._state = False
self._unique_id = unique_id
@callback
def _update_state(self, result):
@ -143,11 +144,6 @@ class SwitchTemplate(TemplateEntity, SwitchEntity, RestoreEntity):
await super().async_added_to_hass()
@property
def unique_id(self):
"""Return the unique id of this switch."""
return self._unique_id
@property
def is_on(self):
"""Return true if device is on."""

View File

@ -222,12 +222,14 @@ class TemplateEntity(Entity):
attribute_templates=None,
config=None,
fallback_name=None,
unique_id=None,
):
"""Template Entity."""
self._template_attrs = {}
self._async_update = None
self._attr_extra_state_attributes = {}
self._self_ref_update_count = 0
self._attr_unique_id = unique_id
if config is None:
self._attribute_templates = attribute_templates
self._availability_template = availability_template

View File

@ -142,7 +142,9 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
unique_id,
):
"""Initialize the vacuum."""
super().__init__(hass, config=config, fallback_name=object_id)
super().__init__(
hass, config=config, fallback_name=object_id, unique_id=unique_id
)
self.entity_id = async_generate_entity_id(
ENTITY_ID_FORMAT, object_id, hass=hass
)
@ -200,16 +202,9 @@ class TemplateVacuum(TemplateEntity, StateVacuumEntity):
if self._battery_level_template:
self._supported_features |= SUPPORT_BATTERY
self._unique_id = unique_id
# List of valid fan speeds
self._fan_speed_list = config[CONF_FAN_SPEED_LIST]
@property
def unique_id(self):
"""Return the unique id of this vacuum."""
return self._unique_id
@property
def supported_features(self) -> int:
"""Flag supported features."""

View File

@ -112,7 +112,7 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
unique_id,
):
"""Initialize the Template weather."""
super().__init__(hass, config=config)
super().__init__(hass, config=config, unique_id=unique_id)
name = self._attr_name
self._condition_template = config[CONF_CONDITION_TEMPLATE]
@ -125,7 +125,6 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
self._ozone_template = config.get(CONF_OZONE_TEMPLATE)
self._visibility_template = config.get(CONF_VISIBILITY_TEMPLATE)
self._forecast_template = config.get(CONF_FORECAST_TEMPLATE)
self._unique_id = unique_id
self.entity_id = async_generate_entity_id(ENTITY_ID_FORMAT, name, hass=hass)
@ -197,11 +196,6 @@ class WeatherTemplate(TemplateEntity, WeatherEntity):
return "Powered by Home Assistant"
return self._attribution
@property
def unique_id(self):
"""Return the unique id of this weather instance."""
return self._unique_id
async def async_added_to_hass(self):
"""Register callbacks."""