Skip to content

Commit

Permalink
✨ Add state_type:option tag when relevant
Browse files Browse the repository at this point in the history
This will help to classify all events whose state is part of a finite
option list
  • Loading branch information
kamaradclimber committed Jan 18, 2025
1 parent 0a225a5 commit f9ff46d
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions custom_components/datadog_agentless/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -404,9 +404,11 @@ def enrich(data_key):
tags.append(f"entity_id:{event.data["old_state"].entity_id}")
if "new_state" in event.data and event.data["new_state"] is not None:
tags.append(f"entity_id:{event.data["new_state"].entity_id}")
state_tag = build_state_tag(event)
state_tag, extra_tags = build_state_tag(event)
if state_tag is not None:
tags.append(f"new_state:{state_tag}")
for t in extra_tags:
tags.append(t)
return ("State changed", list(set(tags)))
elif event.event_type == EVENT_DEVICE_REGISTRY_UPDATED:
enrich("device_id")
Expand All @@ -420,20 +422,20 @@ def enrich(data_key):
else:
return (str(event.event_type), tags)

def build_state_tag(event) -> Optional[str]:
def build_state_tag(event) -> Tuple[Optional[str], list[str]]:
if "new_state" not in event.data or event.data["new_state"] is None:
return None
return (None, [])
new_state = event.data["new_state"]
if "device_class" in new_state.attributes and new_state.attributes["device_class"] == SensorDeviceClass.TIMESTAMP:
return None
return (None, [])
for key in ["operation_list", "options"]:
if key in new_state.attributes and new_state.state in new_state.attributes[key]:
return new_state.state
return (new_state.state, [f"state_type:{key}"])
if "icon" in new_state.attributes and re.match(".*clock.*", new_state.attributes["icon"]):
return None
return (None, [])
if new_state.state == "":
return None
return new_state.state
return (None, [])
return (new_state.state, [])

async def async_migrate_entry(hass, config_entry: ConfigEntry):
if config_entry.version == 1:
Expand Down

0 comments on commit f9ff46d

Please sign in to comment.