Add filtering by area and label

This commit is contained in:
Adam Goldsmith 2024-06-20 01:08:37 -04:00
parent 5a4b04ffac
commit fa21f7e879

View File

@ -68,6 +68,8 @@ class Entities(nagiosplugin.Resource):
token: str
device_class: str
numeric: bool
area: str
label: str
filters: list[AttributeFilter]
attribute: str | None
friendly_name: bool
@ -152,6 +154,16 @@ class Entities(nagiosplugin.Resource):
template_filter += (
f"|selectattr('attributes.{ f.attribute }', '{ op }', '{ f.value }')"
)
if self.area:
template_filter += (
f"|selectattr('entity_id', 'in', area_entities('{ self.area }'))"
)
if self.label:
template_filter += (
f"|selectattr('entity_id', 'in', label_entities('{ self.label }'))"
)
if self.exclude:
template_filter += (
f"|rejectattr('entity_id', 'in', { json.dumps(self.exclude) })"
@ -362,6 +374,8 @@ def main():
action="extend",
help="explicitly include entities by id. Other entities will not be considered if this is specified. Listed entities must also match the specified device class",
)
common_args.add_argument("--area", type=str, help="area to filter by")
common_args.add_argument("--label", type=str, help="label to filter by")
common_args.add_argument(
"-e",
"--exclude",
@ -438,6 +452,8 @@ def main():
"device_class": args.device_class,
"attribute": args.attribute,
"filters": args.filter,
"area": args.area,
"label": args.label,
"include": args.include,
"exclude": args.exclude,
"friendly_name": args.friendly,