From d05f057554f129268a27662793a99b9e92dd72cb Mon Sep 17 00:00:00 2001 From: Adam Goldsmith Date: Tue, 29 Nov 2022 15:42:08 -0500 Subject: [PATCH] Add cli arg to use friendly name, when available --- check_home_assistant_state.conf | 4 ++++ check_home_assistant_state.py | 18 +++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/check_home_assistant_state.conf b/check_home_assistant_state.conf index 087b254..82f549e 100644 --- a/check_home_assistant_state.conf +++ b/check_home_assistant_state.conf @@ -37,5 +37,9 @@ object CheckCommand "home_assistant_state" { value = "$home_assistant_state_filter$" description = "Filter by 'attribute=value' (can be an array)" } + "--friendly" = { + set_if = "$home_assistant_state_friendly" + description = "Use friendly name, when available" + } } } diff --git a/check_home_assistant_state.py b/check_home_assistant_state.py index a14cb94..51aa492 100755 --- a/check_home_assistant_state.py +++ b/check_home_assistant_state.py @@ -22,6 +22,7 @@ class Entities(nagiosplugin.Resource): max: float filters: list[str] attribute: Optional[str] + friendly_name: bool def hass_get(self, endpoint: str) -> requests.Response: headers = { @@ -56,10 +57,19 @@ class Entities(nagiosplugin.Resource): value = float(state["state"]) if state["state"].isnumeric() else -1 uom = state["attributes"].get("unit_of_measurement") + if self.friendly_name: + name = ( + state["attributes"] + .get("friendly_name", state["entity_id"]) + .translate({ord(c): "_" for c in "'-"}) + ) + else: + name = state["entity_id"] + if state["state"] == "unavailable": _log.info(f"{state['entity_id']} unavailable") yield nagiosplugin.Metric( - state["entity_id"], + name, value, uom, context=self.device_class, @@ -126,6 +136,11 @@ def main(): nargs="*", help="Filter by 'attribute=value'", ) + argp.add_argument( + "--friendly", + action="store_true", + help="Use friendly name, when available", + ) args = argp.parse_args() @@ -138,6 +153,7 @@ def main(): args.max, args.filter, args.attribute, + args.friendly, ), nagiosplugin.ScalarContext(args.device_class, args.warning, args.critical), )