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$"
|
value = "$home_assistant_state_filter$"
|
||||||
description = "Filter by 'attribute=value' (can be an array)"
|
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
|
max: float
|
||||||
filters: list[str]
|
filters: list[str]
|
||||||
attribute: Optional[str]
|
attribute: Optional[str]
|
||||||
|
friendly_name: bool
|
||||||
|
|
||||||
def hass_get(self, endpoint: str) -> requests.Response:
|
def hass_get(self, endpoint: str) -> requests.Response:
|
||||||
headers = {
|
headers = {
|
||||||
@ -56,10 +57,19 @@ class Entities(nagiosplugin.Resource):
|
|||||||
value = float(state["state"]) if state["state"].isnumeric() else -1
|
value = float(state["state"]) if state["state"].isnumeric() else -1
|
||||||
uom = state["attributes"].get("unit_of_measurement")
|
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":
|
if state["state"] == "unavailable":
|
||||||
_log.info(f"{state['entity_id']} unavailable")
|
_log.info(f"{state['entity_id']} unavailable")
|
||||||
yield nagiosplugin.Metric(
|
yield nagiosplugin.Metric(
|
||||||
state["entity_id"],
|
name,
|
||||||
value,
|
value,
|
||||||
uom,
|
uom,
|
||||||
context=self.device_class,
|
context=self.device_class,
|
||||||
@ -126,6 +136,11 @@ def main():
|
|||||||
nargs="*",
|
nargs="*",
|
||||||
help="Filter by 'attribute=value'",
|
help="Filter by 'attribute=value'",
|
||||||
)
|
)
|
||||||
|
argp.add_argument(
|
||||||
|
"--friendly",
|
||||||
|
action="store_true",
|
||||||
|
help="Use friendly name, when available",
|
||||||
|
)
|
||||||
|
|
||||||
args = argp.parse_args()
|
args = argp.parse_args()
|
||||||
|
|
||||||
@ -138,6 +153,7 @@ def main():
|
|||||||
args.max,
|
args.max,
|
||||||
args.filter,
|
args.filter,
|
||||||
args.attribute,
|
args.attribute,
|
||||||
|
args.friendly,
|
||||||
),
|
),
|
||||||
nagiosplugin.ScalarContext(args.device_class, args.warning, args.critical),
|
nagiosplugin.ScalarContext(args.device_class, args.warning, args.critical),
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user