1 """Support for Zabbix sensors."""
3 from __future__
import annotations
5 from collections.abc
import Mapping
9 from pyzabbix
import ZabbixAPI
10 import voluptuous
as vol
13 PLATFORM_SCHEMA
as SENSOR_PLATFORM_SCHEMA,
22 from .const
import DOMAIN
24 _LOGGER = logging.getLogger(__name__)
26 _CONF_TRIGGERS =
"triggers"
27 _CONF_HOSTIDS =
"hostids"
28 _CONF_INDIVIDUAL =
"individual"
30 _ZABBIX_ID_LIST_SCHEMA = vol.Schema([int])
31 _ZABBIX_TRIGGER_SCHEMA = vol.Schema(
33 vol.Optional(_CONF_HOSTIDS, default=[]): _ZABBIX_ID_LIST_SCHEMA,
34 vol.Optional(_CONF_INDIVIDUAL, default=
False): cv.boolean,
35 vol.Optional(CONF_NAME): cv.string,
41 PLATFORM_SCHEMA = SENSOR_PLATFORM_SCHEMA.extend(
42 {vol.Required(_CONF_TRIGGERS): vol.Any(_ZABBIX_TRIGGER_SCHEMA,
None)}
49 add_entities: AddEntitiesCallback,
50 discovery_info: DiscoveryInfoType |
None =
None,
52 """Set up the Zabbix sensor platform."""
53 sensors: list[ZabbixTriggerCountSensor] = []
55 if not (zapi := hass.data[DOMAIN]):
56 _LOGGER.error(
"Zabbix integration hasn't been loaded? zapi is None")
59 _LOGGER.debug(
"Connected to Zabbix API Version %s", zapi.api_version())
62 if trigger_conf := config.get(_CONF_TRIGGERS):
63 hostids = trigger_conf.get(_CONF_HOSTIDS)
64 individual = trigger_conf.get(_CONF_INDIVIDUAL)
65 name = trigger_conf.get(CONF_NAME)
71 _LOGGER.error(
"If using 'individual', must specify hostids")
74 for hostid
in hostids:
75 _LOGGER.debug(
"Creating Zabbix Sensor: %s",
str(hostid))
79 _LOGGER.debug(
"Creating Zabbix Sensor")
83 _LOGGER.debug(
"Creating Zabbix Sensor group: %s",
str(hostids))
88 _LOGGER.debug(
"Creating Zabbix Sensor")
95 """Get the active trigger count for all Zabbix monitored hosts."""
97 def __init__(self, zapi: ZabbixAPI, name: str |
None =
"Zabbix") ->
None:
98 """Initialize Zabbix sensor."""
101 self.
_state_state: int |
None =
None
102 self._attributes: dict[str, Any] = {}
106 """Return the name of the sensor."""
107 return self.
_name_name
111 """Return the state of the sensor."""
116 """Return the units of measurement."""
120 return self.
_zapi_zapi.trigger.get(
121 output=
"extend", only_true=1, monitored=1, filter={
"value": 1}
125 """Update the sensor."""
126 _LOGGER.debug(
"Updating ZabbixTriggerCountSensor: %s",
str(self.
_name_name))
132 """Return the state attributes of the device."""
133 return self._attributes
137 """Get the active trigger count for a single Zabbix monitored host."""
140 self, zapi: ZabbixAPI, hostid: list[str], name: str |
None =
None
142 """Initialize Zabbix sensor."""
150 self._attributes[
"Host ID"] = self.
_hostid_hostid
153 return self.
_zapi_zapi.trigger.get(
163 """Get the active trigger count for specified Zabbix monitored hosts."""
166 self, zapi: ZabbixAPI, hostids: list[str], name: str |
None =
None
168 """Initialize Zabbix sensor."""
172 host_names = self.
_zapi_zapi.host.get(hostids=self.
_hostids_hostids, output=
"extend")
173 self.
_name_name_name =
" ".join(name[
"name"]
for name
in host_names)
174 self._attributes[
"Host IDs"] = self.
_hostids_hostids
177 return self.
_zapi_zapi.trigger.get(
def _call_zabbix_api(self)
None __init__(self, ZabbixAPI zapi, list[str] hostids, str|None name=None)
None __init__(self, ZabbixAPI zapi, list[str] hostid, str|None name=None)
def _call_zabbix_api(self)
StateType native_value(self)
str native_unit_of_measurement(self)
def _call_zabbix_api(self)
None __init__(self, ZabbixAPI zapi, str|None name="Zabbix")
Mapping[str, Any]|None extra_state_attributes(self)
def add_entities(account, async_add_entities, tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)