diff --git a/homeassistant/components/rflink/__init__.py b/homeassistant/components/rflink/__init__.py index 95a37c349335..3ddb7fb82cec 100644 --- a/homeassistant/components/rflink/__init__.py +++ b/homeassistant/components/rflink/__init__.py @@ -9,22 +9,13 @@ from rflink.protocol import create_rflink_connection from serial import SerialException from homeassistant.const import ( - CONF_COMMAND, - CONF_DEVICE_ID, CONF_HOST, CONF_PORT, EVENT_HOMEASSISTANT_STOP, EVENT_LOGGING_CHANGED, Platform, ) -from homeassistant.core import ( - CoreState, - Event, - HassJob, - HomeAssistant, - ServiceCall, - callback, -) +from homeassistant.core import CoreState, Event, HassJob, HomeAssistant, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.dispatcher import ( @@ -44,6 +35,7 @@ from .const import ( EVENT_KEY_ID, EVENT_KEY_SENSOR, SIGNAL_AVAILABILITY, + SIGNAL_EVENT, SIGNAL_HANDLE_EVENT, TMP_ENTITY, ) @@ -51,6 +43,7 @@ from .cover import RFLINK_PLATFORM as COVER_PLATFORM from .entity import RflinkCommand from .light import RFLINK_PLATFORM as LIGHT_PLATFORM from .sensor import RFLINK_PLATFORM as SENSOR_PLATFORM +from .services import async_setup_services from .switch import RFLINK_PLATFORM as SWITCH_PLATFORM from .utils import identify_event_type @@ -68,10 +61,6 @@ CONNECTION_TIMEOUT = 10 RFLINK_GROUP_COMMANDS = ["allon", "alloff"] -SERVICE_SEND_COMMAND = "send_command" - -SIGNAL_EVENT = "rflink_event" - BINARY_SENSOR_PS = probatio.Schema( BINARY_SENSOR_PLATFORM, extra=probatio.ALLOW_EXTRA, @@ -124,13 +113,6 @@ CONFIG_SCHEMA = probatio.Schema( extra=probatio.ALLOW_EXTRA, ) -SEND_COMMAND_SCHEMA = probatio.Schema( - { - probatio.Required(CONF_DEVICE_ID): cv.string, - probatio.Required(CONF_COMMAND): cv.string, - } -) - ALLOWED_PLATFORMS = [ Platform.BINARY_SENSOR.value, Platform.COVER.value, @@ -153,28 +135,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: # Allow platform to specify function to register new unknown devices hass.data[DATA_DEVICE_REGISTER] = {} - async def async_send_command(call: ServiceCall) -> None: - """Send Rflink command.""" - _LOGGER.debug("Rflink command for %s", str(call.data)) - if not ( - await RflinkCommand.send_command( - call.data.get(CONF_DEVICE_ID), call.data.get(CONF_COMMAND) - ) - ): - _LOGGER.error("Failed Rflink command for %s", str(call.data)) - else: - async_dispatcher_send( - hass, - SIGNAL_EVENT, - { - EVENT_KEY_ID: call.data.get(CONF_DEVICE_ID), - EVENT_KEY_COMMAND: call.data.get(CONF_COMMAND), - }, - ) - - hass.services.async_register( - DOMAIN, SERVICE_SEND_COMMAND, async_send_command, schema=SEND_COMMAND_SCHEMA - ) + async_setup_services(hass) @callback def event_callback(event): diff --git a/homeassistant/components/rflink/const.py b/homeassistant/components/rflink/const.py index 74d29665ac50..16cd7bb98d5f 100644 --- a/homeassistant/components/rflink/const.py +++ b/homeassistant/components/rflink/const.py @@ -26,6 +26,7 @@ EVENT_KEY_SENSOR = "sensor" EVENT_KEY_UNIT = "unit" SIGNAL_AVAILABILITY = "rflink_device_available" +SIGNAL_EVENT = "rflink_event" SIGNAL_HANDLE_EVENT = "rflink_handle_event_{}" TMP_ENTITY = "tmp.{}" diff --git a/homeassistant/components/rflink/services.py b/homeassistant/components/rflink/services.py new file mode 100644 index 000000000000..6c95ca9ae268 --- /dev/null +++ b/homeassistant/components/rflink/services.py @@ -0,0 +1,52 @@ +"""Services for the Rflink integration.""" + +import logging + +import probatio + +from homeassistant.const import CONF_COMMAND, CONF_DEVICE_ID +from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.dispatcher import async_dispatcher_send + +from .const import DOMAIN, EVENT_KEY_COMMAND, EVENT_KEY_ID, SIGNAL_EVENT +from .entity import RflinkCommand + +_LOGGER = logging.getLogger(__name__) + +SERVICE_SEND_COMMAND = "send_command" + +SEND_COMMAND_SCHEMA = probatio.Schema( + { + probatio.Required(CONF_DEVICE_ID): cv.string, + probatio.Required(CONF_COMMAND): cv.string, + } +) + + +async def _async_send_command(call: ServiceCall) -> None: + """Send Rflink command.""" + _LOGGER.debug("Rflink command for %s", str(call.data)) + if not ( + await RflinkCommand.send_command( + call.data.get(CONF_DEVICE_ID), call.data.get(CONF_COMMAND) + ) + ): + _LOGGER.error("Failed Rflink command for %s", str(call.data)) + else: + async_dispatcher_send( + call.hass, + SIGNAL_EVENT, + { + EVENT_KEY_ID: call.data.get(CONF_DEVICE_ID), + EVENT_KEY_COMMAND: call.data.get(CONF_COMMAND), + }, + ) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up the services for the Rflink integration.""" + hass.services.async_register( + DOMAIN, SERVICE_SEND_COMMAND, _async_send_command, schema=SEND_COMMAND_SCHEMA + ) diff --git a/tests/components/rflink/test_init.py b/tests/components/rflink/test_init.py index 04ae9cfa5f5e..0a84bd22f20b 100644 --- a/tests/components/rflink/test_init.py +++ b/tests/components/rflink/test_init.py @@ -14,10 +14,10 @@ from homeassistant.components.rflink import ( DOMAIN, EVENT_KEY_COMMAND, EVENT_KEY_SENSOR, - SERVICE_SEND_COMMAND, TMP_ENTITY, RflinkCommand, ) +from homeassistant.components.rflink.services import SERVICE_SEND_COMMAND from homeassistant.const import ( ATTR_ENTITY_ID, CONF_HOST,