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: else:
print("OK: " + message) print("OK: " + message)
def probe(self): def _state_to_metric(self, state):
response = self.hass_get("/api/states") """Convert a state into a metric"""
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
):
if self.attribute is not None: if self.attribute is not None:
if ( if self.ignore_missing and self.attribute not in state["attributes"]:
self.ignore_missing return
and self.attribute not in state["attributes"]
):
continue
else: else:
value = state["attributes"].get( value = state["attributes"].get(self.attribute, "missing attribute")
self.attribute, "missing attribute"
)
uom = None uom = None
else: else:
if self.numeric: if self.numeric:
@ -124,6 +115,14 @@ class Entities(nagiosplugin.Resource):
max=self.max, 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): class RegexContext(nagiosplugin.Context):
def __init__( def __init__(