Use shorthand attributes in HVV departures (#104637)

* Use shorthand attributes in HVV departures

* Apply code review suggestion

Co-authored-by: Christopher Fenner <9592452+CFenner@users.noreply.github.com>

* Apply code review sugesstion

Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>

---------

Co-authored-by: Christopher Fenner <9592452+CFenner@users.noreply.github.com>
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
pull/104647/head
Jan-Philipp Benecke 2023-11-28 13:44:40 +01:00 committed by GitHub
parent 9dc5d4a1bb
commit 61a5c0de5e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 50 deletions

View File

@ -125,13 +125,29 @@ class HvvDepartureBinarySensor(CoordinatorEntity, BinarySensorEntity):
_attr_attribution = ATTRIBUTION
_attr_has_entity_name = True
_attr_device_class = BinarySensorDeviceClass.PROBLEM
def __init__(self, coordinator, idx, config_entry):
"""Initialize."""
super().__init__(coordinator)
self.coordinator = coordinator
self.idx = idx
self.config_entry = config_entry
self._attr_name = coordinator.data[idx]["name"]
self._attr_unique_id = idx
self._attr_device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={
(
DOMAIN,
config_entry.entry_id,
config_entry.data[CONF_STATION]["id"],
config_entry.data[CONF_STATION]["type"],
)
},
manufacturer=MANUFACTURER,
name=f"Departures at {config_entry.data[CONF_STATION]['name']}",
)
@property
def is_on(self):
@ -146,38 +162,6 @@ class HvvDepartureBinarySensor(CoordinatorEntity, BinarySensorEntity):
and self.coordinator.data[self.idx]["available"]
)
@property
def device_info(self):
"""Return the device info for this sensor."""
return DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={
(
DOMAIN,
self.config_entry.entry_id,
self.config_entry.data[CONF_STATION]["id"],
self.config_entry.data[CONF_STATION]["type"],
)
},
manufacturer=MANUFACTURER,
name=f"Departures at {self.config_entry.data[CONF_STATION]['name']}",
)
@property
def name(self):
"""Return the name of the sensor."""
return self.coordinator.data[self.idx]["name"]
@property
def unique_id(self):
"""Return a unique ID to use for this sensor."""
return self.idx
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return BinarySensorDeviceClass.PROBLEM
@property
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return the state attributes."""

View File

@ -73,6 +73,19 @@ class HVVDepartureSensor(SensorEntity):
station_id = config_entry.data[CONF_STATION]["id"]
station_type = config_entry.data[CONF_STATION]["type"]
self._attr_unique_id = f"{config_entry.entry_id}-{station_id}-{station_type}"
self._attr_device_info = DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={
(
DOMAIN,
config_entry.entry_id,
config_entry.data[CONF_STATION]["id"],
config_entry.data[CONF_STATION]["type"],
)
},
manufacturer=MANUFACTURER,
name=config_entry.data[CONF_STATION]["name"],
)
@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update(self, **kwargs: Any) -> None:
@ -165,20 +178,3 @@ class HVVDepartureSensor(SensorEntity):
}
)
self._attr_extra_state_attributes[ATTR_NEXT] = departures
@property
def device_info(self):
"""Return the device info for this sensor."""
return DeviceInfo(
entry_type=DeviceEntryType.SERVICE,
identifiers={
(
DOMAIN,
self.config_entry.entry_id,
self.config_entry.data[CONF_STATION]["id"],
self.config_entry.data[CONF_STATION]["type"],
)
},
manufacturer=MANUFACTURER,
name=self.config_entry.data[CONF_STATION]["name"],
)