mirror of
https://github.com/home-assistant/core.git
synced 2026-08-14 01:02:51 +01:00
Add button availability to Portainer containers (#178400)
This commit is contained in:
@@ -6,7 +6,7 @@ from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
from typing import Any, override
|
||||
|
||||
from pyportainer import Portainer
|
||||
from pyportainer import DockerContainerState, Portainer
|
||||
from pyportainer.exceptions import (
|
||||
PortainerAuthenticationError,
|
||||
PortainerConnectionError,
|
||||
@@ -54,6 +54,7 @@ class PortainerContainerButtonDescription(ButtonEntityDescription):
|
||||
[Portainer, int, str],
|
||||
Coroutine[Any, Any, DockerContainer | None],
|
||||
]
|
||||
available_fn: Callable[[PortainerContainerData], bool]
|
||||
|
||||
|
||||
ENDPOINT_BUTTONS: tuple[PortainerEndpointButtonDescription, ...] = (
|
||||
@@ -89,6 +90,9 @@ CONTAINER_BUTTONS: tuple[PortainerContainerButtonDescription, ...] = (
|
||||
endpoint_id, container_id
|
||||
)
|
||||
),
|
||||
available_fn=lambda container: (
|
||||
container.container.state != DockerContainerState.PAUSED
|
||||
),
|
||||
),
|
||||
PortainerContainerButtonDescription(
|
||||
key="pause",
|
||||
@@ -99,6 +103,9 @@ CONTAINER_BUTTONS: tuple[PortainerContainerButtonDescription, ...] = (
|
||||
endpoint_id, container_id
|
||||
)
|
||||
),
|
||||
available_fn=lambda container: (
|
||||
container.container.state == DockerContainerState.RUNNING
|
||||
),
|
||||
),
|
||||
PortainerContainerButtonDescription(
|
||||
key="resume",
|
||||
@@ -109,6 +116,9 @@ CONTAINER_BUTTONS: tuple[PortainerContainerButtonDescription, ...] = (
|
||||
endpoint_id, container_id
|
||||
)
|
||||
),
|
||||
available_fn=lambda container: (
|
||||
container.container.state == DockerContainerState.PAUSED
|
||||
),
|
||||
),
|
||||
PortainerContainerButtonDescription(
|
||||
key="recreate",
|
||||
@@ -122,6 +132,10 @@ CONTAINER_BUTTONS: tuple[PortainerContainerButtonDescription, ...] = (
|
||||
pull_image=True,
|
||||
)
|
||||
),
|
||||
available_fn=lambda container: (
|
||||
container.container.state
|
||||
not in (DockerContainerState.REMOVING, DockerContainerState.DEAD)
|
||||
),
|
||||
),
|
||||
PortainerContainerButtonDescription(
|
||||
key="kill",
|
||||
@@ -132,6 +146,10 @@ CONTAINER_BUTTONS: tuple[PortainerContainerButtonDescription, ...] = (
|
||||
endpoint_id, container_id
|
||||
)
|
||||
),
|
||||
available_fn=lambda container: (
|
||||
container.container.state
|
||||
in (DockerContainerState.RUNNING, DockerContainerState.PAUSED)
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -244,6 +262,14 @@ class PortainerContainerButton(PortainerContainerEntity, PortainerBaseButton):
|
||||
|
||||
entity_description: PortainerContainerButtonDescription
|
||||
|
||||
@property
|
||||
@override
|
||||
def available(self) -> bool:
|
||||
"""Return if the button is available."""
|
||||
return super().available and self.entity_description.available_fn(
|
||||
self.container_data
|
||||
)
|
||||
|
||||
@override
|
||||
async def _async_press_call(self) -> None:
|
||||
"""Call the container button press action."""
|
||||
|
||||
@@ -247,7 +247,7 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
'state': 'unavailable',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_button_entities_snapshot[button.focused_einstein_kill_container-entry]
|
||||
@@ -498,7 +498,7 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
'state': 'unavailable',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_button_entities_snapshot[button.funny_chatelet_kill_container-entry]
|
||||
@@ -749,7 +749,7 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
'state': 'unavailable',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_button_entities_snapshot[button.my_environment_prune_unused_images-entry]
|
||||
@@ -1101,7 +1101,7 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
'state': 'unavailable',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_button_entities_snapshot[button.serene_banach_kill_container-entry]
|
||||
@@ -1352,7 +1352,7 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
'state': 'unavailable',
|
||||
})
|
||||
# ---
|
||||
# name: test_all_button_entities_snapshot[button.stoic_turing_kill_container-entry]
|
||||
@@ -1603,6 +1603,6 @@
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
'state': 'unavailable',
|
||||
})
|
||||
# ---
|
||||
|
||||
Reference in New Issue
Block a user