Example #1
0
 def test_in_error(self, hthp: HtHeatpump):
     in_error = hthp.in_error
     assert isinstance(in_error, bool), "'in_error' must be of type bool"
     assert in_error == hthp.get_param("Stoerung")
Example #2
0
 def test_get_param(self, hthp: HtHeatpump, name: str, param: HtParam):
     value = hthp.get_param(name)
     assert value is not None, "'value' must not be None"
     assert param.in_limits(value)
Example #3
0
 def test_get_param_raises_KeyError(self, cmdopt_device: str,
                                    cmdopt_baudrate: int):
     hp = HtHeatpump(device=cmdopt_device, baudrate=cmdopt_baudrate)
     with pytest.raises(KeyError):
         hp.get_param("BlaBlaBla")
Example #4
0
def main():
    parser = argparse.ArgumentParser(
        description=textwrap.dedent('''\
            Command line tool to query for parameters of the Heliotherm heat pump.

            Example:

              $ python3 %(prog)s --device /dev/ttyUSB1 "Temp. Aussen" "Stoerung"
              Stoerung    : False
              Temp. Aussen: 5.0
            '''),
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog=textwrap.dedent('''\
            DISCLAIMER
            ----------

              Please note that any incorrect or careless usage of this program as well as
              errors in the implementation can damage your heat pump!

              Therefore, the author does not provide any guarantee or warranty concerning
              to correctness, functionality or performance and does not accept any liability
              for damage caused by this program or mentioned information.

              Thus, use it on your own risk!
            ''') + "\r\n")

    parser.add_argument(
        "-d",
        "--device",
        default="/dev/ttyUSB0",
        type=str,
        help=
        "the serial device on which the heat pump is connected, default: %(default)s"
    )

    parser.add_argument(
        "-b",
        "--baudrate",
        default=115200,
        type=int,
        # the supported baudrates of the Heliotherm heat pump (HP08S10W-WEB):
        choices=[9600, 19200, 38400, 57600, 115200],
        help=
        "baudrate of the serial connection (same as configured on the heat pump), default: %(default)s"
    )

    parser.add_argument("-j",
                        "--json",
                        action="store_true",
                        help="output will be in JSON format")

    parser.add_argument("--boolasint",
                        action="store_true",
                        help="boolean values will be stored as '0' and '1'")

    parser.add_argument("-t",
                        "--time",
                        action="store_true",
                        help="measure the execution time")

    parser.add_argument("-v",
                        "--verbose",
                        action="store_true",
                        help="increase output verbosity by activating logging")

    parser.add_argument(
        "name",
        type=str,
        nargs='*',
        help=
        "parameter name(s) to query for (as defined in htparams.csv) or omit to query for all known parameters"
    )

    args = parser.parse_args()

    # activate logging with level DEBUG in verbose mode
    if args.verbose:
        logging.basicConfig(level=logging.DEBUG)
    else:
        logging.basicConfig(level=logging.WARNING)
    # if not given, query for all "known" parameters
    params = args.name if args.name else HtParams.keys()

    hp = HtHeatpump(args.device, baudrate=args.baudrate)
    start = timer()
    try:
        hp.open_connection()
        hp.login()

        rid = hp.get_serial_number()
        if args.verbose:
            _logger.info(
                "connected successfully to heat pump with serial number {:d}".
                format(rid))
        ver = hp.get_version()
        if args.verbose:
            _logger.info("software version = {} ({:d})".format(ver[0], ver[1]))

        # query for the given parameter(s)
        values = {}
        for p in params:
            val = hp.get_param(p)
            if args.boolasint and HtParams[p].data_type == HtDataTypes.BOOL:
                val = 1 if val else 0
            values.update({p: val})

        # print the current value(s) of the retrieved parameter(s)
        if args.json:
            print(json.dumps(values, indent=4, sort_keys=True))
        else:
            if len(params) > 1:
                for p in sorted(params):
                    print("{:{width}}: {}".format(p,
                                                  values[p],
                                                  width=len(
                                                      max(params, key=len))))
            elif len(params) == 1:
                print(values[params[0]])

    except Exception as ex:
        _logger.error(ex)
        sys.exit(1)
    finally:
        hp.logout()  # try to logout for an ordinary cancellation (if possible)
        hp.close_connection()
    end = timer()

    # print execution time only if desired
    if args.time:
        print("execution time: {:.2f} sec".format(end - start))

    sys.exit(0)