mirror of
https://github.com/home-assistant/core.git
synced 2025-07-11 23:37:18 +00:00
ESPHome: Add Time Entity support (#113852)
This commit is contained in:
parent
aebc95b1d2
commit
6ddef7bbff
@ -34,6 +34,7 @@ from aioesphomeapi import (
|
|||||||
SwitchInfo,
|
SwitchInfo,
|
||||||
TextInfo,
|
TextInfo,
|
||||||
TextSensorInfo,
|
TextSensorInfo,
|
||||||
|
TimeInfo,
|
||||||
UserService,
|
UserService,
|
||||||
build_unique_id,
|
build_unique_id,
|
||||||
)
|
)
|
||||||
@ -75,6 +76,7 @@ INFO_TYPE_TO_PLATFORM: dict[type[EntityInfo], Platform] = {
|
|||||||
SwitchInfo: Platform.SWITCH,
|
SwitchInfo: Platform.SWITCH,
|
||||||
TextInfo: Platform.TEXT,
|
TextInfo: Platform.TEXT,
|
||||||
TextSensorInfo: Platform.SENSOR,
|
TextSensorInfo: Platform.SENSOR,
|
||||||
|
TimeInfo: Platform.TIME,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
46
homeassistant/components/esphome/time.py
Normal file
46
homeassistant/components/esphome/time.py
Normal 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)
|
76
tests/components/esphome/test_time.py
Normal file
76
tests/components/esphome/test_time.py
Normal 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
|
Loading…
x
Reference in New Issue
Block a user