From c406e1aeede3f747ea2149d16f5b516b9fa4f6ef Mon Sep 17 00:00:00 2001 From: Glenn Waters Date: Thu, 7 May 2026 14:17:42 -0400 Subject: [PATCH] ElkM1 integration: Add time entity for settings (#170035) --- homeassistant/components/elkm1/__init__.py | 1 + homeassistant/components/elkm1/time.py | 66 ++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 homeassistant/components/elkm1/time.py diff --git a/homeassistant/components/elkm1/__init__.py b/homeassistant/components/elkm1/__init__.py index 827da6effa7..712e5206f41 100644 --- a/homeassistant/components/elkm1/__init__.py +++ b/homeassistant/components/elkm1/__init__.py @@ -76,6 +76,7 @@ PLATFORMS = [ Platform.SCENE, Platform.SENSOR, Platform.SWITCH, + Platform.TIME, ] diff --git a/homeassistant/components/elkm1/time.py b/homeassistant/components/elkm1/time.py new file mode 100644 index 00000000000..b7453cd231c --- /dev/null +++ b/homeassistant/components/elkm1/time.py @@ -0,0 +1,66 @@ +"""Support for ElkM1 time entities.""" + +from datetime import time as dt_time +import logging +from typing import Any, cast + +from elkm1_lib.const import SettingFormat +from elkm1_lib.elements import Element +from elkm1_lib.settings import Setting + +from homeassistant.components.time import TimeEntity +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from . import ElkM1ConfigEntry +from .entity import ElkAttachedEntity, ElkEntity, create_elk_entities + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: ElkM1ConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the Elk-M1 time platform.""" + elk_data = config_entry.runtime_data + elk = elk_data.elk + entities: list[ElkEntity] = [] + time_settings = [ + setting + for setting in cast(list[Setting], elk.settings) + if setting.value_format == SettingFormat.TIME_OF_DAY + ] + + create_elk_entities( + elk_data, + time_settings, + "setting", + ElkTimeSetting, + entities, + ) + async_add_entities(entities) + + +class ElkTimeSetting(ElkAttachedEntity, TimeEntity): + """Representation of an Elk-M1 Time Setting.""" + + _element: Setting + + def _element_changed(self, element: Element, changeset: dict[str, Any]) -> None: + value = self._element.value + # Guard against the panel possibly changing the underlying + # type without us knowing about the change + if isinstance(value, tuple): + self._attr_native_value = dt_time(hour=value[0], minute=value[1]) + else: + self._attr_available = False + _LOGGER.warning( + "Setting type for '%s' differs between the ElkM1 and the entity. Restart the integration to fix", + self.entity_id, + ) + + async def async_set_value(self, value: dt_time) -> None: + """Set the time of the setting.""" + self._element.set((value.hour, value.minute))