Skip to content

Commit d08041a

Browse files
authored
Fix gateway address estimation (diyhue#992)
1 parent 5e1eaca commit d08041a

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

BridgeEmulator/configManager/configInit.py

+34-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,46 @@
1+
import logManager
12
import uuid
3+
import netifaces
24
from random import randrange
3-
5+
logging = logManager.logger.get_logger(__name__)
46

57
def _generate_unique_id():
68
rand_bytes = [randrange(0, 256) for _ in range(3)]
79
return "00:17:88:01:00:%02x:%02x:%02x-0b" % (rand_bytes[0],rand_bytes[1],rand_bytes[2])
810

9-
1011
def write_args(args, yaml_config):
11-
host_ip = args["HOST_IP"]
12-
ip_pieces = host_ip.split(".")
13-
yaml_config["config"]["ipaddress"] = host_ip
14-
yaml_config["config"]["gateway"] = ip_pieces[0] + "." + ip_pieces[1] + "." + ip_pieces[2] + ".1"
15-
yaml_config["config"]["mac"] = args["FULLMAC"]
16-
yaml_config["config"]["bridgeid"] = (args["MAC"][:6] + 'FFFE' + args["MAC"][-6:]).upper()
17-
return yaml_config
12+
host_ip = args['HOST_IP']
13+
14+
devices = []
15+
for interface in netifaces.interfaces():
16+
for family, addresses in netifaces.ifaddresses(interface).items():
17+
if family not in (netifaces.AF_INET, netifaces.AF_INET6):
18+
continue
19+
for address in addresses:
20+
if address['addr'] == host_ip:
21+
devices.append((family, interface))
22+
logging.debug('Found network devices ' + str(devices))
1823

24+
gateway_ips = []
25+
for family, gateways in netifaces.gateways().items():
26+
for device in devices:
27+
if family != device[0]:
28+
continue
29+
for gateway in gateways:
30+
if gateway[1] == device[1]:
31+
gateway_ips.append(gateway[0])
32+
logging.debug('Found gateways ' + str(gateway_ips))
33+
34+
if not gateway_ips:
35+
ip_pieces = host_ip.split('.')
36+
gateway_ips.append(ip_pieces[0] + '.' + ip_pieces[1] + '.' + ip_pieces[2] + '.1')
37+
logging.info('Found no gateways and use fallback ' + str(gateway_ips))
38+
39+
yaml_config['config']['ipaddress'] = host_ip
40+
yaml_config['config']['gateway'] = gateway_ips[0]
41+
yaml_config['config']['mac'] = args['FULLMAC']
42+
yaml_config['config']['bridgeid'] = (args['MAC'][:6] + 'FFFE' + args['MAC'][-6:]).upper()
43+
return yaml_config
1944

2045
def generate_security_key(yaml_config):
2146
# generate security key for Hue Essentials remote access

BridgeEmulator/flaskUI/v2restapi.py

+13-32
Original file line numberDiff line numberDiff line change
@@ -142,24 +142,17 @@ def v2BridgeHome():
142142

143143

144144
def v2Bridge():
145+
bridge_id = bridgeConfig["config"]["bridgeid"]
145146
return {
146-
"bridge_id": bridgeConfig["config"]["bridgeid"].lower(),
147-
"id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'bridge')),
147+
"bridge_id": bridge_id.lower(),
148+
"id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'bridge')),
148149
"id_v1": "",
149150
"identify": {},
150-
"owner": {
151-
"rid": str(uuid.uuid5(
152-
uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'device')),
153-
"rtype": "device"
154-
},
155-
"time_zone": {
156-
"time_zone": bridgeConfig["config"]["timezone"]
157-
},
158-
151+
"owner": {"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'device')), "rtype": "device"},
152+
"time_zone": {"time_zone": bridgeConfig["config"]["timezone"]},
159153
"type": "bridge"
160154
}
161155

162-
163156
def geoLocation():
164157
return {
165158
"id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'geolocation')),
@@ -169,38 +162,26 @@ def geoLocation():
169162

170163

171164
def v2BridgeDevice():
172-
result = {"id": str(uuid.uuid5(
173-
uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'device')), "type": "device"}
165+
config = bridgeConfig["config"]
166+
bridge_id = config["bridgeid"]
167+
result = {"id": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'device')), "type": "device"}
174168
result["id_v1"] = ""
175-
result["metadata"] = {
176-
"archetype": "bridge_v2",
177-
"name": bridgeConfig["config"]["name"]
178-
}
169+
result["metadata"] = {"archetype": "bridge_v2", "name": config["name"]}
179170
result["product_data"] = {
180171
"certified": True,
181172
"manufacturer_name": "Signify Netherlands B.V.",
182173
"model_id": "BSB002",
183174
"product_archetype": "bridge_v2",
184175
"product_name": "Philips hue",
185-
"software_version": bridgeConfig["config"]["apiversion"][:5] + bridgeConfig["config"]["swversion"]
176+
"software_version": config["apiversion"][:5] + config["swversion"]
186177
}
187178
result["services"] = [
188-
{
189-
"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'bridge')),
190-
"rtype": "bridge"
191-
},
192-
{
193-
"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'zigbee_connectivity')),
194-
"rtype": "zigbee_connectivity"
195-
},
196-
{
197-
"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridgeConfig["config"]["bridgeid"] + 'entertainment')),
198-
"rtype": "entertainment"
199-
}
179+
{"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'bridge')), "rtype": "bridge"},
180+
{"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'zigbee_connectivity')), "rtype": "zigbee_connectivity"},
181+
{"rid": str(uuid.uuid5(uuid.NAMESPACE_URL, bridge_id + 'entertainment')), "rtype": "entertainment"}
200182
]
201183
return result
202184

203-
204185
class AuthV1(Resource):
205186
def get(self):
206187
authorisation = authorizeV2(request.headers)

requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ yeelight
1717
python-kasa==0.4.1
1818
bleak==0.14.3
1919
rgbxy==0.5
20-
20+
netifaces==0.11.0

0 commit comments

Comments
 (0)