Add explicit include/exclude by entity id

This commit is contained in:
Adam Goldsmith 2023-03-11 00:41:22 -05:00
parent 8ce50398e9
commit c7f1b9c7c9

View File

@ -56,6 +56,8 @@ class Entities(nagiosplugin.Resource):
attribute: Optional[str] attribute: Optional[str]
friendly_name: bool friendly_name: bool
ignore_missing: bool ignore_missing: bool
include: list[str]
exclude: list[str]
min: Optional[float] = None min: Optional[float] = None
max: Optional[float] = None max: Optional[float] = None
@ -118,8 +120,11 @@ class Entities(nagiosplugin.Resource):
def probe(self): def probe(self):
response = self.hass_get("/api/states") response = self.hass_get("/api/states")
for state in response: for state in response:
if state["attributes"].get("device_class") == self.device_class and all( if (
state["attributes"].get(k) == v for k, v in self.filters state["attributes"].get("device_class") == self.device_class
and all(state["attributes"].get(k) == v for k, v in self.filters)
and (len(self.include) == 0 or state["entity_id"] in self.include)
and state["entity_id"] not in self.exclude
): ):
yield from self._state_to_metric(state) yield from self._state_to_metric(state)
@ -302,6 +307,20 @@ def main():
nargs="*", nargs="*",
help="filter by 'attribute=value' (may be specified multiple times)", help="filter by 'attribute=value' (may be specified multiple times)",
) )
common_args.add_argument(
"-i",
"--include",
default=[],
nargs="*",
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(
"-e",
"--exclude",
default=[],
nargs="*",
help="exclude entities by id",
)
common_args.add_argument( common_args.add_argument(
"--friendly", "--friendly",
action="store_true", action="store_true",
@ -366,19 +385,24 @@ def main():
) )
args = argp.parse_args() args = argp.parse_args()
parsed_common_args = {
"device_class": args.device_class,
"attribute": args.attribute,
"filters": args.filter,
"include": args.include,
"exclude": args.exclude,
"friendly_name": args.friendly,
"ignore_missing": args.ignore_missing,
}
if args.subparser_name == "scalar": if args.subparser_name == "scalar":
check = nagiosplugin.Check( check = nagiosplugin.Check(
Entities( Entities(
url=args.url, url=args.url,
token=args.token, token=args.token,
device_class=args.device_class,
numeric=True, numeric=True,
min=args.min, min=args.min,
max=args.max, max=args.max,
filters=args.filter, **parsed_common_args,
attribute=args.attribute,
friendly_name=args.friendly,
ignore_missing=args.ignore_missing,
), ),
ScalarOrUnknownContext(args.device_class, args.warning, args.critical), ScalarOrUnknownContext(args.device_class, args.warning, args.critical),
) )
@ -387,12 +411,8 @@ def main():
Entities( Entities(
url=args.url, url=args.url,
token=args.token, token=args.token,
device_class=args.device_class,
numeric=False, numeric=False,
filters=args.filter, **parsed_common_args,
attribute=args.attribute,
friendly_name=args.friendly,
ignore_missing=args.ignore_missing,
), ),
RegexContext(args.device_class, args.ok, args.warning, args.critical), RegexContext(args.device_class, args.ok, args.warning, args.critical),
) )