From 61a5c0de5ed1818edfcadc527062f77b4f82b189 Mon Sep 17 00:00:00 2001 From: Jan-Philipp Benecke Date: Tue, 28 Nov 2023 13:44:40 +0100 Subject: [PATCH] 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 --------- Co-authored-by: Christopher Fenner <9592452+CFenner@users.noreply.github.com> Co-authored-by: Joost Lekkerkerker --- .../hvv_departures/binary_sensor.py | 50 +++++++------------ .../components/hvv_departures/sensor.py | 30 +++++------ 2 files changed, 30 insertions(+), 50 deletions(-) diff --git a/homeassistant/components/hvv_departures/binary_sensor.py b/homeassistant/components/hvv_departures/binary_sensor.py index 0ec08e9c791..8337921acf6 100644 --- a/homeassistant/components/hvv_departures/binary_sensor.py +++ b/homeassistant/components/hvv_departures/binary_sensor.py @@ -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.""" diff --git a/homeassistant/components/hvv_departures/sensor.py b/homeassistant/components/hvv_departures/sensor.py index 76a7966a6ed..a8efb663c90 100644 --- a/homeassistant/components/hvv_departures/sensor.py +++ b/homeassistant/components/hvv_departures/sensor.py @@ -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"], - )