mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00

* Support DPTComplex objects and validate sensor types * Gracefully start and stop xknx device objects * Use non-awaitable XknxDevice callbacks * Use non-awaitable xknx.TelegramQueue callbacks * Use non-awaitable xknx.ConnectionManager callbacks * Remove unnecessary `hass.async_block_till_done()` calls * Wait for StateUpdater logic to proceed when receiving responses * Update import module paths for specific DPTs * Support Enum data types * New HVAC mode names * HVAC Enums instead of Enum member value strings * New date and time devices * Update xknx to 3.0.0 * Fix expose tests and DPTEnumData check * ruff and mypy fixes
95 lines
2.9 KiB
Python
95 lines
2.9 KiB
Python
"""Support for KNX/IP time."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import time as dt_time
|
|
from typing import Final
|
|
|
|
from xknx import XKNX
|
|
from xknx.devices import TimeDevice as XknxTimeDevice
|
|
from xknx.dpt.dpt_10 import KNXTime as XknxTime
|
|
|
|
from homeassistant import config_entries
|
|
from homeassistant.components.time import TimeEntity
|
|
from homeassistant.const import (
|
|
CONF_ENTITY_CATEGORY,
|
|
CONF_NAME,
|
|
STATE_UNAVAILABLE,
|
|
STATE_UNKNOWN,
|
|
Platform,
|
|
)
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.restore_state import RestoreEntity
|
|
from homeassistant.helpers.typing import ConfigType
|
|
|
|
from .const import (
|
|
CONF_RESPOND_TO_READ,
|
|
CONF_STATE_ADDRESS,
|
|
CONF_SYNC_STATE,
|
|
DATA_KNX_CONFIG,
|
|
DOMAIN,
|
|
KNX_ADDRESS,
|
|
)
|
|
from .knx_entity import KnxEntity
|
|
|
|
_TIME_TRANSLATION_FORMAT: Final = "%H:%M:%S"
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: config_entries.ConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Set up entities for KNX platform."""
|
|
xknx: XKNX = hass.data[DOMAIN].xknx
|
|
config: list[ConfigType] = hass.data[DATA_KNX_CONFIG][Platform.TIME]
|
|
|
|
async_add_entities(KNXTimeEntity(xknx, entity_config) for entity_config in config)
|
|
|
|
|
|
def _create_xknx_device(xknx: XKNX, config: ConfigType) -> XknxTimeDevice:
|
|
"""Return a XKNX DateTime object to be used within XKNX."""
|
|
return XknxTimeDevice(
|
|
xknx,
|
|
name=config[CONF_NAME],
|
|
localtime=False,
|
|
group_address=config[KNX_ADDRESS],
|
|
group_address_state=config.get(CONF_STATE_ADDRESS),
|
|
respond_to_read=config[CONF_RESPOND_TO_READ],
|
|
sync_state=config[CONF_SYNC_STATE],
|
|
)
|
|
|
|
|
|
class KNXTimeEntity(KnxEntity, TimeEntity, RestoreEntity):
|
|
"""Representation of a KNX time."""
|
|
|
|
_device: XknxTimeDevice
|
|
|
|
def __init__(self, xknx: XKNX, config: ConfigType) -> None:
|
|
"""Initialize a KNX time."""
|
|
super().__init__(_create_xknx_device(xknx, config))
|
|
self._attr_entity_category = config.get(CONF_ENTITY_CATEGORY)
|
|
self._attr_unique_id = str(self._device.remote_value.group_address)
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
"""Restore last state."""
|
|
await super().async_added_to_hass()
|
|
if (
|
|
not self._device.remote_value.readable
|
|
and (last_state := await self.async_get_last_state()) is not None
|
|
and last_state.state not in (STATE_UNKNOWN, STATE_UNAVAILABLE)
|
|
):
|
|
self._device.remote_value.value = XknxTime.from_time(
|
|
dt_time.fromisoformat(last_state.state)
|
|
)
|
|
|
|
@property
|
|
def native_value(self) -> dt_time | None:
|
|
"""Return the latest value."""
|
|
return self._device.value
|
|
|
|
async def async_set_value(self, value: dt_time) -> None:
|
|
"""Change the value."""
|
|
await self._device.set(value)
|