-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fastdotcom service optimization (#107179)
* Startup mechanic * Workable service (again) * Optimized version, for now * Minor refactoring * Test cases * Fixing test case * Adding startup comment * State_unknown added * Update homeassistant/components/fastdotcom/services.py Co-authored-by: Martin Hjelmare <[email protected]> * Check if config entries are not found * Update tests/components/fastdotcom/test_service.py Co-authored-by: Martin Hjelmare <[email protected]> * Update homeassistant/components/fastdotcom/services.py Co-authored-by: Martin Hjelmare <[email protected]> * Update homeassistant/components/fastdotcom/services.py Co-authored-by: Martin Hjelmare <[email protected]> --------- Co-authored-by: Martin Hjelmare <[email protected]>
- Loading branch information
1 parent
e045759
commit b08832a
Showing
5 changed files
with
161 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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 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 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,51 @@ | ||
"""Services for the Fastdotcom integration.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.config_entries import ConfigEntryState | ||
from homeassistant.core import HomeAssistant, ServiceCall, callback | ||
from homeassistant.exceptions import HomeAssistantError | ||
from homeassistant.helpers import issue_registry as ir | ||
|
||
from .const import DOMAIN, SERVICE_NAME | ||
from .coordinator import FastdotcomDataUpdateCoordindator | ||
|
||
|
||
def async_setup_services(hass: HomeAssistant) -> None: | ||
"""Set up the service for the Fastdotcom integration.""" | ||
|
||
@callback | ||
def collect_coordinator() -> FastdotcomDataUpdateCoordindator: | ||
"""Collect the coordinator Fastdotcom.""" | ||
config_entries = hass.config_entries.async_entries(DOMAIN) | ||
if not config_entries: | ||
raise HomeAssistantError("No Fast.com config entries found") | ||
|
||
for config_entry in config_entries: | ||
if config_entry.state != ConfigEntryState.LOADED: | ||
raise HomeAssistantError(f"{config_entry.title} is not loaded") | ||
coordinator: FastdotcomDataUpdateCoordindator = hass.data[DOMAIN][ | ||
config_entry.entry_id | ||
] | ||
break | ||
return coordinator | ||
|
||
async def async_perform_service(call: ServiceCall) -> None: | ||
"""Perform a service call to manually run Fastdotcom.""" | ||
ir.async_create_issue( | ||
hass, | ||
DOMAIN, | ||
"service_deprecation", | ||
breaks_in_ha_version="2024.7.0", | ||
is_fixable=True, | ||
is_persistent=True, | ||
severity=ir.IssueSeverity.WARNING, | ||
translation_key="service_deprecation", | ||
) | ||
coordinator = collect_coordinator() | ||
await coordinator.async_request_refresh() | ||
|
||
hass.services.async_register( | ||
DOMAIN, | ||
SERVICE_NAME, | ||
async_perform_service, | ||
) |
This file contains 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 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,87 @@ | ||
"""Test Fastdotcom service.""" | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from homeassistant.components.fastdotcom.const import DEFAULT_NAME, DOMAIN, SERVICE_NAME | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import HomeAssistantError | ||
|
||
from tests.common import MockConfigEntry | ||
|
||
|
||
async def test_service(hass: HomeAssistant) -> None: | ||
"""Test the Fastdotcom service.""" | ||
config_entry = MockConfigEntry( | ||
domain=DOMAIN, | ||
unique_id="UNIQUE_TEST_ID", | ||
title=DEFAULT_NAME, | ||
) | ||
config_entry.add_to_hass(hass) | ||
|
||
with patch( | ||
"homeassistant.components.fastdotcom.coordinator.fast_com", return_value=0 | ||
): | ||
await hass.config_entries.async_setup(config_entry.entry_id) | ||
await hass.async_block_till_done() | ||
|
||
state = hass.states.get("sensor.fast_com_download") | ||
assert state is not None | ||
assert state.state == "0" | ||
|
||
with patch( | ||
"homeassistant.components.fastdotcom.coordinator.fast_com", return_value=5.0 | ||
): | ||
await hass.services.async_call(DOMAIN, SERVICE_NAME, blocking=True) | ||
|
||
state = hass.states.get("sensor.fast_com_download") | ||
assert state is not None | ||
assert state.state == "5.0" | ||
|
||
|
||
async def test_service_unloaded_entry(hass: HomeAssistant) -> None: | ||
"""Test service called when config entry unloaded.""" | ||
config_entry = MockConfigEntry( | ||
domain=DOMAIN, | ||
unique_id="UNIQUE_TEST_ID", | ||
title=DEFAULT_NAME, | ||
) | ||
config_entry.add_to_hass(hass) | ||
|
||
with patch( | ||
"homeassistant.components.fastdotcom.coordinator.fast_com", return_value=0 | ||
): | ||
await hass.config_entries.async_setup(config_entry.entry_id) | ||
await hass.async_block_till_done() | ||
|
||
assert config_entry | ||
await config_entry.async_unload(hass) | ||
|
||
with pytest.raises(HomeAssistantError) as exc: | ||
await hass.services.async_call(DOMAIN, SERVICE_NAME, blocking=True) | ||
|
||
assert "Fast.com is not loaded" in str(exc) | ||
|
||
|
||
async def test_service_removed_entry(hass: HomeAssistant) -> None: | ||
"""Test service called when config entry was removed and HA was not restarted yet.""" | ||
config_entry = MockConfigEntry( | ||
domain=DOMAIN, | ||
unique_id="UNIQUE_TEST_ID", | ||
title=DEFAULT_NAME, | ||
) | ||
config_entry.add_to_hass(hass) | ||
|
||
with patch( | ||
"homeassistant.components.fastdotcom.coordinator.fast_com", return_value=0 | ||
): | ||
await hass.config_entries.async_setup(config_entry.entry_id) | ||
await hass.async_block_till_done() | ||
|
||
assert config_entry | ||
await hass.config_entries.async_remove(config_entry.entry_id) | ||
|
||
with pytest.raises(HomeAssistantError) as exc: | ||
await hass.services.async_call(DOMAIN, SERVICE_NAME, blocking=True) | ||
|
||
assert "No Fast.com config entries found" in str(exc) |