Skip to content
This repository has been archived by the owner on Oct 26, 2023. It is now read-only.

Commit

Permalink
Fixed Home Assistant autodiscovery messages with global prefix enabled (
Browse files Browse the repository at this point in the history
  • Loading branch information
merll authored and zewelor committed Aug 13, 2019
1 parent 7868cbd commit d1aebad
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 27 deletions.
4 changes: 3 additions & 1 deletion gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@

_LOGGER.info('Starting')

global_topic_prefix = settings['mqtt'].get('topic_prefix')

mqtt = MqttClient(settings['mqtt'])
manager = WorkersManager(settings['manager'])
manager.register_workers().start(mqtt)
manager.register_workers(global_topic_prefix).start(mqtt)

running = True

Expand Down
9 changes: 8 additions & 1 deletion mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ def publish(self, messages):
return

for m in messages:
topic = self._format_topic(m.topic)
if m.use_global_prefix:
topic = self._format_topic(m.topic)
else:
topic = m.topic
self.mqttc.publish(topic, m.payload, retain=m.retain)

@property
Expand Down Expand Up @@ -109,6 +112,8 @@ def _format_topic(self, topic):


class MqttMessage:
use_global_prefix = True

def __init__(self, topic=None, payload=None, retain=False):
self._topic = topic
self._payload = payload
Expand Down Expand Up @@ -149,6 +154,8 @@ class MqttConfigMessage(MqttMessage):
CLIMATE = 'climate'
BINARY_SENSOR = 'binary_sensor'

use_global_prefix = False

def __init__(self, component, name, payload=None, retain=False):
super().__init__("{}/{}/config".format(component, name), json.dumps(payload), retain)

Expand Down
11 changes: 9 additions & 2 deletions workers/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class BaseWorker:
def __init__(self, command_timeout, **args):
def __init__(self, command_timeout, global_topic_prefix, **kwargs):
self.command_timeout = command_timeout
for arg, value in args.items():
self.global_topic_prefix = global_topic_prefix
for arg, value in kwargs.items():
setattr(self, arg, value)
self._setup()

Expand All @@ -22,5 +23,11 @@ def format_discovery_name(self, *sensor_args):
def format_topic(self, *topic_args):
return '/'.join([self.topic_prefix, *topic_args])

def format_prefixed_topic(self, *topic_args):
topic = self.format_topic(*topic_args)
if self.global_topic_prefix:
return '{}/{}'.format(self.global_topic_prefix, topic)
return topic

def __repr__(self):
return self.__module__.split(".")[-1]
4 changes: 2 additions & 2 deletions workers/blescanmulti.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ class BlescanmultiWorker(BaseWorker):
scan_timeout = 10. # type: float
scan_passive = True # type: str or bool

def __init__(self, command_timeout, **kwargs):
super(BlescanmultiWorker, self).__init__(command_timeout, **kwargs)
def __init__(self, command_timeout, global_topic_prefix, **kwargs):
super(BlescanmultiWorker, self).__init__(command_timeout, global_topic_prefix, **kwargs)
self.scanner = Scanner().withDelegate(ScanDelegate())
self.last_status = [
BleDeviceStatus(self, mac, name) for name, mac in self.devices.items()
Expand Down
7 changes: 0 additions & 7 deletions workers/ibbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@


class IbbqWorker(BaseWorker):

def __init__(self, command_timeout, **args):
self.command_timeout = command_timeout
for arg, value in args.items():
setattr(self, arg, value)
self._setup()

def _setup(self):
_LOGGER.info("Adding %d %s devices", len(self.devices), repr(self))
for name, mac in self.devices.items():
Expand Down
2 changes: 1 addition & 1 deletion workers/miflora.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def config_device(self, name, mac):
for attr in monitoredAttrs:
payload = {
"unique_id": self.format_discovery_id(mac, name, attr),
"state_topic": self.format_topic(name, attr),
"state_topic": self.format_prefixed_topic(name, attr),
"name": self.format_discovery_name(name, attr),
"device": device
}
Expand Down
2 changes: 1 addition & 1 deletion workers/mithermometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def config_device(self, name, mac):
payload = {
"unique_id": self.format_discovery_id(mac, name, attr),
"name": self.format_discovery_name(name, attr),
"state_topic": self.format_topic(name, attr),
"state_topic": self.format_prefixed_topic(name, attr),
"device_class": attr,
"device": device
}
Expand Down
20 changes: 10 additions & 10 deletions workers/thermostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ def config_device(self, name, data):
payload = {"unique_id": self.format_discovery_id(mac, name, SENSOR_CLIMATE),
"name": self.format_discovery_name(name, SENSOR_CLIMATE),
"qos": 1,
"temperature_state_topic": self.format_topic(name, SENSOR_TARGET_TEMPERATURE),
"temperature_command_topic": self.format_topic(name, SENSOR_TARGET_TEMPERATURE, 'set'),
"mode_state_topic": self.format_topic(name, 'mode'),
"mode_command_topic": self.format_topic(name, 'mode', 'set'),
"away_mode_state_topic": self.format_topic(name, 'away'),
"away_mode_command_topic": self.format_topic(name, 'away', 'set'),
"temperature_state_topic": self.format_prefixed_topic(name, SENSOR_TARGET_TEMPERATURE),
"temperature_command_topic": self.format_prefixed_topic(name, SENSOR_TARGET_TEMPERATURE, 'set'),
"mode_state_topic": self.format_prefixed_topic(name, 'mode'),
"mode_command_topic": self.format_prefixed_topic(name, 'mode', 'set'),
"away_mode_state_topic": self.format_prefixed_topic(name, 'away'),
"away_mode_command_topic": self.format_prefixed_topic(name, 'away', 'set'),
"min_temp": 5.0,
"max_temp": 29.5,
"temp_step": 0.5,
Expand All @@ -121,7 +121,7 @@ def config_device(self, name, data):

payload = {"unique_id": self.format_discovery_id(mac, name, SENSOR_WINDOW),
"name": self.format_discovery_name(name, SENSOR_WINDOW),
"state_topic": self.format_topic(name, SENSOR_WINDOW),
"state_topic": self.format_prefixed_topic(name, SENSOR_WINDOW),
"device_class": 'window',
"payload_on": "True",
"payload_off": "False",
Expand All @@ -130,7 +130,7 @@ def config_device(self, name, data):

payload = {"unique_id": self.format_discovery_id(mac, name, SENSOR_BATTERY),
"name": self.format_discovery_name(name, SENSOR_BATTERY),
"state_topic": self.format_topic(name, SENSOR_BATTERY),
"state_topic": self.format_prefixed_topic(name, SENSOR_BATTERY),
"device_class": 'battery',
"payload_on": "True",
"payload_off": "False",
Expand All @@ -139,7 +139,7 @@ def config_device(self, name, data):

payload = {"unique_id": self.format_discovery_id(mac, name, SENSOR_LOCKED),
"name": self.format_discovery_name(name, SENSOR_LOCKED),
"state_topic": self.format_topic(name, SENSOR_LOCKED),
"state_topic": self.format_prefixed_topic(name, SENSOR_LOCKED),
"device_class": 'lock',
"payload_on": "False",
"payload_off": "True",
Expand All @@ -149,7 +149,7 @@ def config_device(self, name, data):

payload = {"unique_id": self.format_discovery_id(mac, name, SENSOR_VALVE),
"name": self.format_discovery_name(name, SENSOR_VALVE),
"state_topic": self.format_topic(name, SENSOR_VALVE),
"state_topic": self.format_prefixed_topic(name, SENSOR_VALVE),
"unit_of_measurement": "%",
"device": device}
ret.append(MqttConfigMessage(MqttConfigMessage.SENSOR, self.format_discovery_topic(mac, name, SENSOR_VALVE), payload=payload))
Expand Down
4 changes: 2 additions & 2 deletions workers_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def __init__(self, config):
self._config = config
self._command_timeout = config.get('command_timeout', 35)

def register_workers(self):
def register_workers(self, global_topic_prefix):
for (worker_name, worker_config) in self._config['workers'].items():
module_obj = importlib.import_module("workers.%s" % worker_name)
klass = getattr(module_obj, "%sWorker" % worker_name.title())
Expand All @@ -54,7 +54,7 @@ def register_workers(self):
self._pip_install_helper(module_obj.REQUIREMENTS)

command_timeout = worker_config.get('command_timeout', self._command_timeout)
worker_obj = klass(command_timeout, **worker_config['args'])
worker_obj = klass(command_timeout, global_topic_prefix, **worker_config['args'])

if 'sensor_config' in self._config and hasattr(worker_obj, 'config'):
_LOGGER.debug("Added %s config with a %d seconds timeout", repr(worker_obj), 2)
Expand Down

0 comments on commit d1aebad

Please sign in to comment.