diff --git a/homeassistant/components/isy994/binary_sensor.py b/homeassistant/components/isy994/binary_sensor.py index dc32fcef230..6355a9bcece 100644 --- a/homeassistant/components/isy994/binary_sensor.py +++ b/homeassistant/components/isy994/binary_sensor.py @@ -49,7 +49,6 @@ from .const import ( ) from .entity import ISYNodeEntity, ISYProgramEntity from .helpers import migrate_old_unique_ids -from .services import async_setup_device_services DEVICE_PARENT_REQUIRED = [ DEVICE_CLASS_OPENING, @@ -172,7 +171,6 @@ async def async_setup_entry( await migrate_old_unique_ids(hass, BINARY_SENSOR, devices) async_add_entities(devices) - async_setup_device_services(hass) def _detect_device_type_and_class(node: Union[Group, Node]) -> (str, str): diff --git a/homeassistant/components/isy994/climate.py b/homeassistant/components/isy994/climate.py index 7dfd9a083d3..bb98c3d31bf 100644 --- a/homeassistant/components/isy994/climate.py +++ b/homeassistant/components/isy994/climate.py @@ -52,7 +52,6 @@ from .const import ( ) from .entity import ISYNodeEntity from .helpers import convert_isy_value_to_hass, migrate_old_unique_ids -from .services import async_setup_device_services ISY_SUPPORTED_FEATURES = ( SUPPORT_FAN_MODE | SUPPORT_TARGET_TEMPERATURE | SUPPORT_TARGET_TEMPERATURE_RANGE @@ -73,7 +72,6 @@ async def async_setup_entry( await migrate_old_unique_ids(hass, CLIMATE, entities) async_add_entities(entities) - async_setup_device_services(hass) class ISYThermostatEntity(ISYNodeEntity, ClimateEntity): diff --git a/homeassistant/components/isy994/cover.py b/homeassistant/components/isy994/cover.py index 41273f61f01..51a26585778 100644 --- a/homeassistant/components/isy994/cover.py +++ b/homeassistant/components/isy994/cover.py @@ -24,7 +24,6 @@ from .const import ( ) from .entity import ISYNodeEntity, ISYProgramEntity from .helpers import migrate_old_unique_ids -from .services import async_setup_device_services async def async_setup_entry( @@ -43,7 +42,6 @@ async def async_setup_entry( await migrate_old_unique_ids(hass, COVER, devices) async_add_entities(devices) - async_setup_device_services(hass) class ISYCoverEntity(ISYNodeEntity, CoverEntity): diff --git a/homeassistant/components/isy994/fan.py b/homeassistant/components/isy994/fan.py index 96aa2144b1c..384ff22403a 100644 --- a/homeassistant/components/isy994/fan.py +++ b/homeassistant/components/isy994/fan.py @@ -18,7 +18,6 @@ from homeassistant.helpers.typing import HomeAssistantType from .const import _LOGGER, DOMAIN as ISY994_DOMAIN, ISY994_NODES, ISY994_PROGRAMS from .entity import ISYNodeEntity, ISYProgramEntity from .helpers import migrate_old_unique_ids -from .services import async_setup_device_services VALUE_TO_STATE = { 0: SPEED_OFF, @@ -51,7 +50,6 @@ async def async_setup_entry( await migrate_old_unique_ids(hass, FAN, devices) async_add_entities(devices) - async_setup_device_services(hass) class ISYFanEntity(ISYNodeEntity, FanEntity): diff --git a/homeassistant/components/isy994/light.py b/homeassistant/components/isy994/light.py index 8d58a7f5796..7f94c68714d 100644 --- a/homeassistant/components/isy994/light.py +++ b/homeassistant/components/isy994/light.py @@ -20,7 +20,7 @@ from .const import ( ) from .entity import ISYNodeEntity from .helpers import migrate_old_unique_ids -from .services import async_setup_device_services, async_setup_light_services +from .services import async_setup_light_services ATTR_LAST_BRIGHTNESS = "last_brightness" @@ -41,7 +41,6 @@ async def async_setup_entry( await migrate_old_unique_ids(hass, LIGHT, devices) async_add_entities(devices) - async_setup_device_services(hass) async_setup_light_services(hass) diff --git a/homeassistant/components/isy994/lock.py b/homeassistant/components/isy994/lock.py index da50e4e704a..ceb26f3044c 100644 --- a/homeassistant/components/isy994/lock.py +++ b/homeassistant/components/isy994/lock.py @@ -10,7 +10,6 @@ from homeassistant.helpers.typing import HomeAssistantType from .const import _LOGGER, DOMAIN as ISY994_DOMAIN, ISY994_NODES, ISY994_PROGRAMS from .entity import ISYNodeEntity, ISYProgramEntity from .helpers import migrate_old_unique_ids -from .services import async_setup_device_services VALUE_TO_STATE = {0: False, 100: True} @@ -31,7 +30,6 @@ async def async_setup_entry( await migrate_old_unique_ids(hass, LOCK, devices) async_add_entities(devices) - async_setup_device_services(hass) class ISYLockEntity(ISYNodeEntity, LockEntity): diff --git a/homeassistant/components/isy994/sensor.py b/homeassistant/components/isy994/sensor.py index 8ae646b2791..b4eba10fd34 100644 --- a/homeassistant/components/isy994/sensor.py +++ b/homeassistant/components/isy994/sensor.py @@ -19,7 +19,6 @@ from .const import ( ) from .entity import ISYEntity, ISYNodeEntity from .helpers import convert_isy_value_to_hass, migrate_old_unique_ids -from .services import async_setup_device_services async def async_setup_entry( @@ -40,7 +39,6 @@ async def async_setup_entry( await migrate_old_unique_ids(hass, SENSOR, devices) async_add_entities(devices) - async_setup_device_services(hass) class ISYSensorEntity(ISYNodeEntity): diff --git a/homeassistant/components/isy994/services.py b/homeassistant/components/isy994/services.py index f59db1f5716..ee12cedbca4 100644 --- a/homeassistant/components/isy994/services.py +++ b/homeassistant/components/isy994/services.py @@ -13,9 +13,10 @@ from homeassistant.const import ( CONF_UNIT_OF_MEASUREMENT, SERVICE_RELOAD, ) -from homeassistant.core import callback +from homeassistant.core import ServiceCall, callback from homeassistant.helpers import entity_platform import homeassistant.helpers.config_validation as cv +from homeassistant.helpers.entity_platform import async_get_platforms import homeassistant.helpers.entity_registry as er from homeassistant.helpers.typing import HomeAssistantType @@ -353,6 +354,30 @@ def async_setup_services(hass: HomeAssistantType): domain=DOMAIN, service=SERVICE_RELOAD, service_func=async_reload_config_entries ) + async def _async_send_raw_node_command(call: ServiceCall): + await hass.helpers.service.entity_service_call( + async_get_platforms(hass, DOMAIN), SERVICE_SEND_RAW_NODE_COMMAND, call + ) + + hass.services.async_register( + domain=DOMAIN, + service=SERVICE_SEND_RAW_NODE_COMMAND, + schema=cv.make_entity_service_schema(SERVICE_SEND_RAW_NODE_COMMAND_SCHEMA), + service_func=_async_send_raw_node_command, + ) + + async def _async_send_node_command(call: ServiceCall): + await hass.helpers.service.entity_service_call( + async_get_platforms(hass, DOMAIN), SERVICE_SEND_NODE_COMMAND, call + ) + + hass.services.async_register( + domain=DOMAIN, + service=SERVICE_SEND_NODE_COMMAND, + schema=cv.make_entity_service_schema(SERVICE_SEND_NODE_COMMAND_SCHEMA), + service_func=_async_send_node_command, + ) + @callback def async_unload_services(hass: HomeAssistantType): @@ -374,23 +399,8 @@ def async_unload_services(hass: HomeAssistantType): hass.services.async_remove(domain=DOMAIN, service=SERVICE_SET_VARIABLE) hass.services.async_remove(domain=DOMAIN, service=SERVICE_CLEANUP) hass.services.async_remove(domain=DOMAIN, service=SERVICE_RELOAD) - - -@callback -def async_setup_device_services(hass: HomeAssistantType): - """Create device-specific services for the ISY Integration.""" - platform = entity_platform.current_platform.get() - - platform.async_register_entity_service( - SERVICE_SEND_RAW_NODE_COMMAND, - SERVICE_SEND_RAW_NODE_COMMAND_SCHEMA, - SERVICE_SEND_RAW_NODE_COMMAND, - ) - platform.async_register_entity_service( - SERVICE_SEND_NODE_COMMAND, - SERVICE_SEND_NODE_COMMAND_SCHEMA, - SERVICE_SEND_NODE_COMMAND, - ) + hass.services.async_remove(domain=DOMAIN, service=SERVICE_SEND_RAW_NODE_COMMAND) + hass.services.async_remove(domain=DOMAIN, service=SERVICE_SEND_NODE_COMMAND) @callback diff --git a/homeassistant/components/isy994/switch.py b/homeassistant/components/isy994/switch.py index 68a3bdeecd2..28d2264f283 100644 --- a/homeassistant/components/isy994/switch.py +++ b/homeassistant/components/isy994/switch.py @@ -10,7 +10,6 @@ from homeassistant.helpers.typing import HomeAssistantType from .const import _LOGGER, DOMAIN as ISY994_DOMAIN, ISY994_NODES, ISY994_PROGRAMS from .entity import ISYNodeEntity, ISYProgramEntity from .helpers import migrate_old_unique_ids -from .services import async_setup_device_services async def async_setup_entry( @@ -29,7 +28,6 @@ async def async_setup_entry( await migrate_old_unique_ids(hass, SWITCH, devices) async_add_entities(devices) - async_setup_device_services(hass) class ISYSwitchEntity(ISYNodeEntity, SwitchEntity): diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index 32ef88d361f..da1a3635d72 100644 --- a/homeassistant/helpers/entity_platform.py +++ b/homeassistant/helpers/entity_platform.py @@ -595,3 +595,19 @@ class EntityPlatform: current_platform: ContextVar[Optional[EntityPlatform]] = ContextVar( "current_platform", default=None ) + + +@callback +def async_get_platforms( + hass: HomeAssistantType, integration_name: str +) -> List[EntityPlatform]: + """Find existing platforms.""" + if ( + DATA_ENTITY_PLATFORM not in hass.data + or integration_name not in hass.data[DATA_ENTITY_PLATFORM] + ): + return [] + + platforms: List[EntityPlatform] = hass.data[DATA_ENTITY_PLATFORM][integration_name] + + return platforms diff --git a/homeassistant/helpers/reload.py b/homeassistant/helpers/reload.py index 89fcf45c29a..1c11afdb46b 100644 --- a/homeassistant/helpers/reload.py +++ b/homeassistant/helpers/reload.py @@ -9,7 +9,7 @@ from homeassistant.const import SERVICE_RELOAD from homeassistant.core import Event, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_per_platform -from homeassistant.helpers.entity_platform import DATA_ENTITY_PLATFORM, EntityPlatform +from homeassistant.helpers.entity_platform import EntityPlatform, async_get_platforms from homeassistant.helpers.typing import HomeAssistantType from homeassistant.loader import async_get_integration from homeassistant.setup import async_setup_component @@ -141,13 +141,7 @@ def async_get_platform( hass: HomeAssistantType, integration_name: str, integration_platform_name: str ) -> Optional[EntityPlatform]: """Find an existing platform.""" - if ( - DATA_ENTITY_PLATFORM not in hass.data - or integration_name not in hass.data[DATA_ENTITY_PLATFORM] - ): - return None - - for integration_platform in hass.data[DATA_ENTITY_PLATFORM][integration_name]: + for integration_platform in async_get_platforms(hass, integration_name): if integration_platform.domain == integration_platform_name: platform: EntityPlatform = integration_platform return platform