|
| 1 | +# Requirements Document |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +This feature adds device activity history capabilities to the Telegram bot. Users need the ability to view recent events and state changes for specific devices to answer questions like "When was the garage door last opened?" or "What was the temperature in the bedroom over the last hour?". This is particularly useful for troubleshooting, verification, and peace of mind. |
| 6 | + |
| 7 | +## Glossary |
| 8 | + |
| 9 | +- **Device Event**: A state change or attribute update for a device (e.g., switch turned on, temperature changed, door opened) |
| 10 | +- **Event History**: A chronological list of recent events for a device |
| 11 | +- **Maker API Events Endpoint**: The `/apps/api/{appId}/devices/{deviceId}/events` endpoint that returns device event history |
| 12 | +- **Event Attributes**: The properties of an event including timestamp, attribute name, value, and unit |
| 13 | + |
| 14 | +## Requirements |
| 15 | + |
| 16 | +### Requirement 1 |
| 17 | + |
| 18 | +**User Story:** As a user, I want to view recent activity for a specific device, so that I can see when and how the device state changed. |
| 19 | + |
| 20 | +#### Acceptance Criteria |
| 21 | + |
| 22 | +1. WHEN a user sends the /history command with a device name THEN the Telegram Bot SHALL retrieve recent events for that device from the Hubitat Hub |
| 23 | +2. WHEN displaying event history THEN the Telegram Bot SHALL show each event with timestamp, attribute name, and value in a readable format |
| 24 | +3. WHEN the device is not found THEN the Telegram Bot SHALL return an appropriate error message using the existing device name resolution logic |
| 25 | +4. WHEN the device is found but has no recent events THEN the Telegram Bot SHALL return a message indicating no recent activity |
| 26 | +5. WHEN the Maker API request fails THEN the Telegram Bot SHALL return an error message with failure details |
| 27 | + |
| 28 | +### Requirement 2 |
| 29 | + |
| 30 | +**User Story:** As a user, I want to limit the number of events returned, so that I can get a quick overview without being overwhelmed by data. |
| 31 | + |
| 32 | +#### Acceptance Criteria |
| 33 | + |
| 34 | +1. WHEN a user sends /history without specifying a count THEN the Telegram Bot SHALL return the last 10 events by default |
| 35 | +2. WHEN a user sends /history with a count parameter (e.g., /history [device] 20) THEN the Telegram Bot SHALL return up to that many events |
| 36 | +3. WHEN the requested count exceeds available events THEN the Telegram Bot SHALL return all available events |
| 37 | +4. WHEN the count parameter is invalid THEN the Telegram Bot SHALL return an error message and usage instructions |
| 38 | + |
| 39 | +### Requirement 3 |
| 40 | + |
| 41 | +**User Story:** As a user, I want to see timestamps in a readable format, so that I can easily understand when events occurred. |
| 42 | + |
| 43 | +#### Acceptance Criteria |
| 44 | + |
| 45 | +1. WHEN displaying event timestamps THEN the Telegram Bot SHALL format them in a human-readable format (e.g., "2 hours ago", "Dec 10, 3:45 PM") |
| 46 | +2. WHEN events are very recent (< 1 minute) THEN the Telegram Bot SHALL display "Just now" or similar |
| 47 | +3. WHEN events are from today THEN the Telegram Bot SHALL show time only (e.g., "3:45 PM") |
| 48 | +4. WHEN events are from previous days THEN the Telegram Bot SHALL show date and time |
| 49 | + |
| 50 | +### Requirement 4 |
| 51 | + |
| 52 | +**User Story:** As a user, I want to filter events by attribute type, so that I can focus on specific state changes (e.g., only temperature readings, only switch states). |
| 53 | + |
| 54 | +#### Acceptance Criteria |
| 55 | + |
| 56 | +1. WHEN a user sends /history with an attribute filter (e.g., /history [device] temperature) THEN the Telegram Bot SHALL return only events for that attribute |
| 57 | +2. WHEN the attribute filter doesn't match any events THEN the Telegram Bot SHALL return a message indicating no matching events |
| 58 | +3. WHEN no attribute filter is specified THEN the Telegram Bot SHALL return all event types |
| 59 | + |
| 60 | +### Requirement 5 |
| 61 | + |
| 62 | +**User Story:** As a developer, I want the device activity feature to integrate with existing architecture, so that the implementation is consistent and maintainable. |
| 63 | + |
| 64 | +#### Acceptance Criteria |
| 65 | + |
| 66 | +1. WHEN making event API calls THEN the implementation SHALL use the existing NetworkClient interface |
| 67 | +2. WHEN accessing hub configuration THEN the implementation SHALL use the existing BotConfiguration for API credentials |
| 68 | +3. WHEN handling the history command THEN the CommandHandlers object SHALL follow the same pattern as existing command handlers |
| 69 | +4. WHEN resolving device names THEN the implementation SHALL use the existing DeviceManager.findDevice() method |
| 70 | +5. WHEN formatting responses THEN the implementation SHALL follow existing response formatting patterns |
| 71 | + |
| 72 | +## API Details |
| 73 | + |
| 74 | +Based on Hubitat Maker API documentation, the events endpoint: |
| 75 | +- **URL**: `http://{hubIp}/apps/api/{appId}/devices/{deviceId}/events` |
| 76 | +- **Method**: GET |
| 77 | +- **Query Parameters**: |
| 78 | + - `access_token` (required) |
| 79 | + - `max` (optional) - maximum number of events to return |
| 80 | +- **Response**: JSON array of event objects with properties: |
| 81 | + - `name`: attribute name (e.g., "switch", "temperature", "contact") |
| 82 | + - `value`: attribute value (e.g., "on", "72", "open") |
| 83 | + - `unit`: unit of measurement (e.g., "°F", null for non-numeric) |
| 84 | + - `date`: ISO 8601 timestamp |
| 85 | + - `deviceId`: device ID |
| 86 | + |
| 87 | +## Example Usage |
| 88 | + |
| 89 | +``` |
| 90 | +User: /history garage door |
| 91 | +Bot: Recent activity for Garage Door: |
| 92 | + • 2 hours ago: contact = closed |
| 93 | + • 5 hours ago: contact = open |
| 94 | + • 8 hours ago: contact = closed |
| 95 | + • 10 hours ago: contact = open |
| 96 | +
|
| 97 | +User: /history bedroom temp 5 |
| 98 | +Bot: Recent activity for Bedroom Temperature Sensor: |
| 99 | + • Just now: temperature = 72°F |
| 100 | + • 15 minutes ago: temperature = 71°F |
| 101 | + • 30 minutes ago: temperature = 71°F |
| 102 | + • 45 minutes ago: temperature = 70°F |
| 103 | + • 1 hour ago: temperature = 70°F |
| 104 | +
|
| 105 | +User: /history kitchen light temperature |
| 106 | +Bot: Recent temperature events for Kitchen Light: |
| 107 | + No temperature events found. |
| 108 | +``` |
0 commit comments