#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# ails.  You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.

#   .--output--------------------------------------------------------------.
#   |                               _               _                      |
#   |                    ___  _   _| |_ _ __  _   _| |_                    |
#   |                   / _ \| | | | __| '_ \| | | | __|                   |
#   |                  | (_) | |_| | |_| |_) | |_| | |_                    |
#   |                   \___/ \__,_|\__| .__/ \__,_|\__|                   |
#   |                                  |_|                                 |
#   '----------------------------------------------------------------------'

# .1.3.6.1.4.1.31770.2.2.6.2.1.4.1.1 Inlet 1

# .1.3.6.1.4.1.31770.2.2.6.3.1.4.1.1.1 "00 00 00 00 FF FF 00 00 "
# .1.3.6.1.4.1.31770.2.2.6.3.1.4.1.1.2 "00 00 00 01 FF FF 00 00 "
# .1.3.6.1.4.1.31770.2.2.6.3.1.4.1.1.3 "00 00 00 02 FF FF 00 00 "

# .1.3.6.1.4.1.31770.2.2.6.6.1.7.1.1.1.0.0.1 "00 04 00 00 FF FF 00 00 "
# .1.3.6.1.4.1.31770.2.2.6.6.1.7.1.1.2.0.0.2 "00 04 00 01 FF FF 00 00 "
# .1.3.6.1.4.1.31770.2.2.6.6.1.7.1.1.3.0.0.3 "00 04 00 02 FF FF 00 00 "

# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.1 1 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.4 4 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.5 5 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.17 17 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.18 18 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.19 19 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.20 20 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.22 22 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.23 23 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.32 32 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'....... '
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.34 34 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'......."'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.36 36 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'.......$'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.0.0.0.255.255.0.38 38 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'.......&'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.1.64.4.255.2.1.0 256 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'..@.....'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.1.64.4.255.2.1.1 257 --> BACHMANN-bluenet2-MIB::bluenet2VariableType.'..@.....'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.4.0.0.255.255.0.7 7 --> BACHMANN-BLUENET2-MIB::blueNet2VariableType.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.6.0.4.0.0.255.255.0.8 8 --> BACHMANN-BLUENET2-MIB::blueNet2VariableType.'........'

# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.1 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.4 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.5 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.17 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.18 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.19 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.20 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.22 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.23 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.32 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'....... '
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.34 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'......."'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.36 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'.......$'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.0.0.0.255.255.0.38 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'.......&'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.1.64.4.255.2.1.0 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'..@.....'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.1.64.4.255.2.1.1 2 --> BACHMANN-bluenet2-MIB::bluenet2VariableStatus.'..@.....'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.4.0.0.255.255.0.7 2 --> BACHMANN-BLUENET2-MIB::blueNet2VariableStatus.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.7.0.4.0.0.255.255.0.8 2 --> BACHMANN-BLUENET2-MIB::blueNet2VariableStatus.'........'

# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.1 -2 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.4 -2 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.5 -2 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.17 -3 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.18 -1 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.19 -1 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.20 -1 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.22 -1 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.23 -2 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.32 -4 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'....... '
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.34 -4 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'......."'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.36 -4 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'.......$'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.0.0.0.255.255.0.38 -4 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'.......&'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.1.64.4.255.2.1.0 -1 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'..@.....'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.1.64.4.255.2.1.1 -1 --> BACHMANN-bluenet2-MIB::bluenet2VariableScaling.'..@.....'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.4.0.0.255.255.0.7 -1 --> BACHMANN-BLUENET2-MIB::blueNet2VariableScaling.'........'
# .1.3.6.1.4.1.31770.2.2.8.2.1.9.0.4.0.0.255.255.0.8 -1 --> BACHMANN-BLUENET2-MIB::blueNet2VariableScaling.'........'

# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.1 23410 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'........'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.4 95 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'........'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.5 351 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'........'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.17 -717 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'........'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.18 2234 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'........'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.19 1602 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'........'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.20 5180 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'........'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.22 -1571 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'........'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.23 4997 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'........'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.32 2407491 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'....... '
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.34 1302485 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'......."'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.36 1842643 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'.......$'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.0.0.1.255.255.0.38 1842643 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'.......&'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.1.64.4.255.2.1.0 260 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'..@.....'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.1.64.4.255.2.1.1 369 --> BACHMANN-bluenet2-MIB::bluenet2VariableDataValue.'..@.....'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.4.0.0.255.255.0.7 16 --> BACHMANN-BLUENET2-MIB::blueNet2VariableDataValue.'........'
# .1.3.6.1.4.1.31770.2.2.8.4.1.5.0.4.0.0.255.255.0.8 0 --> BACHMANN-BLUENET2-MIB::blueNet2VariableDataValue.'........'

#.
#   .--phases--------------------------------------------------------------.
#   |                         _                                            |
#   |                   _ __ | |__   __ _ ___  ___  ___                    |
#   |                  | '_ \| '_ \ / _` / __|/ _ \/ __|                   |
#   |                  | |_) | | | | (_| \__ \  __/\__ \                   |
#   |                  | .__/|_| |_|\__,_|___/\___||___/                   |
#   |                  |_|                                                 |
#   +----------------------------------------------------------------------+
#   |                            main check                                |
#   '----------------------------------------------------------------------'


def parse_bluenet2_powerrail(info):
    map_status = {
        "0": (0, "expected"),
        "1": (3, "undefined"),
        "2": (0, "OK"),
        "3": (2, "error high"),
        "4": (2, "error low"),
        "5": (1, "warning high"),
        "6": (1, "warning low"),
        "7": (2, "lost"),
        "8": (1, "deactivate"),
        "9": (2, "on alarm identidy"),
        "10": (2, "off alarm identify"),
        "11": (2, "on alarm"),
        "12": (2, "off alarm"),
        "13": (1, "on warning identify"),
        "14": (1, "off warning identify"),
        "15": (1, "on warning"),
        "16": (1, "off warning"),
        "17": (0, "on identify"),
        "18": (0, "off identify"),
        "19": (0, "on"),
        "20": (1, "off"),
        "21": (2, "on child alarm"),
        "22": (2, "off child alarm"),
        "23": (1, "on child warning"),
        "24": (1, "off child warning"),
        "25": (2, "child alarm"),
        "26": (1, "child warning"),
        "27": (2, "lost child"),
        "36": (1, "update in progress"),
        "37": (2, "update error"),
        "38": (1, "ongoing switch"),
        "39": (2, "high"),
        "40": (1, "low"),
        "41": (2, "alarm"),
        "42": (1, "warning"),
        "43": (0, "ok"),
        "44": (1, "disabled"),
        "45": (1, "fw version too new"),
    }

    map_phase_types = {
        "1": ("phases", "Phase", "voltage"),
        "4": ("phases", "Phase", "current"),
        "18": ("phases", "Phase", "appower"),
        "19": ("phases", "Phase", "power"),
        "23": ("phases", "Phase", "frequency"),
        "7": ("rcm_phases", "RCM Phase", "differential_current_ac"),
        "8": ("rcm_phases", "RCM Phase", "differential_current_dc"),
    }

    map_sensor_types = {
        "256": "temp",
        "257": "humidity",
    }

    def get_item_name(descr, index_str):
        return "%s %d" % (descr, int(index_str) + 1)

    def get_pdu_name(pdu_info):
        if pdu_info == "0":
            return "Master"
        return "PDU %s" % pdu_info

    pre_parsed = {}
    for oidend, _name in info[0]:
        pre_parsed.setdefault(oidend, {"phases": [], "rcm_phases": []})

    for index, what in [(1, "phases"), (2, "rcm_phases")]:
        for oidend, identifier in info[index]:
            inlet_id = ".".join(oidend.split(".")[:2])
            if inlet_id in pre_parsed:
                pre_parsed[inlet_id][what].append([str(ord(x)) for x in identifier][:-1])

    parsed = {"phases": {}, "rcm_phases": {}, "sensors": {}}
    for oidend, ty, status, exponent_str, reading_str in info[3]:
        status_info = map_status[status]
        reading = float(reading_str) * 10**int(exponent_str)
        oid_info = oidend.split(".")
        identifier = oid_info[:-1]

        if ty in map_phase_types:
            phase_ty, phase_txt, what = map_phase_types[ty]
            for inlet_id, inlet_info in pre_parsed.items():
                if identifier in inlet_info[phase_ty]:
                    phase_name = get_item_name("%s %s" % (inlet_id, phase_txt), oid_info[3])
                    parsed[phase_ty].setdefault(phase_name, {})
                    parsed[phase_ty][phase_name].setdefault(what, (reading, status_info))

        elif ty in map_sensor_types:
            # https://www.bachmann.com/fileadmin/05a_Downloads/BlueNet/BlueNet_BN3000_-_BN7500_V2.02.XX_Bedienungsanleitung_DE_Rev_01.pdf
            # Example of OID_END: ...0.1.64.4.255.2.1.0
            # 0   * number of pdu (pdu 0 -> Master PDU, 1, 2, 3,... -> Slave PDU)
            # 1     sensor type (0: electrical, 1: external sensor, 4: rcm)
            # 64    sensor hardware address (64: combination sensor, 72: temperature sensor, 56: GPIO module)
            # 4   * channel number of internal multiplexer (channel 4, 5)
            # 255 * channel number of external multiplexer (channel 255, 1, 2, 4, 8)
            # 2     external sensor type (2: combination sensor, 1: temperature sensor, 3: GPIO module)
            # 1.0   two byte key definition:
            #           1.0: temperature, 1.1: humidity 1.10:dewpoint
            #           1.2-1.5 GPIO in 1-4
            #           1.6-1.9 GPIO out 1-4
            # * becomes part of the item in order to make it unique
            sensor_name = "Sensor %s %s/%s" % (get_pdu_name(oid_info[0]), oid_info[3], oid_info[4])
            inst = parsed["sensors"].setdefault(map_sensor_types[ty], {})
            inst.setdefault(sensor_name, (reading, status_info))

    return parsed


check_info["bluenet2_powerrail"] = {
    'parse_function': parse_bluenet2_powerrail,
    'inventory_function': lambda parsed: inventory_elphase(parsed["phases"]),
    'check_function': lambda item, params, parsed: check_elphase(item, params, parsed["phases"]),
    'service_description': 'Inlet %s',
    "has_perfdata": True,
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.2.0").startswith(".1.3.6.1.4.1.31770.2.1"
                                                                          ),
    "snmp_info": [
        (
            ".1.3.6.1.4.1.31770.2.2.6.2.1",
            [
                OID_END,
                "4",  # blueNet2CircuitName
            ]),
        (
            ".1.3.6.1.4.1.31770.2.2.6.3.1",
            [
                OID_END,
                "4",  # blueNet2PhaseGuid
            ]),
        (
            ".1.3.6.1.4.1.31770.2.2.6.6.1",
            [
                OID_END,
                "7",  # blueNet2RcmGuid
            ]),
        (
            ".1.3.6.1.4.1.31770.2.2.8",
            [
                OID_END,
                "2.1.6",  # bluenet2VariableType
                "2.1.7",  # bluenet2VariableStatus
                "2.1.9",  # bluenet2VariableScaling
                "4.1.5",  # bluenet2VariableDataValue
            ])
    ],
    'includes': ["elphase.include"],
    'group': 'el_inphase',
}

#.
#   .--RCM phases----------------------------------------------------------.
#   |        ____   ____ __  __         _                                  |
#   |       |  _ \ / ___|  \/  |  _ __ | |__   __ _ ___  ___  ___          |
#   |       | |_) | |   | |\/| | | '_ \| '_ \ / _` / __|/ _ \/ __|         |
#   |       |  _ <| |___| |  | | | |_) | | | | (_| \__ \  __/\__ \         |
#   |       |_| \_\\____|_|  |_| | .__/|_| |_|\__,_|___/\___||___/         |
#   |                            |_|                                       |
#   '----------------------------------------------------------------------'

# Suggested by customer, in mA
factory_settings["bluenet2_powerrail_rcm_default_levels"] = {
    "differential_current_ac": (3.5, 30),
    "differential_current_dc": (70, 100),
}

check_info["bluenet2_powerrail.rcm"] = {
    'inventory_function': lambda parsed: inventory_elphase(parsed["rcm_phases"]),
    'check_function': lambda item, params, parsed: check_elphase(item, params, parsed["rcm_phases"]
                                                                ),
    "service_description": "Inlet %s",
    "has_perfdata": True,
    "default_levels_variable": "bluenet2_powerrail_rcm_default_levels",
    'includes': ["elphase.include"],
    'group': 'el_inphase',
}

#.
#   .--temperature---------------------------------------------------------.
#   |      _                                      _                        |
#   |     | |_ ___ _ __ ___  _ __   ___ _ __ __ _| |_ _   _ _ __ ___       |
#   |     | __/ _ \ '_ ` _ \| '_ \ / _ \ '__/ _` | __| | | | '__/ _ \      |
#   |     | ||  __/ | | | | | |_) |  __/ | | (_| | |_| |_| | | |  __/      |
#   |      \__\___|_| |_| |_| .__/ \___|_|  \__,_|\__|\__,_|_|  \___|      |
#   |                       |_|                                            |
#   '----------------------------------------------------------------------'

# Suggested by customer
factory_settings["bluenet2_powerrail_temp_default_levels"] = {
    "levels": (30, 35),
}


def inventory_bluenet2_powerrail_temp(parsed):
    for item in parsed["sensors"].get("temp", {}):
        yield item, {}


def check_bluenet2_powerrail_temp(item, params, parsed):
    if item in parsed["sensors"].get("temp", {}):
        reading, (state, state_readable) = parsed["sensors"]["temp"][item]
        return check_temperature(reading,
                                 params,
                                 "bluenet2_powerrail_temp_%s" % item,
                                 dev_status=state,
                                 dev_status_name=state_readable)


check_info["bluenet2_powerrail.temp"] = {
    "inventory_function": inventory_bluenet2_powerrail_temp,
    "check_function": check_bluenet2_powerrail_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "default_levels_variable": "bluenet2_powerrail_temp_default_levels",
    "includes": ["temperature.include"],
    "group": "temperature",
}

#.
#   .--humidity------------------------------------------------------------.
#   |              _                     _     _ _ _                       |
#   |             | |__  _   _ _ __ ___ (_) __| (_) |_ _   _               |
#   |             | '_ \| | | | '_ ` _ \| |/ _` | | __| | | |              |
#   |             | | | | |_| | | | | | | | (_| | | |_| |_| |              |
#   |             |_| |_|\__,_|_| |_| |_|_|\__,_|_|\__|\__, |              |
#   |                                                  |___/               |
#   '----------------------------------------------------------------------'

# Suggested by customer
bluenet2_powerrail_humidity_default_levels = (5, 8, 75, 80)


def inventory_bluenet2_powerrail_humidity(parsed):
    for item in parsed["sensors"].get("humidity", {}):
        yield item, "bluenet2_powerrail_humidity_default_levels"


def check_bluenet2_powerrail_humidity(item, params, parsed):
    if item in parsed["sensors"].get("humidity", {}):
        reading, (state, state_readable) = parsed["sensors"]["humidity"][item]
        yield check_humidity(reading, params)
        yield state, state_readable


check_info["bluenet2_powerrail.humidity"] = {
    "inventory_function": inventory_bluenet2_powerrail_humidity,
    "check_function": check_bluenet2_powerrail_humidity,
    'service_description': 'Humidity %s',
    "has_perfdata": True,
    "includes": ["humidity.include"],
    "group": "humidity",
}
