1919from .const import DOMAIN , SERVICE_EXECUTE , CONF_KEY_FILE , CONF_INPUT , CONST_DEFAULT_TIMEOUT , \
2020 CONF_CHECK_KNOWN_HOSTS , CONF_KNOWN_HOSTS , CONF_CLIENT_KEYS , CONF_CHECK , CONF_OUTPUT , CONF_EXIT_STATUS
2121
22- CONFIG_SCHEMA = cv .empty_config_schema (DOMAIN )
22+ CONFIG_SCHEMA = cv .empty_config_schema (DOMAIN ) # pylint: disable=invalid-name
2323
2424
2525async def _validate_service_data (data : dict [str , Any ]) -> None :
@@ -77,7 +77,20 @@ async def _validate_service_data(data: dict[str, Any]) -> None:
7777)
7878
7979
80- async def async_setup (hass : HomeAssistant , config : ConfigType ) -> bool :
80+ async def _resolve_known_hosts (hass : HomeAssistant , check_known_hosts : bool , known_hosts : str | None ) -> str | None :
81+ """Resolve the known_hosts value for the SSH connection."""
82+ if not check_known_hosts :
83+ return None
84+ if not known_hosts :
85+ known_hosts = str (Path ('~' , '.ssh' , CONF_KNOWN_HOSTS ).expanduser ())
86+ if await exists (known_hosts ):
87+ # open the known hosts file asynchronously, otherwise Home Assistant will complain about blocking I/O
88+ return await hass .async_add_executor_job (read_known_hosts , known_hosts )
89+ return known_hosts
90+
91+
92+ async def async_setup (hass : HomeAssistant , _config : ConfigType ) -> bool :
93+ """Set up the SSH Command integration."""
8194 async def async_execute (service_call : ServiceCall ) -> ServiceResponse :
8295 await _validate_service_data (service_call .data )
8396 host = service_call .data .get (CONF_HOST )
@@ -101,19 +114,9 @@ async def async_execute(service_call: ServiceCall) -> ServiceResponse:
101114 CONF_USERNAME : username ,
102115 CONF_PASSWORD : password ,
103116 CONF_CLIENT_KEYS : key_file ,
117+ CONF_KNOWN_HOSTS : await _resolve_known_hosts (hass , check_known_hosts , known_hosts ),
104118 }
105119
106- if check_known_hosts :
107- if not known_hosts :
108- known_hosts = str (Path ('~' , '.ssh' , CONF_KNOWN_HOSTS ).expanduser ())
109- if await exists (known_hosts ):
110- # open the known hosts file asynchronously, otherwise Home Assistant will complain about blocking I/O
111- conn_kwargs [CONF_KNOWN_HOSTS ] = await hass .async_add_executor_job (read_known_hosts , known_hosts )
112- else :
113- conn_kwargs [CONF_KNOWN_HOSTS ] = known_hosts
114- else :
115- conn_kwargs [CONF_KNOWN_HOSTS ] = None
116-
117120 run_kwargs = {
118121 CONF_COMMAND : command ,
119122 CONF_CHECK : False ,
@@ -126,32 +129,32 @@ async def async_execute(service_call: ServiceCall) -> ServiceResponse:
126129 try :
127130 async with connect (** conn_kwargs ) as conn :
128131 result = await conn .run (** run_kwargs )
129- except HostKeyNotVerifiable :
132+ except HostKeyNotVerifiable as exc :
130133 raise ServiceValidationError (
131134 "The host key could not be verified." ,
132135 translation_domain = DOMAIN ,
133136 translation_key = "host_key_not_verifiable" ,
134- )
135- except PermissionDenied :
137+ ) from exc
138+ except PermissionDenied as exc :
136139 raise ServiceValidationError (
137140 "SSH login failed." ,
138141 translation_domain = DOMAIN ,
139142 translation_key = "login_failed" ,
140- )
141- except TimeoutError :
143+ ) from exc
144+ except TimeoutError as exc :
142145 raise ServiceValidationError (
143146 "Connection timed out." ,
144147 translation_domain = DOMAIN ,
145148 translation_key = "connection_timed_out" ,
146- )
149+ ) from exc
147150 except OSError as e :
148151 if e .strerror == 'Temporary failure in name resolution' :
149152 raise ServiceValidationError (
150153 "Host is not reachable." ,
151154 translation_domain = DOMAIN ,
152155 translation_key = "host_not_reachable" ,
153- )
154- raise e
156+ ) from e
157+ raise
155158
156159 return {
157160 CONF_OUTPUT : result .stdout ,
@@ -170,11 +173,11 @@ async def async_execute(service_call: ServiceCall) -> ServiceResponse:
170173 return True
171174
172175
173- async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
176+ async def async_setup_entry (_hass : HomeAssistant , _entry : ConfigEntry ) -> bool :
174177 """Set up SSH Command from a config entry. Nothing to do here."""
175178 return True
176179
177180
178- async def async_unload_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
181+ async def async_unload_entry (_hass : HomeAssistant , _entry : ConfigEntry ) -> bool :
179182 """Unload a config entry. Nothing to do here."""
180183 return True
0 commit comments