Move state to metric logic into a function

This commit is contained in:
Adam Goldsmith 2023-03-11 00:21:23 -05:00
parent 5d4f9fb98b
commit 8ce50398e9

View File

@ -79,22 +79,13 @@ class Entities(nagiosplugin.Resource):
else:
print("OK: " + message)
def probe(self):
response = self.hass_get("/api/states")
for state in response:
if state["attributes"].get("device_class") == self.device_class and all(
state["attributes"].get(k) == v for k, v in self.filters
):
def _state_to_metric(self, state):
"""Convert a state into a metric"""
if self.attribute is not None:
if (
self.ignore_missing
and self.attribute not in state["attributes"]
):
continue
if self.ignore_missing and self.attribute not in state["attributes"]:
return
else:
value = state["attributes"].get(
self.attribute, "missing attribute"
)
value = state["attributes"].get(self.attribute, "missing attribute")
uom = None
else:
if self.numeric:
@ -124,6 +115,14 @@ class Entities(nagiosplugin.Resource):
max=self.max,
)
def probe(self):
response = self.hass_get("/api/states")
for state in response:
if state["attributes"].get("device_class") == self.device_class and all(
state["attributes"].get(k) == v for k, v in self.filters
):
yield from self._state_to_metric(state)
class RegexContext(nagiosplugin.Context):
def __init__(