Add preview to sensor group config and option flows (#83638)
This commit is contained in:
@@ -756,31 +756,10 @@ class Entity(ABC):
|
||||
return f"{device_name} {name}" if device_name else name
|
||||
|
||||
@callback
|
||||
def _async_write_ha_state(self) -> None:
|
||||
"""Write the state to the state machine."""
|
||||
if self._platform_state == EntityPlatformState.REMOVED:
|
||||
# Polling returned after the entity has already been removed
|
||||
return
|
||||
|
||||
hass = self.hass
|
||||
entity_id = self.entity_id
|
||||
def _async_generate_attributes(self) -> tuple[str, dict[str, Any]]:
|
||||
"""Calculate state string and attribute mapping."""
|
||||
entry = self.registry_entry
|
||||
|
||||
if entry and entry.disabled_by:
|
||||
if not self._disabled_reported:
|
||||
self._disabled_reported = True
|
||||
_LOGGER.warning(
|
||||
(
|
||||
"Entity %s is incorrectly being triggered for updates while it"
|
||||
" is disabled. This is a bug in the %s integration"
|
||||
),
|
||||
entity_id,
|
||||
self.platform.platform_name,
|
||||
)
|
||||
return
|
||||
|
||||
start = timer()
|
||||
|
||||
attr = self.capability_attributes
|
||||
attr = dict(attr) if attr else {}
|
||||
|
||||
@@ -818,6 +797,33 @@ class Entity(ABC):
|
||||
if (supported_features := self.supported_features) is not None:
|
||||
attr[ATTR_SUPPORTED_FEATURES] = supported_features
|
||||
|
||||
return (state, attr)
|
||||
|
||||
@callback
|
||||
def _async_write_ha_state(self) -> None:
|
||||
"""Write the state to the state machine."""
|
||||
if self._platform_state == EntityPlatformState.REMOVED:
|
||||
# Polling returned after the entity has already been removed
|
||||
return
|
||||
|
||||
hass = self.hass
|
||||
entity_id = self.entity_id
|
||||
|
||||
if (entry := self.registry_entry) and entry.disabled_by:
|
||||
if not self._disabled_reported:
|
||||
self._disabled_reported = True
|
||||
_LOGGER.warning(
|
||||
(
|
||||
"Entity %s is incorrectly being triggered for updates while it"
|
||||
" is disabled. This is a bug in the %s integration"
|
||||
),
|
||||
entity_id,
|
||||
self.platform.platform_name,
|
||||
)
|
||||
return
|
||||
|
||||
start = timer()
|
||||
state, attr = self._async_generate_attributes()
|
||||
end = timer()
|
||||
|
||||
if end - start > 0.4 and not self._slow_reported:
|
||||
|
||||
@@ -78,6 +78,9 @@ class SchemaFlowFormStep(SchemaFlowStep):
|
||||
have priority over the suggested values.
|
||||
"""
|
||||
|
||||
preview: str | None = None
|
||||
"""Optional preview component."""
|
||||
|
||||
|
||||
@dataclass(slots=True)
|
||||
class SchemaFlowMenuStep(SchemaFlowStep):
|
||||
@@ -237,6 +240,7 @@ class SchemaCommonFlowHandler:
|
||||
data_schema=data_schema,
|
||||
errors=errors,
|
||||
last_step=last_step,
|
||||
preview=form_step.preview,
|
||||
)
|
||||
|
||||
async def _async_menu_step(
|
||||
@@ -271,7 +275,10 @@ class SchemaConfigFlowHandler(config_entries.ConfigFlow, ABC):
|
||||
raise UnknownHandler
|
||||
|
||||
return SchemaOptionsFlowHandler(
|
||||
config_entry, cls.options_flow, cls.async_options_flow_finished
|
||||
config_entry,
|
||||
cls.options_flow,
|
||||
cls.async_options_flow_finished,
|
||||
cls.async_setup_preview,
|
||||
)
|
||||
|
||||
# Create an async_get_options_flow method
|
||||
@@ -285,6 +292,11 @@ class SchemaConfigFlowHandler(config_entries.ConfigFlow, ABC):
|
||||
"""Initialize config flow."""
|
||||
self._common_handler = SchemaCommonFlowHandler(self, self.config_flow, None)
|
||||
|
||||
@callback
|
||||
@staticmethod
|
||||
def async_setup_preview(hass: HomeAssistant) -> None:
|
||||
"""Set up preview."""
|
||||
|
||||
@classmethod
|
||||
@callback
|
||||
def async_supports_options_flow(
|
||||
@@ -357,6 +369,7 @@ class SchemaOptionsFlowHandler(config_entries.OptionsFlowWithConfigEntry):
|
||||
options_flow: Mapping[str, SchemaFlowStep],
|
||||
async_options_flow_finished: Callable[[HomeAssistant, Mapping[str, Any]], None]
|
||||
| None = None,
|
||||
async_setup_preview: Callable[[HomeAssistant], None] | None = None,
|
||||
) -> None:
|
||||
"""Initialize options flow.
|
||||
|
||||
@@ -378,6 +391,9 @@ class SchemaOptionsFlowHandler(config_entries.OptionsFlowWithConfigEntry):
|
||||
types.MethodType(self._async_step(step), self),
|
||||
)
|
||||
|
||||
if async_setup_preview:
|
||||
setattr(self, "async_setup_preview", async_setup_preview)
|
||||
|
||||
@staticmethod
|
||||
def _async_step(step_id: str) -> Callable:
|
||||
"""Generate a step handler."""
|
||||
|
||||
Reference in New Issue
Block a user