Home Assistant Unofficial Reference 2024.12.1
statistics.py
Go to the documentation of this file.
1 """Class to track subscription event statistics."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 from soco.data_structures_entry import from_didl_string
8 from soco.events_base import Event as SonosEvent, parse_event_xml
9 
10 _LOGGER = logging.getLogger(__name__)
11 
12 
14  """Base class of Sonos statistics."""
15 
16  def __init__(self, zone_name: str, kind: str) -> None:
17  """Initialize SonosStatistics."""
18  self._stats: dict[str, dict[str, int | float]] = {}
19  self._stat_type_stat_type = kind
20  self.zone_namezone_name = zone_name
21 
22  def report(self) -> dict:
23  """Generate a report for use in diagnostics."""
24  return self._stats.copy()
25 
26  def log_report(self) -> None:
27  """Log statistics for this speaker."""
28  _LOGGER.debug(
29  "%s statistics for %s: %s",
30  self._stat_type_stat_type,
31  self.zone_namezone_name,
32  self.reportreport(),
33  )
34 
35 
37  """Representation of Sonos activity statistics."""
38 
39  def __init__(self, zone_name: str) -> None:
40  """Initialize ActivityStatistics."""
41  super().__init__(zone_name, "Activity")
42 
43  def activity(self, source: str, timestamp: float) -> None:
44  """Track an activity occurrence."""
45  activity_entry = self._stats.setdefault(source, {"count": 0})
46  activity_entry["count"] += 1
47  activity_entry["last_seen"] = timestamp
48 
49 
51  """Representation of Sonos event statistics."""
52 
53  def __init__(self, zone_name: str) -> None:
54  """Initialize EventStatistics."""
55  super().__init__(zone_name, "Event")
56 
57  def receive(self, event: SonosEvent) -> None:
58  """Mark a received event by subscription type."""
59  stats_entry = self._stats.setdefault(
60  event.service.service_type, {"received": 0, "duplicates": 0, "processed": 0}
61  )
62  stats_entry["received"] += 1
63 
64  def duplicate(self, event: SonosEvent) -> None:
65  """Mark a duplicate event by subscription type."""
66  self._stats[event.service.service_type]["duplicates"] += 1
67 
68  def process(self, event: SonosEvent) -> None:
69  """Mark a fully processed event by subscription type."""
70  self._stats[event.service.service_type]["processed"] += 1
71 
72  def report(self) -> dict:
73  """Generate a report for use in diagnostics."""
74  payload = self._stats.copy()
75  payload["soco:from_didl_string"] = from_didl_string.cache_info()
76  payload["soco:parse_event_xml"] = parse_event_xml.cache_info()
77  return payload
None activity(self, str source, float timestamp)
Definition: statistics.py:43
None __init__(self, str zone_name, str kind)
Definition: statistics.py:16