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