Remove previously-deprecated OpenUV services (#81055)
This commit is contained in:
parent
1967ce67d7
commit
4e8e53f357
3 changed files with 4 additions and 207 deletions
|
@ -5,9 +5,8 @@ import asyncio
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from pyopenuv import Client
|
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 (
|
from homeassistant.const import (
|
||||||
CONF_API_KEY,
|
CONF_API_KEY,
|
||||||
CONF_BINARY_SENSORS,
|
CONF_BINARY_SENSORS,
|
||||||
|
@ -17,17 +16,10 @@ from homeassistant.const import (
|
||||||
CONF_SENSORS,
|
CONF_SENSORS,
|
||||||
Platform,
|
Platform,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.exceptions import HomeAssistantError
|
from homeassistant.helpers import aiohttp_client
|
||||||
from homeassistant.helpers import (
|
|
||||||
aiohttp_client,
|
|
||||||
config_validation as cv,
|
|
||||||
entity_registry,
|
|
||||||
)
|
|
||||||
from homeassistant.helpers.entity import EntityDescription
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
||||||
from homeassistant.helpers.service import verify_domain_control
|
|
||||||
from homeassistant.helpers.update_coordinator import CoordinatorEntity, UpdateFailed
|
|
||||||
|
|
||||||
from .const import (
|
from .const import (
|
||||||
CONF_FROM_WINDOW,
|
CONF_FROM_WINDOW,
|
||||||
|
@ -41,82 +33,11 @@ from .const import (
|
||||||
)
|
)
|
||||||
from .coordinator import OpenUvCoordinator
|
from .coordinator import OpenUvCoordinator
|
||||||
|
|
||||||
CONF_ENTRY_ID = "entry_id"
|
|
||||||
|
|
||||||
PLATFORMS = [Platform.BINARY_SENSOR, Platform.SENSOR]
|
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:
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
"""Set up OpenUV as config entry."""
|
"""Set up OpenUV as config entry."""
|
||||||
_verify_domain_control = verify_domain_control(hass, DOMAIN)
|
|
||||||
|
|
||||||
websession = aiohttp_client.async_get_clientsession(hass)
|
websession = aiohttp_client.async_get_clientsession(hass)
|
||||||
client = Client(
|
client = Client(
|
||||||
entry.data[CONF_API_KEY],
|
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)
|
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
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@ -246,17 +92,6 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||||
if unload_ok:
|
if unload_ok:
|
||||||
hass.data[DOMAIN].pop(entry.entry_id)
|
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
|
return unload_ok
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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
|
|
|
@ -56,8 +56,6 @@ def data_uv_index_fixture():
|
||||||
async def setup_openuv_fixture(hass, config, data_protection_window, data_uv_index):
|
async def setup_openuv_fixture(hass, config, data_protection_window, data_uv_index):
|
||||||
"""Define a fixture to set up OpenUV."""
|
"""Define a fixture to set up OpenUV."""
|
||||||
with patch(
|
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
|
"homeassistant.components.openuv.Client.uv_index", return_value=data_uv_index
|
||||||
), patch(
|
), patch(
|
||||||
"homeassistant.components.openuv.Client.uv_protection_window",
|
"homeassistant.components.openuv.Client.uv_protection_window",
|
||||||
|
|
Loading…
Add table
Reference in a new issue