|
| 1 | +"""Tests for the Risco services.""" |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +import pytest |
| 7 | + |
| 8 | +from homeassistant.components.risco import DOMAIN |
| 9 | +from homeassistant.components.risco.const import SERVICE_SET_TIME |
| 10 | +from homeassistant.config_entries import ConfigEntryState |
| 11 | +from homeassistant.const import ATTR_CONFIG_ENTRY_ID, ATTR_TIME |
| 12 | +from homeassistant.core import HomeAssistant |
| 13 | +from homeassistant.exceptions import ServiceValidationError |
| 14 | + |
| 15 | +from .conftest import TEST_CLOUD_CONFIG |
| 16 | + |
| 17 | +from tests.common import MockConfigEntry |
| 18 | + |
| 19 | + |
| 20 | +async def test_set_time_service( |
| 21 | + hass: HomeAssistant, setup_risco_local, local_config_entry |
| 22 | +) -> None: |
| 23 | + """Test the set_time service.""" |
| 24 | + with patch("homeassistant.components.risco.RiscoLocal.set_time") as mock: |
| 25 | + time_str = "2025-02-21T12:00:00" |
| 26 | + time = datetime.fromisoformat(time_str) |
| 27 | + data = { |
| 28 | + ATTR_CONFIG_ENTRY_ID: local_config_entry.entry_id, |
| 29 | + ATTR_TIME: time_str, |
| 30 | + } |
| 31 | + |
| 32 | + await hass.services.async_call( |
| 33 | + DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True |
| 34 | + ) |
| 35 | + |
| 36 | + mock.assert_called_once_with(time) |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.freeze_time("2025-02-21T12:00:00Z") |
| 40 | +async def test_set_time_service_with_no_time( |
| 41 | + hass: HomeAssistant, setup_risco_local, local_config_entry |
| 42 | +) -> None: |
| 43 | + """Test the set_time service when no time is provided.""" |
| 44 | + with patch("homeassistant.components.risco.RiscoLocal.set_time") as mock_set_time: |
| 45 | + data = { |
| 46 | + "config_entry_id": local_config_entry.entry_id, |
| 47 | + } |
| 48 | + |
| 49 | + await hass.services.async_call( |
| 50 | + DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True |
| 51 | + ) |
| 52 | + |
| 53 | + mock_set_time.assert_called_once_with(datetime.now()) |
| 54 | + |
| 55 | + |
| 56 | +async def test_set_time_service_with_invalid_entry( |
| 57 | + hass: HomeAssistant, setup_risco_local |
| 58 | +) -> None: |
| 59 | + """Test the set_time service with an invalid config entry.""" |
| 60 | + data = { |
| 61 | + ATTR_CONFIG_ENTRY_ID: "invalid_entry_id", |
| 62 | + } |
| 63 | + |
| 64 | + with pytest.raises(ServiceValidationError, match="Config entry not found"): |
| 65 | + await hass.services.async_call( |
| 66 | + DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True |
| 67 | + ) |
| 68 | + |
| 69 | + |
| 70 | +async def test_set_time_service_with_not_loaded_entry( |
| 71 | + hass: HomeAssistant, setup_risco_local, local_config_entry |
| 72 | +) -> None: |
| 73 | + """Test the set_time service with a config entry that is not loaded.""" |
| 74 | + await hass.config_entries.async_unload(local_config_entry.entry_id) |
| 75 | + await hass.async_block_till_done() |
| 76 | + |
| 77 | + assert local_config_entry.state is ConfigEntryState.NOT_LOADED |
| 78 | + |
| 79 | + data = { |
| 80 | + ATTR_CONFIG_ENTRY_ID: local_config_entry.entry_id, |
| 81 | + } |
| 82 | + |
| 83 | + with pytest.raises(ServiceValidationError, match="is not loaded"): |
| 84 | + await hass.services.async_call( |
| 85 | + DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +async def test_set_time_service_with_cloud_entry( |
| 90 | + hass: HomeAssistant, setup_risco_local |
| 91 | +) -> None: |
| 92 | + """Test the set_time service with a cloud config entry.""" |
| 93 | + cloud_entry = MockConfigEntry( |
| 94 | + domain=DOMAIN, |
| 95 | + unique_id="test-cloud", |
| 96 | + data=TEST_CLOUD_CONFIG, |
| 97 | + ) |
| 98 | + cloud_entry.add_to_hass(hass) |
| 99 | + cloud_entry.mock_state(hass, ConfigEntryState.LOADED) |
| 100 | + |
| 101 | + data = { |
| 102 | + ATTR_CONFIG_ENTRY_ID: cloud_entry.entry_id, |
| 103 | + } |
| 104 | + |
| 105 | + with pytest.raises( |
| 106 | + ServiceValidationError, match="This service only works with local" |
| 107 | + ): |
| 108 | + await hass.services.async_call( |
| 109 | + DOMAIN, SERVICE_SET_TIME, service_data=data, blocking=True |
| 110 | + ) |
0 commit comments