avoid 'referenced before assignment' exception (#150)

This commit is contained in:
Kevron Rees 2019-01-07 13:28:05 -08:00 committed by Teemu R
parent 29c91151e3
commit 8034c47b89

View File

@ -42,6 +42,7 @@ class TPLinkSmartHomeProtocol:
request = json.dumps(request) request = json.dumps(request)
timeout = TPLinkSmartHomeProtocol.DEFAULT_TIMEOUT timeout = TPLinkSmartHomeProtocol.DEFAULT_TIMEOUT
sock = None
try: try:
sock = socket.create_connection((host, port), timeout) sock = socket.create_connection((host, port), timeout)
@ -63,14 +64,17 @@ class TPLinkSmartHomeProtocol:
finally: finally:
try: try:
sock.shutdown(socket.SHUT_RDWR) if sock:
sock.shutdown(socket.SHUT_RDWR)
except OSError: except OSError:
# OSX raises OSError when shutdown() gets called on a closed # OSX raises OSError when shutdown() gets called on a closed
# socket. We ignore it here as the data has already been read # socket. We ignore it here as the data has already been read
# into the buffer at this point. # into the buffer at this point.
pass pass
finally: finally:
sock.close() if sock:
sock.close()
response = TPLinkSmartHomeProtocol.decrypt(buffer[4:]) response = TPLinkSmartHomeProtocol.decrypt(buffer[4:])
_LOGGER.debug("< (%i) %s", len(response), response) _LOGGER.debug("< (%i) %s", len(response), response)