-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity.py
More file actions
72 lines (56 loc) · 2.32 KB
/
entity.py
File metadata and controls
72 lines (56 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Base entity for Example Integration."""
from __future__ import annotations
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN
from .coordinator import ExampleIntegrationCoordinator
class ExampleIntegrationEntity(CoordinatorEntity[ExampleIntegrationCoordinator]):
"""Base entity for Example Integration.
All entity platforms (sensor, binary_sensor, etc.) should inherit from this class.
It provides common functionality like device registry integration and availability.
"""
_attr_has_entity_name = True
def __init__(
self,
coordinator: ExampleIntegrationCoordinator,
device_id: str,
entity_type: str,
) -> None:
"""Initialize the entity.
Args:
coordinator: The data update coordinator.
device_id: Unique identifier for the device.
entity_type: Type of entity (e.g., "temperature", "humidity").
"""
super().__init__(coordinator)
self._device_id = device_id
self._entity_type = entity_type
@property
def device_info(self) -> DeviceInfo:
"""Return device information for device registry.
This groups all entities from the same device together in the UI.
"""
data = self.coordinator.data
return DeviceInfo(
identifiers={(DOMAIN, self._device_id)},
name=data.get("name", "Example Device"),
manufacturer="Example Manufacturer",
model=data.get("model", "Unknown Model"),
sw_version=data.get("firmware", "Unknown"),
)
@property
def unique_id(self) -> str:
"""Return unique ID for this entity.
The unique ID should be stable across restarts and never change.
Format: domain_deviceid_entitytype
"""
return f"{DOMAIN}_{self._device_id}_{self._entity_type}"
@property
def available(self) -> bool:
"""Return True if entity is available.
Entity is available if:
1. Coordinator has valid data (super().available)
2. Device is marked as online in the data
This prevents showing stale data when device is offline.
"""
return super().available and self.coordinator.data.get("online", False)