|
1 | | -"""Binary sensor for SMART Sniffer — drive health. |
| 1 | +"""Binary sensors for SMART Sniffer — drive health and standby. |
2 | 2 |
|
3 | | -One binary sensor per drive: |
| 3 | +Per-drive binary sensors: |
4 | 4 |
|
5 | | - health — SMART's official pass/fail verdict + NVMe critical_warning. |
6 | | - This is the lagging indicator: drives can report PASSED right up |
7 | | - until catastrophic failure. |
| 5 | + health — SMART's official pass/fail verdict + NVMe critical_warning. |
| 6 | + This is the lagging indicator: drives can report PASSED right up |
| 7 | + until catastrophic failure. |
8 | 8 |
|
9 | | - device_class PROBLEM: on = SMART FAILED, off = SMART PASSED. |
10 | | - Returns None (HA renders "Unknown") when the drive provides no |
11 | | - usable SMART data (e.g., USB enclosures blocking passthrough). |
| 9 | + device_class PROBLEM: on = SMART FAILED, off = SMART PASSED. |
| 10 | + Returns None (HA renders "Unknown") when the drive provides no |
| 11 | + usable SMART data (e.g., USB enclosures blocking passthrough). |
| 12 | +
|
| 13 | + standby — Whether the drive is currently spun down. When on, the SMART |
| 14 | + readings for this drive are being served from cache; the |
| 15 | + sensor exposes a data_as_of attribute so consumers can see |
| 16 | + how stale those readings are. Introduced in v0.5.4. |
12 | 17 |
|
13 | 18 | The early-warning "Attention Needed" sensor lives in sensor.py as an enum |
14 | 19 | sensor (NO / MAYBE / YES / UNSUPPORTED). See attention.py for the logic. |
@@ -103,11 +108,12 @@ async def async_setup_entry( |
103 | 108 |
|
104 | 109 | entities: list[BinarySensorEntity] = [] |
105 | 110 |
|
106 | | - # Per-drive health sensors. |
| 111 | + # Per-drive health + standby sensors. |
107 | 112 | for drive_id, drive_data in coordinator.data.items(): |
108 | 113 | if drive_id.startswith("_"): |
109 | 114 | continue # skip internal keys like _filesystems |
110 | 115 | entities.append(SmartSnifferHealthSensor(coordinator, drive_id, drive_data)) |
| 116 | + entities.append(DriveStandbySensor(coordinator, drive_id, drive_data)) |
111 | 117 |
|
112 | 118 | # Agent-level connectivity and auth sensors. |
113 | 119 | entities.append(AgentStatusBinarySensor(health_coordinator, entry)) |
@@ -174,13 +180,75 @@ def extra_state_attributes(self) -> dict[str, Any]: |
174 | 180 | status = smart_data.get("smart_status", {}) |
175 | 181 | if isinstance(status, dict): |
176 | 182 | attrs["smart_passed"] = status.get("passed") |
177 | | - # Standby attributes when drive is sleeping. |
| 183 | + # DEPRECATED (v0.5.4): the in_standby and data_as_of attributes on |
| 184 | + # this sensor are superseded by the dedicated DriveStandbySensor |
| 185 | + # (binary_sensor.*_standby). Kept here for backward compatibility |
| 186 | + # with v0.5.3 automations/templates. Planned removal in a future |
| 187 | + # release. See docs/internal/process/deprecations.md. |
178 | 188 | if drive_data.get("in_standby"): |
179 | 189 | attrs["in_standby"] = True |
180 | 190 | attrs["data_as_of"] = drive_data.get("last_updated", "unknown") |
181 | 191 | return attrs |
182 | 192 |
|
183 | 193 |
|
| 194 | +class DriveStandbySensor( |
| 195 | + CoordinatorEntity[SmartSnifferCoordinator], BinarySensorEntity |
| 196 | +): |
| 197 | + """Binary sensor showing whether the drive is currently in standby. |
| 198 | +
|
| 199 | + on = drive is spun down / sleeping; SMART data served from cache |
| 200 | + off = drive is active; SMART data is fresh |
| 201 | +
|
| 202 | + When on, exposes `data_as_of` as an attribute so consumers can see how |
| 203 | + stale the cached readings are. Introduced in v0.5.4 as the canonical |
| 204 | + replacement for the in_standby/data_as_of attributes previously attached |
| 205 | + to the Health sensor. |
| 206 | + """ |
| 207 | + |
| 208 | + _attr_has_entity_name = True |
| 209 | + _attr_name = "Standby" |
| 210 | + _attr_entity_category = EntityCategory.DIAGNOSTIC |
| 211 | + _attr_entity_registry_enabled_default = True |
| 212 | + |
| 213 | + def __init__( |
| 214 | + self, |
| 215 | + coordinator: SmartSnifferCoordinator, |
| 216 | + drive_id: str, |
| 217 | + drive_data: dict[str, Any], |
| 218 | + ) -> None: |
| 219 | + super().__init__(coordinator) |
| 220 | + self._drive_id = drive_id |
| 221 | + model = drive_data.get("model", "Unknown Drive") |
| 222 | + serial = drive_data.get("serial", drive_id) |
| 223 | + self._attr_unique_id = ( |
| 224 | + f"{coordinator.config_entry.entry_id}_{drive_id}_standby" |
| 225 | + ) |
| 226 | + self._attr_device_info = { |
| 227 | + "identifiers": {(DOMAIN, drive_id)}, |
| 228 | + "name": f"{model} ({serial})", |
| 229 | + "manufacturer": model.split()[0] if model else "Unknown", |
| 230 | + "model": model, |
| 231 | + "serial_number": serial, |
| 232 | + } |
| 233 | + |
| 234 | + @property |
| 235 | + def is_on(self) -> bool: |
| 236 | + """Return True when the drive is in standby.""" |
| 237 | + drive_data = self.coordinator.data.get(self._drive_id) or {} |
| 238 | + return bool(drive_data.get("in_standby", False)) |
| 239 | + |
| 240 | + @property |
| 241 | + def icon(self) -> str: |
| 242 | + return "mdi:sleep" if self.is_on else "mdi:power" |
| 243 | + |
| 244 | + @property |
| 245 | + def extra_state_attributes(self) -> dict[str, Any]: |
| 246 | + if not self.is_on: |
| 247 | + return {} |
| 248 | + drive_data = self.coordinator.data.get(self._drive_id) or {} |
| 249 | + return {"data_as_of": drive_data.get("last_updated", "unknown")} |
| 250 | + |
| 251 | + |
184 | 252 | # --------------------------------------------------------------------------- |
185 | 253 | # Agent connectivity binary sensor |
186 | 254 | # --------------------------------------------------------------------------- |
|
0 commit comments