#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.temperature import *  # pylint: disable=wildcard-import,unused-wildcard-import

climaveneta_sensors = {
    1: "Room",
    3: "Outlet Air 1",
    4: "Outlet Air 2",
    5: "Outlet Air 3",
    6: "Outlet Air 4",
    7: "Intlet Air 1",
    8: "Intlet Air 2",
    9: "Intlet Air 3",
    10: "Intlet Air 4",
    11: "Coil 1 Inlet Water",
    12: "Coil 2 Inlet Water",
    13: "Coil 1 Outlet Water",
    14: "Coil 2 Outlet Water",
    23: "Regulation Valve/Compressor",
    24: "Regulation Fan 1",
    25: "Regulation Fan 2",
    28: "Suction",
}

factory_settings["climaveneta_temp_default_levels"] = {"levels": (28, 30)}


def inventory_climaveneta_temp(info):
    for sensor_id, value in info:
        sensor_id = int(sensor_id.split(".")[0])
        if sensor_id in climaveneta_sensors and int(value) > 0:
            yield climaveneta_sensors[sensor_id], {}


def check_climaveneta_temp(item, params, info):
    for sensor_id, sensor_value in info:
        sensor_id = int(sensor_id.split(".")[0])
        if climaveneta_sensors.get(sensor_id) == item:
            sensor_value = int(sensor_value) / 10.0
            return check_temperature(sensor_value, params, "climaveneta_temp_%s" % item)


check_info["climaveneta_temp"] = {
    "check_function": check_climaveneta_temp,
    "inventory_function": inventory_climaveneta_temp,
    "service_description": "Temperature %s",
    "has_perfdata": True,
    "snmp_scan_function": lambda oid: oid(".1.3.6.1.2.1.1.1.0") == "pCO Gateway",
    "snmp_info": (".1.3.6.1.4.1.9839.2.1", [OID_END, 2]),
    "group": "temperature",
    "default_levels_variable": "climaveneta_temp_default_levels",
}
