mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 20:27:08 +00:00
Use shorthand attributes in NextBus (#94084)
* NextBus: Use attr with unique id * Feedback * Remove unique id
This commit is contained in:
parent
228da71cc4
commit
4b4660994c
@ -108,41 +108,19 @@ class NextBusDepartureSensor(SensorEntity):
|
|||||||
self.agency = agency
|
self.agency = agency
|
||||||
self.route = route
|
self.route = route
|
||||||
self.stop = stop
|
self.stop = stop
|
||||||
self._custom_name = name
|
self._attr_extra_state_attributes = {}
|
||||||
# Maybe pull a more user friendly name from the API here
|
|
||||||
self._name = f"{agency} {route}"
|
|
||||||
self._client = client
|
|
||||||
|
|
||||||
# set up default state attributes
|
# Maybe pull a more user friendly name from the API here
|
||||||
self._state = None
|
self._attr_name = f"{agency} {route}"
|
||||||
self._attributes = {}
|
if name:
|
||||||
|
self._attr_name = name
|
||||||
|
|
||||||
|
self._client = client
|
||||||
|
|
||||||
def _log_debug(self, message, *args):
|
def _log_debug(self, message, *args):
|
||||||
"""Log debug message with prefix."""
|
"""Log debug message with prefix."""
|
||||||
_LOGGER.debug(":".join((self.agency, self.route, self.stop, message)), *args)
|
_LOGGER.debug(":".join((self.agency, self.route, self.stop, message)), *args)
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return sensor name.
|
|
||||||
|
|
||||||
Uses an auto generated name based on the data from the API unless a
|
|
||||||
custom name is provided in the configuration.
|
|
||||||
"""
|
|
||||||
if self._custom_name:
|
|
||||||
return self._custom_name
|
|
||||||
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
@property
|
|
||||||
def native_value(self):
|
|
||||||
"""Return current state of the sensor."""
|
|
||||||
return self._state
|
|
||||||
|
|
||||||
@property
|
|
||||||
def extra_state_attributes(self):
|
|
||||||
"""Return additional state attributes."""
|
|
||||||
return self._attributes
|
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Update sensor with new departures times."""
|
"""Update sensor with new departures times."""
|
||||||
# Note: using Multi because there is a bug with the single stop impl
|
# Note: using Multi because there is a bug with the single stop impl
|
||||||
@ -151,21 +129,22 @@ class NextBusDepartureSensor(SensorEntity):
|
|||||||
)
|
)
|
||||||
|
|
||||||
self._log_debug("Predictions results: %s", results)
|
self._log_debug("Predictions results: %s", results)
|
||||||
|
self._attr_attribution = results.get("copyright")
|
||||||
|
|
||||||
if "Error" in results:
|
if "Error" in results:
|
||||||
self._log_debug("Could not get predictions: %s", results)
|
self._log_debug("Could not get predictions: %s", results)
|
||||||
|
|
||||||
if not results.get("predictions"):
|
if not results.get("predictions"):
|
||||||
self._log_debug("No predictions available")
|
self._log_debug("No predictions available")
|
||||||
self._state = None
|
self._attr_native_value = None
|
||||||
# Remove attributes that may now be outdated
|
# Remove attributes that may now be outdated
|
||||||
self._attributes.pop("upcoming", None)
|
self._attr_extra_state_attributes.pop("upcoming", None)
|
||||||
return
|
return
|
||||||
|
|
||||||
results = results["predictions"]
|
results = results["predictions"]
|
||||||
|
|
||||||
# Set detailed attributes
|
# Set detailed attributes
|
||||||
self._attributes.update(
|
self._attr_extra_state_attributes.update(
|
||||||
{
|
{
|
||||||
"agency": results.get("agencyTitle"),
|
"agency": results.get("agencyTitle"),
|
||||||
"route": results.get("routeTitle"),
|
"route": results.get("routeTitle"),
|
||||||
@ -176,13 +155,13 @@ class NextBusDepartureSensor(SensorEntity):
|
|||||||
# List all messages in the attributes
|
# List all messages in the attributes
|
||||||
messages = listify(results.get("message", []))
|
messages = listify(results.get("message", []))
|
||||||
self._log_debug("Messages: %s", messages)
|
self._log_debug("Messages: %s", messages)
|
||||||
self._attributes["message"] = " -- ".join(
|
self._attr_extra_state_attributes["message"] = " -- ".join(
|
||||||
message.get("text", "") for message in messages
|
message.get("text", "") for message in messages
|
||||||
)
|
)
|
||||||
|
|
||||||
# List out all directions in the attributes
|
# List out all directions in the attributes
|
||||||
directions = listify(results.get("direction", []))
|
directions = listify(results.get("direction", []))
|
||||||
self._attributes["direction"] = ", ".join(
|
self._attr_extra_state_attributes["direction"] = ", ".join(
|
||||||
direction.get("title", "") for direction in directions
|
direction.get("title", "") for direction in directions
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -196,14 +175,16 @@ class NextBusDepartureSensor(SensorEntity):
|
|||||||
# Short circuit if we don't have any actual bus predictions
|
# Short circuit if we don't have any actual bus predictions
|
||||||
if not predictions:
|
if not predictions:
|
||||||
self._log_debug("No upcoming predictions available")
|
self._log_debug("No upcoming predictions available")
|
||||||
self._state = None
|
self._attr_native_value = None
|
||||||
self._attributes["upcoming"] = "No upcoming predictions"
|
self._attr_extra_state_attributes["upcoming"] = "No upcoming predictions"
|
||||||
return
|
return
|
||||||
|
|
||||||
# Generate list of upcoming times
|
# Generate list of upcoming times
|
||||||
self._attributes["upcoming"] = ", ".join(
|
self._attr_extra_state_attributes["upcoming"] = ", ".join(
|
||||||
sorted((p["minutes"] for p in predictions), key=int)
|
sorted((p["minutes"] for p in predictions), key=int)
|
||||||
)
|
)
|
||||||
|
|
||||||
latest_prediction = maybe_first(predictions)
|
latest_prediction = maybe_first(predictions)
|
||||||
self._state = utc_from_timestamp(int(latest_prediction["epochTime"]) / 1000)
|
self._attr_native_value = utc_from_timestamp(
|
||||||
|
int(latest_prediction["epochTime"]) / 1000
|
||||||
|
)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user