-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add device registry support to group entities under their stations (#7)
feat: add last update sensor to track data freshness closes #1
- Loading branch information
1 parent
c83704b
commit 2d0a6d5
Showing
7 changed files
with
129 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
from homeassistant.helpers.entity import DeviceInfo | ||
from homeassistant.components.sensor import ( | ||
SensorEntity, | ||
SensorDeviceClass, | ||
) | ||
|
||
from .const import DOMAIN | ||
|
||
class WeatherXMLastUpdateSensor(CoordinatorEntity, SensorEntity): | ||
"""WeatherXM Last Update Sensor.""" | ||
|
||
_attr_device_class = SensorDeviceClass.TIMESTAMP | ||
|
||
def __init__(self, coordinator, device_id, alias): | ||
"""Initialize.""" | ||
super().__init__(coordinator) | ||
self._device_id = device_id | ||
self._alias = alias | ||
self._attr_name = f"{alias} Last Update" | ||
self._attr_unique_id = f"{alias}_last_update" | ||
|
||
def _get_device_data(self): | ||
"""Get device data from coordinator.""" | ||
if not self.coordinator.data: | ||
return None | ||
for device in self.coordinator.data: | ||
if device['id'] == self._device_id: | ||
return device | ||
return None | ||
|
||
@property | ||
def state(self): | ||
"""Return the state of the sensor.""" | ||
device = self._get_device_data() | ||
if device and device.get('current_weather', {}).get('timestamp'): | ||
return device['current_weather']['timestamp'] | ||
return None | ||
|
||
@property | ||
def device_info(self) -> DeviceInfo: | ||
"""Return device information.""" | ||
return DeviceInfo( | ||
identifiers={(DOMAIN, self._device_id)}, | ||
name=self._alias, | ||
manufacturer="WeatherXM", | ||
model="Weather Station", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters