1 """Support for command line notification services."""
3 from __future__
import annotations
7 from typing
import Any, cast
15 from .const
import CONF_COMMAND_TIMEOUT
17 _LOGGER = logging.getLogger(__name__)
23 discovery_info: DiscoveryInfoType |
None =
None,
24 ) -> CommandLineNotificationService |
None:
25 """Get the Command Line notification service."""
26 if not discovery_info:
29 discovery_info = cast(DiscoveryInfoType, discovery_info)
30 notify_config = discovery_info
31 command: str = notify_config[CONF_COMMAND]
32 timeout: int = notify_config[CONF_COMMAND_TIMEOUT]
38 """Implement the notification service for the Command Line service."""
40 def __init__(self, command: str, timeout: int) ->
None:
41 """Initialize the service."""
46 """Send a message to a command line."""
47 with subprocess.Popen(
49 universal_newlines=
True,
50 stdin=subprocess.PIPE,
55 proc.communicate(input=message, timeout=self.
_timeout_timeout)
56 if proc.returncode != 0:
58 "Command failed (with return code %s): %s",
62 except subprocess.TimeoutExpired:
63 _LOGGER.error(
"Timeout for command: %s", self.
commandcommand)
65 except subprocess.SubprocessError:
66 _LOGGER.error(
"Error trying to exec command: %s", self.
commandcommand)
None send_message(self, str message="", **Any kwargs)
None __init__(self, str command, int timeout)
CommandLineNotificationService|None get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
None kill_subprocess(subprocess.Popen[Any] process)