Add cli arg to use friendly name, when available
This commit is contained in:
parent
b3fd15789a
commit
78669f8ff6
@ -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_friendly"
|
||||
description = "Use friendly name, when available"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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),
|
||||
)
|
||||
|
Loading…
Reference in New Issue
Block a user