|
10 | 10 | from homeassistant.components.nest.device_info import NestDeviceInfo |
11 | 11 | from homeassistant.components.sensor import SensorDeviceClass, SensorEntity |
12 | 12 | from homeassistant.core import HomeAssistant |
13 | | -from homeassistant.helpers.entity import cached_property |
| 13 | +import homeassistant.helpers.device_registry as dr |
14 | 14 | from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
15 | 15 |
|
16 | 16 | from . import GoogleNestFan |
@@ -47,38 +47,35 @@ def __init__(self, hass: HomeAssistant, device: Device) -> None: |
47 | 47 | """Initialize the sensor.""" |
48 | 48 | super().__init__() |
49 | 49 | self._device: Device = device |
50 | | - self._device_info = NestDeviceInfo(device) |
| 50 | + self._device_info: NestDeviceInfo = NestDeviceInfo(device) |
51 | 51 | # The API "name" field is a unique device identifier. |
52 | 52 | self._attr_unique_id = f"{device.name}-{self.device_class}" |
53 | | - self._attr_device_info = self._device_info.device_info |
| 53 | + if "identifiers" in self._device_info.device_info: |
| 54 | + self.device_entry = dr.async_get(hass).async_get_device( |
| 55 | + self._device_info.device_info["identifiers"] |
| 56 | + ) |
54 | 57 |
|
55 | | - @cached_property |
56 | | - def available(self) -> bool: |
| 58 | + @property |
| 59 | + def available(self) -> bool: # pyright: ignore[reportIncompatibleVariableOverride] |
57 | 60 | """Return device availability.""" |
58 | | - return self._device_info.available |
59 | | - |
60 | | - async def async_update(self) -> None: |
61 | | - """Called on init.""" |
62 | | - self.async_write_ha_state() |
| 61 | + return NestDeviceInfo(self._device).available |
63 | 62 |
|
64 | 63 | async def async_added_to_hass(self) -> None: |
65 | 64 | """Run when entity is added to register update signal handler.""" |
66 | 65 | self.async_on_remove( |
67 | 66 | self._device.add_update_listener(self.async_write_ha_state) |
68 | 67 | ) |
69 | | - await self.async_update() |
70 | 68 |
|
71 | | - @cached_property |
72 | | - def native_value(self) -> datetime.datetime | str: |
| 69 | + @property |
| 70 | + def native_value(self) -> datetime.datetime | None: # pyright: ignore[reportIncompatibleVariableOverride] |
73 | 71 | """Return the state of the sensor.""" |
74 | 72 | fan_trait: FanTrait = self._device.traits[FanTrait.NAME] |
75 | 73 | _LOGGER.debug( |
76 | | - "Updating fan timer timeout: %s, timerMode: %s", |
| 74 | + "Updating %s: %s, timerMode: %s", |
| 75 | + self.entity_id, |
77 | 76 | fan_trait.timer_timeout, |
78 | 77 | fan_trait.timer_mode, |
79 | 78 | ) |
80 | 79 | if not fan_trait.timer_timeout or fan_trait.timer_mode == "OFF": |
81 | | - self._attr_device_class = None |
82 | | - return "Not Running" |
83 | | - self._attr_device_class = SensorDeviceClass.TIMESTAMP |
| 80 | + return None |
84 | 81 | return fan_trait.timer_timeout |
0 commit comments