Skip to content
Merged
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions xiaomi_gateway/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class XiaomiGatewayDiscovery(object):
GATEWAY_DISCOVERY_PORT = 4321
SOCKET_BUFSIZE = 1024

disabled_gateways = []
gateways = defaultdict(list)

def __init__(self, callback_func, gateways_config, interface):
Expand Down Expand Up @@ -57,6 +58,11 @@ def discover_gateways(self):

try:
ip_address = socket.gethostbyname(host)
if gateway.get('disabled'):
_LOGGER.info(
'Xiaomi Gateway %s is disabled in the configuration', sid)
self.disabled_gateways.append(ip_address)
continue
_LOGGER.info(
'Xiaomi Gateway %s configured at IP %s:%s',
sid, ip_address, port)
Expand Down Expand Up @@ -88,20 +94,29 @@ def discover_gateways(self):
if ip_add in self.gateways:
continue

disabled = False
gateway_key = None
for gateway in self._gateways_config:
sid = gateway['sid']
key = gateway['key']
sid = gateway.get('sid')
key = gateway.get('key')
_disabled = gateway.get('disabled')
if sid is None or sid == resp["sid"]:
gateway_key = key
if sid and sid == resp['sid'] and _disabled:
disabled = True

sid = resp["sid"]
port = resp["port"]

_LOGGER.info('Xiaomi Gateway %s found at IP %s', sid, ip_add)

self.gateways[ip_add] = XiaomiGateway(ip_add, port, sid, gateway_key, self._socket,
resp["proto_version"] if "proto_version" in resp else None)
if disabled:
_LOGGER.info("Xiaomi Gateway %s is disabled in the configuration",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get

sid)
self.disabled_gateways.append(ip_add)
else:
_LOGGER.info('Xiaomi Gateway %s found at IP %s', sid, ip_add)
self.gateways[ip_add] = XiaomiGateway(
ip_add, port, sid, gateway_key, self._socket,
resp["proto_version"] if "proto_version" in resp else None)

except socket.timeout:
_LOGGER.info("Gateway discovery finished in 5 seconds")
Expand Down Expand Up @@ -165,7 +180,8 @@ def _listen_to_msg(self):
data = json.loads(data.decode("ascii"))
gateway = self.gateways.get(ip_add)
if gateway is None:
_LOGGER.error('Unknown gateway ip %s', ip_add)
if ip_add not in self.disabled_gateways:
_LOGGER.error('Unknown gateway ip %s', ip_add)
continue

cmd = data['cmd']
Expand Down Expand Up @@ -423,7 +439,7 @@ def _get_value(resp, data_key=None):
if data_key in param:
return param[data_key]
return None
return data[data_key]
return data.get(data_key)


def _list2map(data):
Expand Down