Use shorthand attr for mqtt assumed_state (#100241)
This commit is contained in:
parent
eb0ab3de93
commit
e3837cd1e0
13 changed files with 26 additions and 78 deletions
|
@ -294,6 +294,7 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||
):
|
||||
# Force into optimistic mode.
|
||||
self._optimistic = True
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
|
||||
if (
|
||||
config[CONF_TILT_STATE_OPTIMISTIC]
|
||||
|
@ -488,11 +489,6 @@ class MqttCover(MqttEntity, CoverEntity):
|
|||
"""(Re)Subscribe to topics."""
|
||||
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return bool(self._optimistic)
|
||||
|
||||
@property
|
||||
def is_closed(self) -> bool | None:
|
||||
"""Return true if the cover is closed or None if the status is unknown."""
|
||||
|
|
|
@ -295,6 +295,7 @@ class MqttFan(MqttEntity, FanEntity):
|
|||
|
||||
optimistic = config[CONF_OPTIMISTIC]
|
||||
self._optimistic = optimistic or self._topic[CONF_STATE_TOPIC] is None
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
self._optimistic_direction = (
|
||||
optimistic or self._topic[CONF_DIRECTION_STATE_TOPIC] is None
|
||||
)
|
||||
|
@ -491,11 +492,6 @@ class MqttFan(MqttEntity, FanEntity):
|
|||
"""(Re)Subscribe to topics."""
|
||||
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool | None:
|
||||
"""Return true if device is on."""
|
||||
|
|
|
@ -260,6 +260,7 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
|||
|
||||
optimistic: bool = config[CONF_OPTIMISTIC]
|
||||
self._optimistic = optimistic or self._topic[CONF_STATE_TOPIC] is None
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
self._optimistic_target_humidity = (
|
||||
optimistic or self._topic[CONF_TARGET_HUMIDITY_STATE_TOPIC] is None
|
||||
)
|
||||
|
@ -465,11 +466,6 @@ class MqttHumidifier(MqttEntity, HumidifierEntity):
|
|||
"""(Re)Subscribe to topics."""
|
||||
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn on the entity.
|
||||
|
||||
|
|
|
@ -113,7 +113,6 @@ class MqttLawnMower(MqttEntity, LawnMowerEntity, RestoreEntity):
|
|||
_command_templates: dict[str, Callable[[PublishPayloadType], PublishPayloadType]]
|
||||
_command_topics: dict[str, str]
|
||||
_value_template: Callable[[ReceivePayloadType], ReceivePayloadType]
|
||||
_optimistic: bool = False
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
|
@ -134,7 +133,7 @@ class MqttLawnMower(MqttEntity, LawnMowerEntity, RestoreEntity):
|
|||
|
||||
def _setup_from_config(self, config: ConfigType) -> None:
|
||||
"""(Re)Setup the entity."""
|
||||
self._optimistic = config[CONF_OPTIMISTIC]
|
||||
self._attr_assumed_state = config[CONF_OPTIMISTIC]
|
||||
|
||||
self._value_template = MqttValueTemplate(
|
||||
config.get(CONF_ACTIVITY_VALUE_TEMPLATE), entity=self
|
||||
|
@ -198,7 +197,7 @@ class MqttLawnMower(MqttEntity, LawnMowerEntity, RestoreEntity):
|
|||
|
||||
if self._config.get(CONF_ACTIVITY_STATE_TOPIC) is None:
|
||||
# Force into optimistic mode.
|
||||
self._optimistic = True
|
||||
self._attr_assumed_state = True
|
||||
else:
|
||||
self._sub_state = subscription.async_prepare_subscribe_topics(
|
||||
self.hass,
|
||||
|
@ -217,19 +216,16 @@ class MqttLawnMower(MqttEntity, LawnMowerEntity, RestoreEntity):
|
|||
"""(Re)Subscribe to topics."""
|
||||
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
||||
|
||||
if self._optimistic and (last_state := await self.async_get_last_state()):
|
||||
if self._attr_assumed_state and (
|
||||
last_state := await self.async_get_last_state()
|
||||
):
|
||||
with contextlib.suppress(ValueError):
|
||||
self._attr_activity = LawnMowerActivity(last_state.state)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
||||
async def _async_operate(self, option: str, activity: LawnMowerActivity) -> None:
|
||||
"""Execute operation."""
|
||||
payload = self._command_templates[option](option)
|
||||
if self._optimistic:
|
||||
if self._attr_assumed_state:
|
||||
self._attr_activity = activity
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
|
|
@ -330,6 +330,7 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||
optimistic or topic[CONF_COLOR_MODE_STATE_TOPIC] is None
|
||||
)
|
||||
self._optimistic = optimistic or topic[CONF_STATE_TOPIC] is None
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
self._optimistic_rgb_color = optimistic or topic[CONF_RGB_STATE_TOPIC] is None
|
||||
self._optimistic_rgbw_color = optimistic or topic[CONF_RGBW_STATE_TOPIC] is None
|
||||
self._optimistic_rgbww_color = (
|
||||
|
@ -668,11 +669,6 @@ class MqttLight(MqttEntity, LightEntity, RestoreEntity):
|
|||
restore_state(ATTR_XY_COLOR)
|
||||
restore_state(ATTR_HS_COLOR, ATTR_XY_COLOR)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None: # noqa: C901
|
||||
"""Turn the device on.
|
||||
|
||||
|
|
|
@ -215,6 +215,7 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
|
|||
}
|
||||
optimistic: bool = config[CONF_OPTIMISTIC]
|
||||
self._optimistic = optimistic or self._topic[CONF_STATE_TOPIC] is None
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
|
||||
self._flash_times = {
|
||||
key: config.get(key)
|
||||
|
@ -462,11 +463,6 @@ class MqttLightJson(MqttEntity, LightEntity, RestoreEntity):
|
|||
)
|
||||
self._attr_xy_color = last_attributes.get(ATTR_XY_COLOR, self.xy_color)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
||||
@property
|
||||
def color_mode(self) -> ColorMode | str | None:
|
||||
"""Return current color mode."""
|
||||
|
|
|
@ -179,6 +179,7 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity):
|
|||
or self._topics[CONF_STATE_TOPIC] is None
|
||||
or CONF_STATE_TEMPLATE not in self._config
|
||||
)
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
|
||||
color_modes = {ColorMode.ONOFF}
|
||||
if CONF_BRIGHTNESS_TEMPLATE in config:
|
||||
|
@ -315,11 +316,6 @@ class MqttLightTemplate(MqttEntity, LightEntity, RestoreEntity):
|
|||
if last_state.attributes.get(ATTR_EFFECT):
|
||||
self._attr_effect = last_state.attributes.get(ATTR_EFFECT)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return True if unable to access real state of the entity."""
|
||||
return self._optimistic
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the entity on.
|
||||
|
||||
|
|
|
@ -159,6 +159,7 @@ class MqttLock(MqttEntity, LockEntity):
|
|||
self._optimistic = (
|
||||
config[CONF_OPTIMISTIC] or self._config.get(CONF_STATE_TOPIC) is None
|
||||
)
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
|
||||
self._compiled_pattern = config.get(CONF_CODE_FORMAT)
|
||||
self._attr_code_format = (
|
||||
|
@ -221,11 +222,6 @@ class MqttLock(MqttEntity, LockEntity):
|
|||
"""(Re)Subscribe to topics."""
|
||||
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
||||
async def async_lock(self, **kwargs: Any) -> None:
|
||||
"""Lock the device.
|
||||
|
||||
|
|
|
@ -161,7 +161,7 @@ class MqttNumber(MqttEntity, RestoreNumber):
|
|||
def _setup_from_config(self, config: ConfigType) -> None:
|
||||
"""(Re)Setup the entity."""
|
||||
self._config = config
|
||||
self._optimistic = config[CONF_OPTIMISTIC]
|
||||
self._attr_assumed_state = config[CONF_OPTIMISTIC]
|
||||
|
||||
self._command_template = MqttCommandTemplate(
|
||||
config.get(CONF_COMMAND_TEMPLATE), entity=self
|
||||
|
@ -218,7 +218,7 @@ class MqttNumber(MqttEntity, RestoreNumber):
|
|||
|
||||
if self._config.get(CONF_STATE_TOPIC) is None:
|
||||
# Force into optimistic mode.
|
||||
self._optimistic = True
|
||||
self._attr_assumed_state = True
|
||||
else:
|
||||
self._sub_state = subscription.async_prepare_subscribe_topics(
|
||||
self.hass,
|
||||
|
@ -237,7 +237,7 @@ class MqttNumber(MqttEntity, RestoreNumber):
|
|||
"""(Re)Subscribe to topics."""
|
||||
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
||||
|
||||
if self._optimistic and (
|
||||
if self._attr_assumed_state and (
|
||||
last_number_data := await self.async_get_last_number_data()
|
||||
):
|
||||
self._attr_native_value = last_number_data.native_value
|
||||
|
@ -250,7 +250,7 @@ class MqttNumber(MqttEntity, RestoreNumber):
|
|||
current_number = int(value)
|
||||
payload = self._command_template(current_number)
|
||||
|
||||
if self._optimistic:
|
||||
if self._attr_assumed_state:
|
||||
self._attr_native_value = current_number
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
@ -261,8 +261,3 @@ class MqttNumber(MqttEntity, RestoreNumber):
|
|||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
|
|
@ -115,7 +115,7 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity):
|
|||
|
||||
def _setup_from_config(self, config: ConfigType) -> None:
|
||||
"""(Re)Setup the entity."""
|
||||
self._optimistic = config[CONF_OPTIMISTIC]
|
||||
self._attr_assumed_state = config[CONF_OPTIMISTIC]
|
||||
self._attr_options = config[CONF_OPTIONS]
|
||||
|
||||
self._command_template = MqttCommandTemplate(
|
||||
|
@ -152,7 +152,7 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity):
|
|||
|
||||
if self._config.get(CONF_STATE_TOPIC) is None:
|
||||
# Force into optimistic mode.
|
||||
self._optimistic = True
|
||||
self._attr_assumed_state = True
|
||||
else:
|
||||
self._sub_state = subscription.async_prepare_subscribe_topics(
|
||||
self.hass,
|
||||
|
@ -171,13 +171,15 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity):
|
|||
"""(Re)Subscribe to topics."""
|
||||
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
||||
|
||||
if self._optimistic and (last_state := await self.async_get_last_state()):
|
||||
if self._attr_assumed_state and (
|
||||
last_state := await self.async_get_last_state()
|
||||
):
|
||||
self._attr_current_option = last_state.state
|
||||
|
||||
async def async_select_option(self, option: str) -> None:
|
||||
"""Update the current value."""
|
||||
payload = self._command_template(option)
|
||||
if self._optimistic:
|
||||
if self._attr_assumed_state:
|
||||
self._attr_current_option = option
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
@ -188,8 +190,3 @@ class MqttSelect(MqttEntity, SelectEntity, RestoreEntity):
|
|||
self._config[CONF_RETAIN],
|
||||
self._config[CONF_ENCODING],
|
||||
)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
|
|
@ -194,6 +194,7 @@ class MqttSiren(MqttEntity, SirenEntity):
|
|||
|
||||
self._attr_supported_features = _supported_features
|
||||
self._optimistic = config[CONF_OPTIMISTIC] or CONF_STATE_TOPIC not in config
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
self._attr_is_on = False if self._optimistic else None
|
||||
|
||||
command_template: Template | None = config.get(CONF_COMMAND_TEMPLATE)
|
||||
|
@ -301,11 +302,6 @@ class MqttSiren(MqttEntity, SirenEntity):
|
|||
"""(Re)Subscribe to topics."""
|
||||
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||
"""Return the state attributes."""
|
||||
|
|
|
@ -125,6 +125,7 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
|||
self._optimistic = (
|
||||
config[CONF_OPTIMISTIC] or config.get(CONF_STATE_TOPIC) is None
|
||||
)
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
|
||||
self._value_template = MqttValueTemplate(
|
||||
self._config.get(CONF_VALUE_TEMPLATE), entity=self
|
||||
|
@ -171,11 +172,6 @@ class MqttSwitch(MqttEntity, SwitchEntity, RestoreEntity):
|
|||
if self._optimistic and (last_state := await self.async_get_last_state()):
|
||||
self._attr_is_on = last_state.state == STATE_ON
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the device on.
|
||||
|
||||
|
|
|
@ -169,6 +169,7 @@ class MqttTextEntity(MqttEntity, TextEntity):
|
|||
).async_render_with_possible_json_value
|
||||
optimistic: bool = config[CONF_OPTIMISTIC]
|
||||
self._optimistic = optimistic or config.get(CONF_STATE_TOPIC) is None
|
||||
self._attr_assumed_state = bool(self._optimistic)
|
||||
|
||||
def _prepare_subscribe_topics(self) -> None:
|
||||
"""(Re)Subscribe to topics."""
|
||||
|
@ -203,11 +204,6 @@ class MqttTextEntity(MqttEntity, TextEntity):
|
|||
"""(Re)Subscribe to topics."""
|
||||
await subscription.async_subscribe_topics(self.hass, self._sub_state)
|
||||
|
||||
@property
|
||||
def assumed_state(self) -> bool:
|
||||
"""Return true if we do optimistic updates."""
|
||||
return self._optimistic
|
||||
|
||||
async def async_set_value(self, value: str) -> None:
|
||||
"""Change the text."""
|
||||
payload = self._command_template(value)
|
||||
|
|
Loading…
Add table
Reference in a new issue