mirror of
https://github.com/gangoke/kobrax-lan-hass-component.git
synced 2026-06-10 05:02:12 +02:00
All entites working - Moved all existing functionality from custom_components/kobrax to custom_components/kobrax_lan. - Updated manifest, const, and configuration flow to reflect the new component structure. - Reimplemented API client, coordinator, entity, and platform files under the new component. - Added support for image entities and binary sensors. - Ensured compatibility with Home Assistant's latest standards and practices.
87 lines
2.7 KiB
Python
87 lines
2.7 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
|
|
from homeassistant.exceptions import ServiceValidationError
|
|
|
|
from .api import KobraXApiError
|
|
from .const import DOMAIN
|
|
from .entity import KobraXEntity
|
|
|
|
|
|
@dataclass(frozen=True, kw_only=True)
|
|
class KobraXButtonDescription(ButtonEntityDescription):
|
|
action: str
|
|
|
|
|
|
BUTTONS: tuple[KobraXButtonDescription, ...] = (
|
|
KobraXButtonDescription(
|
|
key="pause_print",
|
|
name="Pause Print",
|
|
icon="mdi:pause",
|
|
action="pause",
|
|
),
|
|
KobraXButtonDescription(
|
|
key="resume_print",
|
|
name="Resume Print",
|
|
icon="mdi:play",
|
|
action="resume",
|
|
),
|
|
KobraXButtonDescription(
|
|
key="cancel_print",
|
|
name="Cancel Print",
|
|
icon="mdi:stop",
|
|
action="cancel",
|
|
),
|
|
KobraXButtonDescription(
|
|
key="connect",
|
|
name="Connect Bridge",
|
|
icon="mdi:lan-connect",
|
|
action="connect",
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
KobraXButtonDescription(
|
|
key="disconnect",
|
|
name="Disconnect Bridge",
|
|
icon="mdi:lan-disconnect",
|
|
action="disconnect",
|
|
entity_registry_enabled_default=False,
|
|
),
|
|
)
|
|
|
|
|
|
class KobraXActionButton(KobraXEntity, ButtonEntity):
|
|
entity_description: KobraXButtonDescription
|
|
|
|
def __init__(self, coordinator, entry, description: KobraXButtonDescription) -> None:
|
|
super().__init__(coordinator, entry, description.key, description.name)
|
|
self.entity_description = description
|
|
|
|
async def async_press(self) -> None:
|
|
api = self.hass.data[DOMAIN][self._entry.entry_id]["api"]
|
|
try:
|
|
if self.entity_description.action == "pause":
|
|
await api.async_pause_print()
|
|
elif self.entity_description.action == "resume":
|
|
await api.async_resume_print()
|
|
elif self.entity_description.action == "cancel":
|
|
await api.async_cancel_print()
|
|
elif self.entity_description.action == "connect":
|
|
await api.async_connect()
|
|
elif self.entity_description.action == "disconnect":
|
|
await api.async_disconnect()
|
|
await self.coordinator.async_request_refresh()
|
|
except KobraXApiError as err:
|
|
raise ServiceValidationError(str(err)) from err
|
|
|
|
|
|
async def async_setup_entry(hass, entry, async_add_entities):
|
|
coordinator = hass.data[DOMAIN][entry.entry_id]["coordinator"]
|
|
async_add_entities(
|
|
[
|
|
KobraXActionButton(coordinator, entry, description)
|
|
for description in BUTTONS
|
|
]
|
|
)
|