Esempio n. 1
0
 def test_verify_param_action(self, cmdopt_device: str,
                              cmdopt_baudrate: int, action: set):
     hp = HtHeatpump(device=cmdopt_device, baudrate=cmdopt_baudrate)
     val = hp.verify_param_action
     assert isinstance(val, set)
     hp.verify_param_action = action
     assert hp.verify_param_action == action
     hp.verify_param_action = val
Esempio n. 2
0
def create_app(
    device="/dev/ttyUSB0",
    baudrate=115200,
    user=None,
    bool_as_int=False,
    read_only=False,
    no_param_verification=False,
):
    # try to connect to the heat pump
    global ht_heatpump
    try:
        ht_heatpump = HtHeatpump(device, baudrate=baudrate)
        if no_param_verification:
            ht_heatpump.verify_param_action = VerifyAction.NONE()
        _LOGGER.info("open connection to heat pump (%s)", ht_heatpump)
        ht_heatpump.open_connection()
        ht_heatpump.login()
        _LOGGER.info("successfully connected to heat pump #%d",
                     ht_heatpump.get_serial_number())
        _LOGGER.info("software version = %s (%d)", *ht_heatpump.get_version())
    except Exception as ex:
        _LOGGER.error(ex)
        raise
    finally:
        ht_heatpump.logout()

    # create the Flask app
    app = Flask(__name__)
    app.config[
        "SWAGGER_UI_DOC_EXPANSION"] = settings.RESTX_SWAGGER_UI_DOC_EXPANSION
    app.config["RESTX_VALIDATE"] = settings.RESTX_VALIDATE
    app.config["RESTX_MASK_SWAGGER"] = settings.RESTX_MASK_SWAGGER
    app.config["ERROR_404_HELP"] = settings.RESTX_ERROR_404_HELP
    app.config["BUNDLE_ERRORS"] = settings.RESTX_BUNDLE_ERRORS
    if user:
        username, _, password = user.partition(":")
        app.config["BASIC_AUTH_USERNAME"] = username
        app.config["BASIC_AUTH_PASSWORD"] = password
        app.config["BASIC_AUTH_FORCE"] = True
        basic_auth = BasicAuth(app)  # noqa: F841
    _LOGGER.info("*** created Flask app %s with config %s", app, app.config)

    @app.before_first_request
    def before_first_request():
        # _LOGGER.debug("*** @app.before_first_request -- %s", __file__)
        pass

    settings.BOOL_AS_INT = bool_as_int
    settings.READ_ONLY = read_only

    from htrest.apiv1 import blueprint as apiv1

    app.register_blueprint(apiv1)
    # print(apiv1.url_prefix)
    print(app.url_map)

    return app