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

Read rtl_433_mqtt_hass.py configuration from rtl_433.conf #2841

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
60 changes: 57 additions & 3 deletions examples/rtl_433_mqtt_hass.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,13 +1053,67 @@ def run():
if not args.password and 'MQTT_PASSWORD' in os.environ:
args.password = os.environ['MQTT_PASSWORD']

if not args.user or not args.password:
logging.warning("User or password is not set. Check credentials if subscriptions do not return messages.")

if args.ids:
ids = ', '.join(str(id) for id in args.ids)
logging.info("Only discovering devices with ids: [%s]" % ids)
else:
logging.info("Discovering all devices")

found_config = False
try:
with open(find_file_in_path(CONFIG_FILENAME, CONFIG_PATHS)) as f:
found = re.findall('^output (.*)$', f.read(), re.MULTILINE)
devices = None
events = None
retain = None
user = None
password = None
host = None
port = None
for find in found:
if "mqtt" not in find: continue
host = re.findall(r'mqtt://(.*?),', find)[0]
devices = re.findall(r'devices=(.*?)(?:,+|$)', find)
if len(devices) > 0: devices = devices[0]
events = re.findall(r'events=(.*?)(?:,+|$)', find)
if len(events) > 0: events = events[0]
retain = re.findall(r'retain=(.*?)(?:,+|$)', find)
if len(retain) > 0: retain = retain[0]

if '@' in host:
user, password = host.split('@')[0].split(':')
host = host.split('@')[1]
if ':' in host:
host, port = host.split(':')
found_config = True
break
except FileNotFoundError:
logging.warning("Config file not found")
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be helpful to be less ambiguous about what config file you are talking about in this and the other log messages.

I would explicitly mention that it is rtl_433's config file and/or the path.

Some of the Home Assistant add-ons have their own config files for this script.

except Exception as e:
logging.exception("Error reading config file")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above, be explicit about the file.


if found_config:
if host:
logging.info(f"Using host {host} from config")
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as about, be more explicit about the source, "from config" is a bit ambiguous.

args.host = host
if port:
logging.info(f"Using port {port} from config")
args.port = int(port)
if user:
logging.info(f"Using user {user} from config")
args.user = user
if password:
args.password = password
if retain:
logging.info(f"Using retain {retain} from config")
args.retain = retain
if devices:
logging.info(f"Using device_topic_suffix {devices} from config")
args.device_topic_suffix = devices
if events:
logging.info(f"Using rtl_topic {events} from config")
args.rtl_topic = events
if not args.user or not args.password:
logging.warning("User or password is not set. Check credentials if subscriptions do not return messages.")

run()
Loading