-
-
Notifications
You must be signed in to change notification settings - Fork 37.4k
Add Risco set_time service #139015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
joostlek
merged 14 commits into
home-assistant:dev
from
FredericMa:feature/time-service
Dec 15, 2025
Merged
Add Risco set_time service #139015
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ef2a004
Add set time service
FredericMa 19d0eba
Update config schema
FredericMa 06d442d
Add test if no time is provided in the service call.
FredericMa e982361
Update config_entry_id constant
FredericMa 9cfdacd
Remove unnecessary CONFIG_SCHEMA
FredericMa bfa8347
Revert "Remove unnecessary CONFIG_SCHEMA"
FredericMa e4c25b7
Update test to freeze time instead of mocking time.
FredericMa 82aaa8e
Merge branch 'dev' into feature/time-service
FredericMa 1761266
Merge branch 'dev' into feature/time-service
FredericMa cf3aef3
Merge branch 'dev' into feature/time-service
FredericMa ae80499
Add validation to the service call.
FredericMa b8a8db9
Fix linting
FredericMa af9deb7
Use time constant from homeassistant.const
FredericMa b7f0f41
Merge branch 'dev' into feature/time-service
FredericMa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "services": { | ||
| "set_time": { | ||
| "service": "mdi:clock-edit" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| """Models for Risco integration.""" | ||
|
|
||
| from collections.abc import Callable | ||
| from dataclasses import dataclass, field | ||
| from typing import Any | ||
|
|
||
| from pyrisco import RiscoLocal | ||
|
|
||
|
|
||
| @dataclass | ||
| class LocalData: | ||
| """A data class for local data passed to the platforms.""" | ||
|
|
||
| system: RiscoLocal | ||
| partition_updates: dict[int, Callable[[], Any]] = field(default_factory=dict) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """Services for Risco integration.""" | ||
|
|
||
| from datetime import datetime | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| from homeassistant.config_entries import ConfigEntryState | ||
| from homeassistant.const import ATTR_CONFIG_ENTRY_ID, ATTR_TIME, CONF_TYPE | ||
| from homeassistant.core import HomeAssistant, ServiceCall | ||
| from homeassistant.exceptions import ServiceValidationError | ||
| from homeassistant.helpers import config_validation as cv | ||
|
|
||
| from .const import DOMAIN, SERVICE_SET_TIME, TYPE_LOCAL | ||
| from .models import LocalData | ||
|
|
||
|
|
||
| async def async_setup_services(hass: HomeAssistant) -> None: | ||
| """Create the Risco Services/Actions.""" | ||
|
|
||
| async def _set_time(service_call: ServiceCall) -> None: | ||
| config_entry_id = service_call.data[ATTR_CONFIG_ENTRY_ID] | ||
| time = service_call.data.get(ATTR_TIME) | ||
|
|
||
| # Validate config entry exists | ||
| if not (entry := hass.config_entries.async_get_entry(config_entry_id)): | ||
| raise ServiceValidationError( | ||
| translation_domain=DOMAIN, | ||
| translation_key="config_entry_not_found", | ||
| ) | ||
|
|
||
| # Validate config entry is loaded | ||
| if entry.state is not ConfigEntryState.LOADED: | ||
| raise ServiceValidationError( | ||
| translation_domain=DOMAIN, | ||
| translation_key="config_entry_not_loaded", | ||
| ) | ||
|
|
||
| # Validate config entry is local (not cloud) | ||
| if entry.data.get(CONF_TYPE) != TYPE_LOCAL: | ||
| raise ServiceValidationError( | ||
| translation_domain=DOMAIN, | ||
| translation_key="not_local_entry", | ||
| ) | ||
|
|
||
| time_to_send = time | ||
| if time is None: | ||
| time_to_send = datetime.now() | ||
|
FredericMa marked this conversation as resolved.
FredericMa marked this conversation as resolved.
|
||
|
|
||
| local_data: LocalData = hass.data[DOMAIN][config_entry_id] | ||
|
FredericMa marked this conversation as resolved.
|
||
|
|
||
| await local_data.system.set_time(time_to_send) | ||
|
FredericMa marked this conversation as resolved.
|
||
|
|
||
| hass.services.async_register( | ||
| domain=DOMAIN, | ||
| service=SERVICE_SET_TIME, | ||
| schema=vol.Schema( | ||
| { | ||
| vol.Required(ATTR_CONFIG_ENTRY_ID): cv.string, | ||
| vol.Optional(ATTR_TIME): cv.datetime, | ||
| } | ||
| ), | ||
| service_func=_set_time, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| set_time: | ||
| fields: | ||
| config_entry_id: | ||
| required: true | ||
| selector: | ||
| config_entry: | ||
| integration: risco | ||
| time: | ||
| required: false | ||
| selector: | ||
| datetime: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| """Tests for the Risco services.""" | ||
|
|
||
| from datetime import datetime | ||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
|
|
||
| from homeassistant.components.risco import DOMAIN | ||
| from homeassistant.components.risco.const import SERVICE_SET_TIME | ||
| from homeassistant.config_entries import ConfigEntryState | ||
| from homeassistant.const import ATTR_CONFIG_ENTRY_ID, ATTR_TIME | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.exceptions import ServiceValidationError | ||
|
|
||
| from .conftest import TEST_CLOUD_CONFIG | ||
|
|
||
| from tests.common import MockConfigEntry | ||
|
|
||
|
|
||
| async def test_set_time_service( | ||
| hass: HomeAssistant, setup_risco_local, local_config_entry | ||
| ) -> None: | ||
| """Test the set_time service.""" | ||
| with patch("homeassistant.components.risco.RiscoLocal.set_time") as mock: | ||
| time_str = "2025-02-21T12:00:00" | ||
| time = datetime.fromisoformat(time_str) | ||
| data = { | ||
| ATTR_CONFIG_ENTRY_ID: local_config_entry.entry_id, | ||
| ATTR_TIME: time_str, | ||
| } | ||
|
|
||
| await hass.services.async_call( | ||
| DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True | ||
| ) | ||
|
|
||
| mock.assert_called_once_with(time) | ||
|
|
||
|
|
||
| @pytest.mark.freeze_time("2025-02-21T12:00:00Z") | ||
| async def test_set_time_service_with_no_time( | ||
| hass: HomeAssistant, setup_risco_local, local_config_entry | ||
| ) -> None: | ||
| """Test the set_time service when no time is provided.""" | ||
| with patch("homeassistant.components.risco.RiscoLocal.set_time") as mock_set_time: | ||
| data = { | ||
| "config_entry_id": local_config_entry.entry_id, | ||
| } | ||
|
|
||
| await hass.services.async_call( | ||
| DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True | ||
| ) | ||
|
|
||
| mock_set_time.assert_called_once_with(datetime.now()) | ||
|
FredericMa marked this conversation as resolved.
|
||
|
|
||
|
|
||
| async def test_set_time_service_with_invalid_entry( | ||
| hass: HomeAssistant, setup_risco_local | ||
| ) -> None: | ||
| """Test the set_time service with an invalid config entry.""" | ||
| data = { | ||
| ATTR_CONFIG_ENTRY_ID: "invalid_entry_id", | ||
| } | ||
|
|
||
| with pytest.raises(ServiceValidationError, match="Config entry not found"): | ||
| await hass.services.async_call( | ||
| DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True | ||
| ) | ||
|
|
||
|
|
||
| async def test_set_time_service_with_not_loaded_entry( | ||
| hass: HomeAssistant, setup_risco_local, local_config_entry | ||
| ) -> None: | ||
| """Test the set_time service with a config entry that is not loaded.""" | ||
| await hass.config_entries.async_unload(local_config_entry.entry_id) | ||
| await hass.async_block_till_done() | ||
|
|
||
| assert local_config_entry.state is ConfigEntryState.NOT_LOADED | ||
|
|
||
| data = { | ||
| ATTR_CONFIG_ENTRY_ID: local_config_entry.entry_id, | ||
| } | ||
|
|
||
| with pytest.raises(ServiceValidationError, match="is not loaded"): | ||
| await hass.services.async_call( | ||
| DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True | ||
| ) | ||
|
|
||
|
|
||
| async def test_set_time_service_with_cloud_entry( | ||
| hass: HomeAssistant, setup_risco_local | ||
| ) -> None: | ||
| """Test the set_time service with a cloud config entry.""" | ||
| cloud_entry = MockConfigEntry( | ||
| domain=DOMAIN, | ||
| unique_id="test-cloud", | ||
| data=TEST_CLOUD_CONFIG, | ||
| ) | ||
| cloud_entry.add_to_hass(hass) | ||
| cloud_entry.mock_state(hass, ConfigEntryState.LOADED) | ||
|
|
||
| data = { | ||
| ATTR_CONFIG_ENTRY_ID: cloud_entry.entry_id, | ||
| } | ||
|
|
||
| with pytest.raises( | ||
| ServiceValidationError, match="This service only works with local" | ||
| ): | ||
| await hass.services.async_call( | ||
| DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.