Implement sync time button for moehlenhoff_alpha2 (#85676)

* Implement sync time button for moehlenhoff_alpha2

* fix outdated doc comment

Co-authored-by: j-a-n <oss@janschneider.net>

* address review comments

Co-authored-by: j-a-n <oss@janschneider.net>
This commit is contained in:
Jovan Gerodetti 2023-01-12 22:09:14 +01:00 committed by GitHub
parent cb04a52220
commit 49031b8fa7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 1 deletions

View File

@ -17,7 +17,7 @@ from .const import DOMAIN
_LOGGER = logging.getLogger(__name__)
PLATFORMS = [Platform.CLIMATE, Platform.SENSOR, Platform.BINARY_SENSOR]
PLATFORMS = [Platform.BUTTON, Platform.CLIMATE, Platform.SENSOR, Platform.BINARY_SENSOR]
UPDATE_INTERVAL = timedelta(seconds=60)

View File

@ -0,0 +1,41 @@
"""Button entity to set the time of the Alpha2 base."""
from homeassistant.components.button import ButtonEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.util import dt
from . import Alpha2BaseCoordinator
from .const import DOMAIN
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Add Alpha2 button entities."""
coordinator: Alpha2BaseCoordinator = hass.data[DOMAIN][config_entry.entry_id]
async_add_entities([Alpha2TimeSyncButton(coordinator, config_entry.entry_id)])
class Alpha2TimeSyncButton(CoordinatorEntity[Alpha2BaseCoordinator], ButtonEntity):
"""Alpha2 virtual time sync button."""
_attr_name = "Sync time"
_attr_entity_category = EntityCategory.DIAGNOSTIC
def __init__(self, coordinator: Alpha2BaseCoordinator, entry_id: str) -> None:
"""Initialize Alpha2TimeSyncButton."""
super().__init__(coordinator)
self._attr_unique_id = f"{entry_id}:sync_time"
async def async_press(self) -> None:
"""Synchronize current local time from HA instance to base station."""
await self.coordinator.base.set_datetime(dt.now())