Remove previously-deprecated OpenUV services (#81055)

pull/81074/head
Aaron Bach 2022-10-27 02:22:44 -06:00 committed by GitHub
parent 1967ce67d7
commit 4e8e53f357
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 4 additions and 207 deletions

View File

@ -5,9 +5,8 @@ import asyncio
from typing import Any
from pyopenuv import Client
import voluptuous as vol
from homeassistant.config_entries import ConfigEntry, ConfigEntryState
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_API_KEY,
CONF_BINARY_SENSORS,
@ -17,17 +16,10 @@ from homeassistant.const import (
CONF_SENSORS,
Platform,
)
from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers import (
aiohttp_client,
config_validation as cv,
entity_registry,
)
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.service import verify_domain_control
from homeassistant.helpers.update_coordinator import CoordinatorEntity, UpdateFailed
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import (
CONF_FROM_WINDOW,
@ -41,82 +33,11 @@ from .const import (
)
from .coordinator import OpenUvCoordinator
CONF_ENTRY_ID = "entry_id"
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
SERVICE_NAME_UPDATE_DATA = "update_data"
SERVICE_NAME_UPDATE_PROTECTION_DATA = "update_protection_data"
SERVICE_NAME_UPDATE_UV_INDEX_DATA = "update_uv_index_data"
SERVICES = (
SERVICE_NAME_UPDATE_DATA,
SERVICE_NAME_UPDATE_PROTECTION_DATA,
SERVICE_NAME_UPDATE_UV_INDEX_DATA,
)
@callback
def async_get_entity_id_from_unique_id_suffix(
hass: HomeAssistant, entry: ConfigEntry, unique_id_suffix: str
) -> str:
"""Get the entity ID for a config entry based on unique ID suffix."""
ent_reg = entity_registry.async_get(hass)
[registry_entry] = [
registry_entry
for registry_entry in ent_reg.entities.values()
if registry_entry.config_entry_id == entry.entry_id
and registry_entry.unique_id.endswith(unique_id_suffix)
]
return registry_entry.entity_id
@callback
def async_log_deprecated_service_call(
hass: HomeAssistant,
call: ServiceCall,
alternate_service: str,
alternate_targets: list[str],
breaks_in_ha_version: str,
) -> None:
"""Log a warning about a deprecated service call."""
deprecated_service = f"{call.domain}.{call.service}"
if len(alternate_targets) > 1:
translation_key = "deprecated_service_multiple_alternate_targets"
else:
translation_key = "deprecated_service_single_alternate_target"
async_create_issue(
hass,
DOMAIN,
f"deprecated_service_{deprecated_service}",
breaks_in_ha_version=breaks_in_ha_version,
is_fixable=False,
is_persistent=True,
severity=IssueSeverity.WARNING,
translation_key=translation_key,
translation_placeholders={
"alternate_service": alternate_service,
"alternate_targets": ", ".join(alternate_targets),
"deprecated_service": deprecated_service,
},
)
LOGGER.warning(
(
'The "%s" service is deprecated and will be removed in %s; review the '
"Repairs item in the UI for more information"
),
deprecated_service,
breaks_in_ha_version,
)
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up OpenUV as config entry."""
_verify_domain_control = verify_domain_control(hass, DOMAIN)
websession = aiohttp_client.async_get_clientsession(hass)
client = Client(
entry.data[CONF_API_KEY],
@ -162,81 +83,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
# We determine entity IDs needed to help the user migrate from deprecated services:
current_uv_index_entity_id = async_get_entity_id_from_unique_id_suffix(
hass, entry, "current_uv_index"
)
protection_window_entity_id = async_get_entity_id_from_unique_id_suffix(
hass, entry, "protection_window"
)
@_verify_domain_control
async def update_data(call: ServiceCall) -> None:
"""Refresh all OpenUV data."""
LOGGER.debug("Refreshing all OpenUV data")
async_log_deprecated_service_call(
hass,
call,
"homeassistant.update_entity",
[protection_window_entity_id, current_uv_index_entity_id],
"2022.12.0",
)
tasks = [coordinator.async_refresh() for coordinator in coordinators.values()]
try:
await asyncio.gather(*tasks)
except UpdateFailed as err:
raise HomeAssistantError(err) from err
@_verify_domain_control
async def update_uv_index_data(call: ServiceCall) -> None:
"""Refresh OpenUV UV index data."""
LOGGER.debug("Refreshing OpenUV UV index data")
async_log_deprecated_service_call(
hass,
call,
"homeassistant.update_entity",
[current_uv_index_entity_id],
"2022.12.0",
)
try:
await coordinators[DATA_UV].async_request_refresh()
except UpdateFailed as err:
raise HomeAssistantError(err) from err
@_verify_domain_control
async def update_protection_data(call: ServiceCall) -> None:
"""Refresh OpenUV protection window data."""
LOGGER.debug("Refreshing OpenUV protection window data")
async_log_deprecated_service_call(
hass,
call,
"homeassistant.update_entity",
[protection_window_entity_id],
"2022.12.0",
)
try:
await coordinators[DATA_PROTECTION_WINDOW].async_request_refresh()
except UpdateFailed as err:
raise HomeAssistantError(err) from err
service_schema = vol.Schema(
{
vol.Optional(CONF_ENTRY_ID, default=entry.entry_id): cv.string,
}
)
for service, method in (
(SERVICE_NAME_UPDATE_DATA, update_data),
(SERVICE_NAME_UPDATE_UV_INDEX_DATA, update_uv_index_data),
(SERVICE_NAME_UPDATE_PROTECTION_DATA, update_protection_data),
):
if hass.services.has_service(DOMAIN, service):
continue
hass.services.async_register(DOMAIN, service, method, schema=service_schema)
return True
@ -246,17 +92,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if unload_ok:
hass.data[DOMAIN].pop(entry.entry_id)
loaded_entries = [
entry
for entry in hass.config_entries.async_entries(DOMAIN)
if entry.state == ConfigEntryState.LOADED
]
if len(loaded_entries) == 1:
# If this is the last loaded instance of OpenUV, deregister any services
# defined during integration setup:
for service_name in SERVICES:
hass.services.async_remove(DOMAIN, service_name)
return unload_ok

View File

@ -1,36 +0,0 @@
# Describes the format for available OpenUV services
update_data:
name: Update data
description: Request new data from OpenUV. Consumes two API calls.
fields:
entry_id:
name: Config Entry
description: The configured instance of the OpenUV integration to use
required: true
selector:
config_entry:
integration: openuv
update_uv_index_data:
name: Update UV index data
description: Request new UV index data from OpenUV.
fields:
entry_id:
name: Config Entry
description: The configured instance of the OpenUV integration to use
required: true
selector:
config_entry:
integration: openuv
update_protection_data:
name: Update protection data
description: Request new protection window data from OpenUV.
fields:
entry_id:
name: Config Entry
description: The configured instance of the OpenUV integration to use
required: true
selector:
config_entry:
integration: openuv

View File

@ -56,8 +56,6 @@ def data_uv_index_fixture():
async def setup_openuv_fixture(hass, config, data_protection_window, data_uv_index):
"""Define a fixture to set up OpenUV."""
with patch(
"homeassistant.components.openuv.async_get_entity_id_from_unique_id_suffix",
), patch(
"homeassistant.components.openuv.Client.uv_index", return_value=data_uv_index
), patch(
"homeassistant.components.openuv.Client.uv_protection_window",