mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Add Time platform with alarm clock to Home Connect (#126155)
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
This commit is contained in:
parent
beafcf74ab
commit
275bbc81f0
@ -79,7 +79,13 @@ SERVICE_PROGRAM_SCHEMA = vol.Any(
|
||||
|
||||
SERVICE_COMMAND_SCHEMA = vol.Schema({vol.Required(ATTR_DEVICE_ID): str})
|
||||
|
||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.LIGHT, Platform.SENSOR, Platform.SWITCH]
|
||||
PLATFORMS = [
|
||||
Platform.BINARY_SENSOR,
|
||||
Platform.LIGHT,
|
||||
Platform.SENSOR,
|
||||
Platform.SWITCH,
|
||||
Platform.TIME,
|
||||
]
|
||||
|
||||
|
||||
def _get_appliance_by_device_id(
|
||||
|
@ -357,6 +357,11 @@
|
||||
"door_assistant_freezer": {
|
||||
"name": "Freezer door assistant"
|
||||
}
|
||||
},
|
||||
"time": {
|
||||
"alarm_clock": {
|
||||
"name": "Alarm clock"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
98
homeassistant/components/home_connect/time.py
Normal file
98
homeassistant/components/home_connect/time.py
Normal file
@ -0,0 +1,98 @@
|
||||
"""Provides time enties for Home Connect."""
|
||||
|
||||
from datetime import time
|
||||
import logging
|
||||
|
||||
from homeconnect.api import HomeConnectError
|
||||
|
||||
from homeassistant.components.time import TimeEntity, TimeEntityDescription
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from .api import ConfigEntryAuth
|
||||
from .const import ATTR_VALUE, DOMAIN
|
||||
from .entity import HomeConnectEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
||||
TIME_ENTITIES = (
|
||||
TimeEntityDescription(
|
||||
key="BSH.Common.Setting.AlarmClock",
|
||||
translation_key="alarm_clock",
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistant,
|
||||
config_entry: ConfigEntry,
|
||||
async_add_entities: AddEntitiesCallback,
|
||||
) -> None:
|
||||
"""Set up the Home Connect switch."""
|
||||
|
||||
def get_entities() -> list[HomeConnectTimeEntity]:
|
||||
"""Get a list of entities."""
|
||||
hc_api: ConfigEntryAuth = hass.data[DOMAIN][config_entry.entry_id]
|
||||
return [
|
||||
HomeConnectTimeEntity(device, description)
|
||||
for description in TIME_ENTITIES
|
||||
for device in hc_api.devices
|
||||
if description.key in device.appliance.status
|
||||
]
|
||||
|
||||
async_add_entities(await hass.async_add_executor_job(get_entities), True)
|
||||
|
||||
|
||||
def seconds_to_time(seconds: int) -> time:
|
||||
"""Convert seconds to a time object."""
|
||||
minutes, sec = divmod(seconds, 60)
|
||||
hours, minutes = divmod(minutes, 60)
|
||||
return time(hour=hours, minute=minutes, second=sec)
|
||||
|
||||
|
||||
def time_to_seconds(t: time) -> int:
|
||||
"""Convert a time object to seconds."""
|
||||
return t.hour * 3600 + t.minute * 60 + t.second
|
||||
|
||||
|
||||
class HomeConnectTimeEntity(HomeConnectEntity, TimeEntity):
|
||||
"""Time setting class for Home Connect."""
|
||||
|
||||
async def async_set_value(self, value: time) -> None:
|
||||
"""Set the native value of the entity."""
|
||||
_LOGGER.debug(
|
||||
"Tried to set value %s to %s for %s",
|
||||
value,
|
||||
self.bsh_key,
|
||||
self.entity_id,
|
||||
)
|
||||
try:
|
||||
await self.hass.async_add_executor_job(
|
||||
self.device.appliance.set_setting,
|
||||
self.bsh_key,
|
||||
time_to_seconds(value),
|
||||
)
|
||||
except HomeConnectError as err:
|
||||
_LOGGER.error(
|
||||
"Error setting value %s to %s for %s: %s",
|
||||
value,
|
||||
self.bsh_key,
|
||||
self.entity_id,
|
||||
err,
|
||||
)
|
||||
|
||||
async def async_update(self) -> None:
|
||||
"""Update the Time setting status."""
|
||||
data = self.device.appliance.status.get(self.bsh_key)
|
||||
if data is None:
|
||||
_LOGGER.error("No value for %s", self.bsh_key)
|
||||
self._attr_native_value = None
|
||||
return
|
||||
seconds = data.get(ATTR_VALUE, None)
|
||||
if seconds is not None:
|
||||
self._attr_native_value = seconds_to_time(seconds)
|
||||
else:
|
||||
self._attr_native_value = None
|
||||
_LOGGER.debug("Updated, new value: %s", self._attr_native_value)
|
146
tests/components/home_connect/test_time.py
Normal file
146
tests/components/home_connect/test_time.py
Normal file
@ -0,0 +1,146 @@
|
||||
"""Tests for home_connect time entities."""
|
||||
|
||||
from collections.abc import Awaitable, Callable, Generator
|
||||
from datetime import time
|
||||
from unittest.mock import MagicMock, Mock
|
||||
|
||||
from homeconnect.api import HomeConnectError
|
||||
import pytest
|
||||
|
||||
from homeassistant.components.home_connect.const import ATTR_VALUE
|
||||
from homeassistant.components.time import DOMAIN as TIME_DOMAIN, SERVICE_SET_VALUE
|
||||
from homeassistant.config_entries import ConfigEntryState
|
||||
from homeassistant.const import ATTR_ENTITY_ID, ATTR_TIME, Platform
|
||||
from homeassistant.core import HomeAssistant
|
||||
|
||||
from .conftest import get_all_appliances
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def platforms() -> list[str]:
|
||||
"""Fixture to specify platforms to test."""
|
||||
return [Platform.TIME]
|
||||
|
||||
|
||||
async def test_time(
|
||||
bypass_throttle: Generator[None],
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
integration_setup: Callable[[], Awaitable[bool]],
|
||||
setup_credentials: None,
|
||||
get_appliances: Mock,
|
||||
) -> None:
|
||||
"""Test time entity."""
|
||||
get_appliances.side_effect = get_all_appliances
|
||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert await integration_setup()
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
|
||||
@pytest.mark.parametrize("appliance", ["Oven"], indirect=True)
|
||||
@pytest.mark.parametrize(
|
||||
("entity_id", "setting_key", "setting_value", "expected_state"),
|
||||
[
|
||||
(
|
||||
f"{TIME_DOMAIN}.oven_alarm_clock",
|
||||
"BSH.Common.Setting.AlarmClock",
|
||||
{ATTR_VALUE: 59},
|
||||
str(time(second=59)),
|
||||
),
|
||||
(
|
||||
f"{TIME_DOMAIN}.oven_alarm_clock",
|
||||
"BSH.Common.Setting.AlarmClock",
|
||||
{ATTR_VALUE: None},
|
||||
"unknown",
|
||||
),
|
||||
(
|
||||
f"{TIME_DOMAIN}.oven_alarm_clock",
|
||||
"BSH.Common.Setting.AlarmClock",
|
||||
None,
|
||||
"unknown",
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("bypass_throttle")
|
||||
async def test_time_entity_functionality(
|
||||
appliance: Mock,
|
||||
entity_id: str,
|
||||
setting_key: str,
|
||||
setting_value: dict,
|
||||
expected_state: str,
|
||||
bypass_throttle: Generator[None],
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
integration_setup: Callable[[], Awaitable[bool]],
|
||||
setup_credentials: None,
|
||||
get_appliances: MagicMock,
|
||||
) -> None:
|
||||
"""Test time entity functionality."""
|
||||
get_appliances.return_value = [appliance]
|
||||
appliance.status.update({setting_key: setting_value})
|
||||
|
||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
assert await integration_setup()
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
assert hass.states.is_state(entity_id, expected_state)
|
||||
|
||||
new_value = 30
|
||||
assert hass.states.get(entity_id).state != new_value
|
||||
await hass.services.async_call(
|
||||
TIME_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{
|
||||
ATTR_ENTITY_ID: entity_id,
|
||||
ATTR_TIME: time(second=new_value),
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
appliance.set_setting.assert_called_once_with(setting_key, new_value)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("problematic_appliance", ["Oven"], indirect=True)
|
||||
@pytest.mark.parametrize(
|
||||
("entity_id", "setting_key", "mock_attr"),
|
||||
[
|
||||
(
|
||||
f"{TIME_DOMAIN}.oven_alarm_clock",
|
||||
"BSH.Common.Setting.AlarmClock",
|
||||
"set_setting",
|
||||
),
|
||||
],
|
||||
)
|
||||
@pytest.mark.usefixtures("bypass_throttle")
|
||||
async def test_time_entity_error(
|
||||
problematic_appliance: Mock,
|
||||
entity_id: str,
|
||||
setting_key: str,
|
||||
mock_attr: str,
|
||||
hass: HomeAssistant,
|
||||
config_entry: MockConfigEntry,
|
||||
integration_setup: Callable[[], Awaitable[bool]],
|
||||
setup_credentials: None,
|
||||
get_appliances: MagicMock,
|
||||
) -> None:
|
||||
"""Test time entity error."""
|
||||
get_appliances.return_value = [problematic_appliance]
|
||||
|
||||
assert config_entry.state is ConfigEntryState.NOT_LOADED
|
||||
problematic_appliance.status.update({setting_key: {}})
|
||||
assert await integration_setup()
|
||||
assert config_entry.state is ConfigEntryState.LOADED
|
||||
|
||||
with pytest.raises(HomeConnectError):
|
||||
getattr(problematic_appliance, mock_attr)()
|
||||
|
||||
await hass.services.async_call(
|
||||
TIME_DOMAIN,
|
||||
SERVICE_SET_VALUE,
|
||||
{
|
||||
ATTR_ENTITY_ID: entity_id,
|
||||
ATTR_TIME: time(minute=1),
|
||||
},
|
||||
blocking=True,
|
||||
)
|
||||
assert getattr(problematic_appliance, mock_attr).call_count == 2
|
Loading…
x
Reference in New Issue
Block a user