2021-11-11 06:22:52 +00:00
|
|
|
"""Support for WLED button."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-11-30 11:12:08 +00:00
|
|
|
from homeassistant.components.button import ButtonDeviceClass, ButtonEntity
|
2021-11-11 06:22:52 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-18 15:17:55 +00:00
|
|
|
from homeassistant.helpers.entity import EntityCategory
|
2021-11-11 06:22:52 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .coordinator import WLEDDataUpdateCoordinator
|
|
|
|
from .helpers import wled_exception_handler
|
|
|
|
from .models import WLEDEntity
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up WLED button based on a config entry."""
|
|
|
|
coordinator: WLEDDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
|
2021-11-16 18:58:04 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
WLEDRestartButton(coordinator),
|
2021-11-30 11:12:08 +00:00
|
|
|
WLEDUpdateButton(coordinator),
|
2021-11-16 18:58:04 +00:00
|
|
|
]
|
|
|
|
)
|
2021-11-11 06:22:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WLEDRestartButton(WLEDEntity, ButtonEntity):
|
2021-11-16 18:58:04 +00:00
|
|
|
"""Defines a WLED restart button."""
|
2021-11-11 06:22:52 +00:00
|
|
|
|
2021-11-30 11:12:08 +00:00
|
|
|
_attr_device_class = ButtonDeviceClass.RESTART
|
2021-12-18 15:17:55 +00:00
|
|
|
_attr_entity_category = EntityCategory.CONFIG
|
2021-11-11 06:22:52 +00:00
|
|
|
|
|
|
|
def __init__(self, coordinator: WLEDDataUpdateCoordinator) -> None:
|
|
|
|
"""Initialize the button entity."""
|
|
|
|
super().__init__(coordinator=coordinator)
|
|
|
|
self._attr_name = f"{coordinator.data.info.name} Restart"
|
|
|
|
self._attr_unique_id = f"{coordinator.data.info.mac_address}_restart"
|
|
|
|
|
|
|
|
@wled_exception_handler
|
|
|
|
async def async_press(self) -> None:
|
|
|
|
"""Send out a restart command."""
|
|
|
|
await self.coordinator.wled.reset()
|
2021-11-16 18:58:04 +00:00
|
|
|
|
|
|
|
|
2021-11-30 11:12:08 +00:00
|
|
|
class WLEDUpdateButton(WLEDEntity, ButtonEntity):
|
|
|
|
"""Defines a WLED update button."""
|
2021-11-16 18:58:04 +00:00
|
|
|
|
2021-11-30 11:12:08 +00:00
|
|
|
_attr_device_class = ButtonDeviceClass.UPDATE
|
2021-12-18 15:17:55 +00:00
|
|
|
_attr_entity_category = EntityCategory.CONFIG
|
2021-11-16 18:58:04 +00:00
|
|
|
|
|
|
|
def __init__(self, coordinator: WLEDDataUpdateCoordinator) -> None:
|
|
|
|
"""Initialize the button entity."""
|
|
|
|
super().__init__(coordinator=coordinator)
|
2021-11-30 11:12:08 +00:00
|
|
|
self._attr_name = f"{coordinator.data.info.name} Update"
|
|
|
|
self._attr_unique_id = f"{coordinator.data.info.mac_address}_update"
|
2021-11-16 18:58:04 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
2021-11-30 11:12:08 +00:00
|
|
|
"""Return if the entity and an update is available."""
|
2021-11-16 18:58:04 +00:00
|
|
|
current = self.coordinator.data.info.version
|
|
|
|
beta = self.coordinator.data.info.version_latest_beta
|
|
|
|
stable = self.coordinator.data.info.version_latest_stable
|
|
|
|
|
|
|
|
# If we already run a pre-release, allow upgrading to a newer
|
|
|
|
# pre-release offer a normal upgrade otherwise.
|
|
|
|
return (
|
|
|
|
super().available
|
|
|
|
and current is not None
|
|
|
|
and (
|
|
|
|
(stable is not None and stable > current)
|
|
|
|
or (
|
|
|
|
beta is not None
|
|
|
|
and (current.alpha or current.beta or current.release_candidate)
|
|
|
|
and beta > current
|
|
|
|
)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
@wled_exception_handler
|
|
|
|
async def async_press(self) -> None:
|
2021-11-30 11:12:08 +00:00
|
|
|
"""Send out a update command."""
|
2021-11-16 18:58:04 +00:00
|
|
|
current = self.coordinator.data.info.version
|
|
|
|
beta = self.coordinator.data.info.version_latest_beta
|
|
|
|
stable = self.coordinator.data.info.version_latest_stable
|
|
|
|
|
2021-11-30 11:12:08 +00:00
|
|
|
# If we already run a pre-release, allow update to a newer
|
|
|
|
# pre-release or newer stable, otherwise, offer a normal stable updates.
|
2021-11-16 18:58:04 +00:00
|
|
|
version = stable
|
|
|
|
if (
|
|
|
|
current is not None
|
|
|
|
and beta is not None
|
|
|
|
and (current.alpha or current.beta or current.release_candidate)
|
|
|
|
and beta > current
|
|
|
|
and beta > stable
|
|
|
|
):
|
|
|
|
version = beta
|
|
|
|
|
|
|
|
await self.coordinator.wled.upgrade(version=str(version))
|