From 9daa208f79c86d1fdb65bf4a7c1a3c7b49216f52 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.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) 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), )