Skip to content

Commit c8a7a80

Browse files
committed
take 3 on protocol config: use enum in config schema to enforce allowed values
keep `MQTTv311` default in sensor class in case `protocol` is not specified in pack config
1 parent 25e16ff commit c8a7a80

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

Diff for: config.schema.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@
1818
protocol:
1919
description: "MQTT protocol version. Default MQTTv311"
2020
type: "string"
21+
enum:
22+
- MQTTv3
23+
- MQTTv311
24+
- MQTTv5
25+
default: MQTTv311
2126
required: false
2227
secret: false
2328
client_id:

Diff for: sensors/mqtt_sensor.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@ def __init__(self, sensor_service, config=None):
2222
self._client = None
2323
self._hostname = self._config.get('hostname', None)
2424
self._port = self._config.get('port', 1883)
25-
# NOTE: Need to translate MQTT* names into values expected by paho
26-
# Default to MQTTv311 if value is not valid since we can't
27-
# enforce "allowed" values from the pack config schema
28-
self._protocol = getattr(mqtt, self._config.get('protocol', ''), mqtt.MQTTv311)
25+
self._protocol = self._config.get('protocol', 'MQTTv311')
2926
self._client_id = self._config.get('client_id', None)
3027
self._userdata = self._config.get('userdata', None)
3128
self._username = self._config.get('username', None)
@@ -39,8 +36,11 @@ def __init__(self, sensor_service, config=None):
3936
def setup(self):
4037
self._logger.debug('[MQTTSensor]: setting up sensor...')
4138

39+
# NOTE: Need to ensure `protocol` MQTT* names are properly
40+
# handled as paho.mqtt constants and not bare strings
4241
self._client = mqtt.Client(self._client_id, clean_session=True,
43-
userdata=self._userdata, protocol=self._protocol)
42+
userdata=self._userdata,
43+
protocol=getattr(mqtt, self._protocol))
4444

4545
if self._username:
4646
self._client.username_pw_set(self._username, password=self._password)

0 commit comments

Comments
 (0)