Deprecate Harmony switch platform (#92787)

* Deprecate Harmony switches

* uno mas

* add test for issues

* switch to remote

* uno mas
This commit is contained in:
Robert Hillis 2023-11-24 05:54:43 -05:00 committed by GitHub
parent e9dd158a8d
commit 76427a0080
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 104 additions and 2 deletions

View file

@ -42,6 +42,16 @@
}
}
},
"issues": {
"deprecated_switches": {
"title": "The Logitech Harmony switch platform is being removed",
"description": "Using the switch platform to change the current activity is now deprecated and will be removed in a future version of Home Assistant.\n\nPlease adjust any automations or scripts that use switch entities to instead use the select entity."
},
"deprecated_switches_entity": {
"title": "Deprecated Harmony entity detected in {info}",
"description": "Your Harmony entity `{entity}` is being used in `{info}`. A select entity is available and should be used going forward.\n\nPlease adjust `{info}` to fix this issue."
}
},
"services": {
"sync": {
"name": "Sync",

View file

@ -1,12 +1,15 @@
"""Support for Harmony Hub activities."""
import logging
from typing import Any
from typing import Any, cast
from homeassistant.components.switch import SwitchEntity
from homeassistant.components.automation import automations_with_entity
from homeassistant.components.script import scripts_with_entity
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from .const import DOMAIN, HARMONY_DATA
from .data import HarmonyData
@ -20,6 +23,15 @@ async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up harmony activity switches."""
async_create_issue(
hass,
DOMAIN,
"deprecated_switches",
breaks_in_ha_version="2023.8.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_switches",
)
data = hass.data[DOMAIN][entry.entry_id][HARMONY_DATA]
activities = data.activities
@ -72,6 +84,22 @@ class HarmonyActivitySwitch(HarmonyEntity, SwitchEntity):
)
)
)
entity_automations = automations_with_entity(self.hass, self.entity_id)
entity_scripts = scripts_with_entity(self.hass, self.entity_id)
for item in entity_automations + entity_scripts:
async_create_issue(
self.hass,
DOMAIN,
f"deprecated_switches_{self.entity_id}_{item}",
breaks_in_ha_version="2023.8.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_switches_entity",
translation_placeholders={
"entity": f"{SWITCH_DOMAIN}.{cast(str, self.name).lower().replace(' ', '_')}",
"info": item,
},
)
@callback
def _async_activity_update(self, activity_info: tuple):

View file

@ -1,7 +1,10 @@
"""Test the Logitech Harmony Hub activity switches."""
from datetime import timedelta
from homeassistant.components import automation, script
from homeassistant.components.automation import automations_with_entity
from homeassistant.components.harmony.const import DOMAIN
from homeassistant.components.script import scripts_with_entity
from homeassistant.components.switch import (
DOMAIN as SWITCH_DOMAIN,
SERVICE_TURN_OFF,
@ -17,6 +20,8 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
import homeassistant.helpers.issue_registry as ir
from homeassistant.setup import async_setup_component
from homeassistant.util import utcnow
from .const import ENTITY_PLAY_MUSIC, ENTITY_REMOTE, ENTITY_WATCH_TV, HUB_NAME
@ -133,3 +138,62 @@ async def _toggle_switch_and_wait(hass, service_name, entity):
blocking=True,
)
await hass.async_block_till_done()
async def test_create_issue(
harmony_client,
mock_hc,
hass: HomeAssistant,
mock_write_config,
entity_registry_enabled_by_default: None,
) -> None:
"""Test we create an issue when an automation or script is using a deprecated entity."""
assert await async_setup_component(
hass,
automation.DOMAIN,
{
automation.DOMAIN: {
"alias": "test",
"trigger": {"platform": "state", "entity_id": ENTITY_WATCH_TV},
"action": {"service": "switch.turn_on", "entity_id": ENTITY_WATCH_TV},
}
},
)
assert await async_setup_component(
hass,
script.DOMAIN,
{
script.DOMAIN: {
"test": {
"sequence": [
{
"service": "switch.turn_on",
"data": {"entity_id": ENTITY_WATCH_TV},
},
],
}
}
},
)
entry = MockConfigEntry(
domain=DOMAIN, data={CONF_HOST: "192.0.2.0", CONF_NAME: HUB_NAME}
)
entry.add_to_hass(hass)
await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert automations_with_entity(hass, ENTITY_WATCH_TV)[0] == "automation.test"
assert scripts_with_entity(hass, ENTITY_WATCH_TV)[0] == "script.test"
issue_registry: ir.IssueRegistry = ir.async_get(hass)
assert issue_registry.async_get_issue(DOMAIN, "deprecated_switches")
assert issue_registry.async_get_issue(
DOMAIN, "deprecated_switches_switch.guest_room_watch_tv_automation.test"
)
assert issue_registry.async_get_issue(
DOMAIN, "deprecated_switches_switch.guest_room_watch_tv_script.test"
)
assert len(issue_registry.issues) == 3