ESPHome: Add Time Entity support (#113852)

This commit is contained in:
Jesse Hills 2024-03-21 16:59:43 +13:00 committed by GitHub
parent aebc95b1d2
commit 6ddef7bbff
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 124 additions and 0 deletions

View File

@ -34,6 +34,7 @@ from aioesphomeapi import (
SwitchInfo,
TextInfo,
TextSensorInfo,
TimeInfo,
UserService,
build_unique_id,
)
@ -75,6 +76,7 @@ INFO_TYPE_TO_PLATFORM: dict[type[EntityInfo], Platform] = {
SwitchInfo: Platform.SWITCH,
TextInfo: Platform.TEXT,
TextSensorInfo: Platform.SENSOR,
TimeInfo: Platform.TIME,
}

View File

@ -0,0 +1,46 @@
"""Support for esphome times."""
from __future__ import annotations
from datetime import time
from aioesphomeapi import TimeInfo, TimeState
from homeassistant.components.time import TimeEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .entity import EsphomeEntity, esphome_state_property, platform_async_setup_entry
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up esphome times based on a config entry."""
await platform_async_setup_entry(
hass,
entry,
async_add_entities,
info_type=TimeInfo,
entity_type=EsphomeTime,
state_type=TimeState,
)
class EsphomeTime(EsphomeEntity[TimeInfo, TimeState], TimeEntity):
"""A time implementation for esphome."""
@property
@esphome_state_property
def native_value(self) -> time | None:
"""Return the state of the entity."""
state = self._state
if state.missing_state:
return None
return time(state.hour, state.minute, state.second)
async def async_set_value(self, value: time) -> None:
"""Update the current time."""
self._client.time_command(self._key, value.hour, value.minute, value.second)

View File

@ -0,0 +1,76 @@
"""Test ESPHome times."""
from unittest.mock import call
from aioesphomeapi import APIClient, TimeInfo, TimeState
from homeassistant.components.time import (
ATTR_TIME,
DOMAIN as TIME_DOMAIN,
SERVICE_SET_VALUE,
)
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
from homeassistant.core import HomeAssistant
async def test_generic_time_entity(
hass: HomeAssistant,
mock_client: APIClient,
mock_generic_device_entry,
) -> None:
"""Test a generic time entity."""
entity_info = [
TimeInfo(
object_id="mytime",
key=1,
name="my time",
unique_id="my_time",
)
]
states = [TimeState(key=1, hour=12, minute=34, second=56)]
user_service = []
await mock_generic_device_entry(
mock_client=mock_client,
entity_info=entity_info,
user_service=user_service,
states=states,
)
state = hass.states.get("time.test_mytime")
assert state is not None
assert state.state == "12:34:56"
await hass.services.async_call(
TIME_DOMAIN,
SERVICE_SET_VALUE,
{ATTR_ENTITY_ID: "time.test_mytime", ATTR_TIME: "01:23:45"},
blocking=True,
)
mock_client.time_command.assert_has_calls([call(1, 1, 23, 45)])
mock_client.time_command.reset_mock()
async def test_generic_time_missing_state(
hass: HomeAssistant,
mock_client: APIClient,
mock_generic_device_entry,
) -> None:
"""Test a generic time entity with missing state."""
entity_info = [
TimeInfo(
object_id="mytime",
key=1,
name="my time",
unique_id="my_time",
)
]
states = [TimeState(key=1, missing_state=True)]
user_service = []
await mock_generic_device_entry(
mock_client=mock_client,
entity_info=entity_info,
user_service=user_service,
states=states,
)
state = hass.states.get("time.test_mytime")
assert state is not None
assert state.state == STATE_UNKNOWN