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 token: str
device_class: str device_class: str
numeric: bool numeric: bool
area: str
label: str
filters: list[AttributeFilter] filters: list[AttributeFilter]
attribute: str | None attribute: str | None
friendly_name: bool friendly_name: bool
@ -152,6 +154,16 @@ class Entities(nagiosplugin.Resource):
template_filter += ( template_filter += (
f"|selectattr('attributes.{ f.attribute }', '{ op }', '{ f.value }')" 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: if self.exclude:
template_filter += ( template_filter += (
f"|rejectattr('entity_id', 'in', { json.dumps(self.exclude) })" f"|rejectattr('entity_id', 'in', { json.dumps(self.exclude) })"
@ -362,6 +374,8 @@ def main():
action="extend", 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", 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( common_args.add_argument(
"-e", "-e",
"--exclude", "--exclude",
@ -438,6 +452,8 @@ def main():
"device_class": args.device_class, "device_class": args.device_class,
"attribute": args.attribute, "attribute": args.attribute,
"filters": args.filter, "filters": args.filter,
"area": args.area,
"label": args.label,
"include": args.include, "include": args.include,
"exclude": args.exclude, "exclude": args.exclude,
"friendly_name": args.friendly, "friendly_name": args.friendly,