Handle elkm1 login case with username and insecure login (#67602)

This commit is contained in:
J. Nick Koston 2022-03-04 12:30:40 -10:00 committed by GitHub
parent 1ebe82fc4b
commit e4221336dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 13 deletions

View File

@ -279,9 +279,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
keypad.add_callback(_element_changed) keypad.add_callback(_element_changed)
try: try:
if not await async_wait_for_elk_to_sync( if not await async_wait_for_elk_to_sync(elk, LOGIN_TIMEOUT, SYNC_TIMEOUT):
elk, LOGIN_TIMEOUT, SYNC_TIMEOUT, bool(conf[CONF_USERNAME])
):
return False return False
except asyncio.TimeoutError as exc: except asyncio.TimeoutError as exc:
raise ConfigEntryNotReady(f"Timed out connecting to {conf[CONF_HOST]}") from exc raise ConfigEntryNotReady(f"Timed out connecting to {conf[CONF_HOST]}") from exc
@ -334,7 +332,6 @@ async def async_wait_for_elk_to_sync(
elk: elkm1.Elk, elk: elkm1.Elk,
login_timeout: int, login_timeout: int,
sync_timeout: int, sync_timeout: int,
password_auth: bool,
) -> bool: ) -> bool:
"""Wait until the elk has finished sync. Can fail login or timeout.""" """Wait until the elk has finished sync. Can fail login or timeout."""
@ -354,18 +351,23 @@ async def async_wait_for_elk_to_sync(
login_event.set() login_event.set()
sync_event.set() sync_event.set()
def first_response(*args, **kwargs):
_LOGGER.debug("ElkM1 received first response (VN)")
login_event.set()
def sync_complete(): def sync_complete():
sync_event.set() sync_event.set()
success = True success = True
elk.add_handler("login", login_status) elk.add_handler("login", login_status)
# VN is the first command sent for panel, when we get
# it back we now we are logged in either with or without a password
elk.add_handler("VN", first_response)
elk.add_handler("sync_complete", sync_complete) elk.add_handler("sync_complete", sync_complete)
events = [] for name, event, timeout in (
if password_auth: ("login", login_event, login_timeout),
events.append(("login", login_event, login_timeout)) ("sync_complete", sync_event, sync_timeout),
events.append(("sync_complete", sync_event, sync_timeout)) ):
for name, event, timeout in events:
_LOGGER.debug("Waiting for %s event for %s seconds", name, timeout) _LOGGER.debug("Waiting for %s event for %s seconds", name, timeout)
try: try:
async with async_timeout.timeout(timeout): async with async_timeout.timeout(timeout):

View File

@ -81,9 +81,7 @@ async def validate_input(data: dict[str, str], mac: str | None) -> dict[str, str
) )
elk.connect() elk.connect()
if not await async_wait_for_elk_to_sync( if not await async_wait_for_elk_to_sync(elk, LOGIN_TIMEOUT, VALIDATE_TIMEOUT):
elk, LOGIN_TIMEOUT, VALIDATE_TIMEOUT, bool(userid)
):
raise InvalidAuth raise InvalidAuth
short_mac = _short_mac(mac) if mac else None short_mac = _short_mac(mac) if mac else None