import config
import devdi
import home
import logging
import mqtt
import os
import report
import user_interface

# TODO: Add some more ui commands for devices
# TODO: Add some test for smart_brain
# TODO: Implementation of missing devices in devices/__init__.py

logger = logging.getLogger(config.APP_NAME)


if __name__ == "__main__":
    #
    # Logging
    #
    if config.DEBUG:
        report.appLoggingConfigure(None, 'stdout', ((config.APP_NAME, logging.DEBUG), ),
                                   target_level=logging.WARNING, fmt=report.SHORT_FMT, host='localhost', port=19996)
    else:
        report.stdoutLoggingConfigure(((config.APP_NAME, logging.WARNING), ), report.SHORT_FMT)

    #
    # MQTT Client
    #
    mc = mqtt.mqtt_client(host=config.MQTT_SERVER, port=config.MQTT_PORT, username=config.MQTT_USER,
                          password=config.MQTT_PASSWORD, name=config.APP_NAME)

    #
    # Smarthome physical Devices
    #
    pd = devdi.physical_devices(mc)

    #
    # Smart Home Functionality
    #
    home.functions(pd)

    #
    # User Interface
    #
    ui = user_interface.user_interface()
    # Add the device commands
    for d in pd.values():
        try:
            for cmd in d.user_cmds:
                ui.add_command(cmd, d.user_cmds[cmd])
        except AttributeError:
            pass    # device seems to have no user commands
    ui.run()
