ESPHome: Add Date Entity support (#112475)
* ESPHome: Add Date Entity support * Formatting * Add missing state test
This commit is contained in:
parent
e5ba4dbde9
commit
3d7d3d263d
3 changed files with 124 additions and 0 deletions
46
homeassistant/components/esphome/date.py
Normal file
46
homeassistant/components/esphome/date.py
Normal file
|
@ -0,0 +1,46 @@
|
|||
"""Support for esphome dates."""
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import date
|
||||
|
||||
from aioesphomeapi import DateInfo, DateState
|
||||
|
||||
from homeassistant.components.date import DateEntity
|
||||
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 dates based on a config entry."""
|
||||
await platform_async_setup_entry(
|
||||
hass,
|
||||
entry,
|
||||
async_add_entities,
|
||||
info_type=DateInfo,
|
||||
entity_type=EsphomeDate,
|
||||
state_type=DateState,
|
||||
)
|
||||
|
||||
|
||||
class EsphomeDate(EsphomeEntity[DateInfo, DateState], DateEntity):
|
||||
"""A date implementation for esphome."""
|
||||
|
||||
@property
|
||||
@esphome_state_property
|
||||
def native_value(self) -> date | None:
|
||||
"""Return the state of the entity."""
|
||||
state = self._state
|
||||
if state.missing_state:
|
||||
return None
|
||||
return date(state.year, state.month, state.day)
|
||||
|
||||
async def async_set_value(self, value: date) -> None:
|
||||
"""Update the current date."""
|
||||
self._client.date_command(self._key, value.year, value.month, value.day)
|
|
@ -19,6 +19,7 @@ from aioesphomeapi import (
|
|||
CameraState,
|
||||
ClimateInfo,
|
||||
CoverInfo,
|
||||
DateInfo,
|
||||
DeviceInfo,
|
||||
EntityInfo,
|
||||
EntityState,
|
||||
|
@ -63,6 +64,7 @@ INFO_TYPE_TO_PLATFORM: dict[type[EntityInfo], Platform] = {
|
|||
CameraInfo: Platform.CAMERA,
|
||||
ClimateInfo: Platform.CLIMATE,
|
||||
CoverInfo: Platform.COVER,
|
||||
DateInfo: Platform.DATE,
|
||||
FanInfo: Platform.FAN,
|
||||
LightInfo: Platform.LIGHT,
|
||||
LockInfo: Platform.LOCK,
|
||||
|
|
76
tests/components/esphome/test_date.py
Normal file
76
tests/components/esphome/test_date.py
Normal file
|
@ -0,0 +1,76 @@
|
|||
"""Test ESPHome dates."""
|
||||
|
||||
from unittest.mock import call
|
||||
|
||||
from aioesphomeapi import APIClient, DateInfo, DateState
|
||||
|
||||
from homeassistant.components.date import (
|
||||
ATTR_DATE,
|
||||
DOMAIN as DATE_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
)
|
||||
from homeassistant.const import ATTR_ENTITY_ID, STATE_UNKNOWN
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
|
||||
async def test_generic_date_entity(
|
||||
hass: HomeAssistant,
|
||||
mock_client: APIClient,
|
||||
mock_generic_device_entry,
|
||||
) -> None:
|
||||
"""Test a generic date entity."""
|
||||
entity_info = [
|
||||
DateInfo(
|
||||
object_id="mydate",
|
||||
key=1,
|
||||
name="my date",
|
||||
unique_id="my_date",
|
||||
)
|
||||
]
|
||||
states = [DateState(key=1, year=2024, month=12, day=31)]
|
||||
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("date.test_mydate")
|
||||
assert state is not None
|
||||
assert state.state == "2024-12-31"
|
||||
|
||||
await hass.services.async_call(
|
||||
DATE_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{ATTR_ENTITY_ID: "date.test_mydate", ATTR_DATE: "1999-01-01"},
|
||||
blocking=True,
|
||||
)
|
||||
mock_client.date_command.assert_has_calls([call(1, 1999, 1, 1)])
|
||||
mock_client.date_command.reset_mock()
|
||||
|
||||
|
||||
async def test_generic_date_missing_state(
|
||||
hass: HomeAssistant,
|
||||
mock_client: APIClient,
|
||||
mock_generic_device_entry,
|
||||
) -> None:
|
||||
"""Test a generic date entity with missing state."""
|
||||
entity_info = [
|
||||
DateInfo(
|
||||
object_id="mydate",
|
||||
key=1,
|
||||
name="my date",
|
||||
unique_id="my_date",
|
||||
)
|
||||
]
|
||||
states = [DateState(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("date.test_mydate")
|
||||
assert state is not None
|
||||
assert state.state == STATE_UNKNOWN
|
Loading…
Add table
Add a link
Reference in a new issue