Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT: allow selective inclusion of MQTT devices #1001

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
35 changes: 33 additions & 2 deletions BridgeEmulator/services/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,29 @@ def getObject(friendly_name):
logging.debug("Device not found for " + friendly_name)
return False

def should_include_device(device_name, device_id):
if not bridgeConfig['config']['mqtt']['explicitInclude']:
return True
return device_name in bridgeConfig['config']['mqtt']['includeList'] or device_id in bridgeConfig['config']['mqtt']['includeList']

def should_include_discovery_device(msg):
data = json.loads(msg.payload)
if not bridgeConfig['config']['mqtt']['includeGroups'] and data['device']['model'] == "Group":
logging.info("Ignoring discovered group on: " + msg.topic)
return False

if bridgeConfig['config']['mqtt']['explicitInclude']:
device_name = data['device']['name']
device_id = data['unique_id'].split('_')[0]
return should_include_device(device_name, device_id)

return True

# Will get called zero or more times depending on how many lights are available for autodiscovery
def on_autodiscovery_light(msg):
if not should_include_discovery_device(msg):
return

data = json.loads(msg.payload)
logging.info("Auto discovery message on: " + msg.topic)
#logging.debug(json.dumps(data, indent=4))
Expand Down Expand Up @@ -258,8 +279,6 @@ def on_autodiscovery_light(msg):

addNewLight(modelid, lightName, "mqtt", protocol_cfg)



def on_state_update(msg):
logging.debug("MQTT: got state message on " + msg.topic)
data = json.loads(msg.payload)
Expand All @@ -278,6 +297,11 @@ def on_message(client, userdata, msg):
on_autodiscovery_light(msg)
elif msg.topic == "zigbee2mqtt/bridge/devices":
for key in data:
if "friendly_name" in key and "ieee_address" in key:
if not should_include_device(key['friendly_name'], key['ieee_address']):
logging.info("MQTT: bypassing device " + key['friendly_name'])
continue

if "model_id" in key and (key["model_id"] in standardSensors or key["model_id"] in motionSensors): # Sensor is supported
if getObject(key["friendly_name"]) == False: ## Add the new sensor
logging.info("MQTT: Add new mqtt sensor " + key["friendly_name"])
Expand Down Expand Up @@ -433,6 +457,13 @@ def mqttServer():
bridgeConfig["config"]["mqtt"]["mqttTls"] = False
if 'mqttTlsInsecure' not in bridgeConfig["config"]["mqtt"]:
bridgeConfig["config"]["mqtt"]["mqttTlsInsecure"] = False
if 'explicitInclude' not in bridgeConfig["config"]["mqtt"]:
bridgeConfig["config"]["mqtt"]["explicitInclude"] = False
if 'includeGroups' not in bridgeConfig["config"]["mqtt"]:
bridgeConfig["config"]["mqtt"]["includeGroups"] = True
if 'includeList' not in bridgeConfig["config"]["mqtt"]:
bridgeConfig["config"]["mqtt"]["includeList"] = []

# TLS set?
if bridgeConfig["config"]["mqtt"]["mqttTls"]:
mqttTlsVersion = ssl.PROTOCOL_TLS
Expand Down