mirror of
https://github.com/home-assistant/core.git
synced 2025-07-21 12:17:07 +00:00
Use constructor instead of factory method for sensors in here_travel_time (#76471)
This commit is contained in:
parent
929eeac1e4
commit
b19ace9124
@ -153,38 +153,6 @@ def sensor_descriptions(travel_mode: str) -> tuple[SensorEntityDescription, ...]
|
||||
)
|
||||
|
||||
|
||||
def create_origin_sensor(
|
||||
config_entry: ConfigEntry, hass: HomeAssistant
|
||||
) -> OriginSensor:
|
||||
"""Create a origin sensor."""
|
||||
return OriginSensor(
|
||||
config_entry.entry_id,
|
||||
config_entry.data[CONF_NAME],
|
||||
SensorEntityDescription(
|
||||
name="Origin",
|
||||
icon="mdi:store-marker",
|
||||
key=ATTR_ORIGIN_NAME,
|
||||
),
|
||||
hass.data[DOMAIN][config_entry.entry_id],
|
||||
)
|
||||
|
||||
|
||||
def create_destination_sensor(
|
||||
config_entry: ConfigEntry, hass: HomeAssistant
|
||||
) -> DestinationSensor:
|
||||
"""Create a destination sensor."""
|
||||
return DestinationSensor(
|
||||
config_entry.entry_id,
|
||||
config_entry.data[CONF_NAME],
|
||||
SensorEntityDescription(
|
||||
name="Destination",
|
||||
icon="mdi:store-marker",
|
||||
key=ATTR_DESTINATION_NAME,
|
||||
),
|
||||
hass.data[DOMAIN][config_entry.entry_id],
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_platform(
|
||||
hass: HomeAssistant,
|
||||
config: ConfigType,
|
||||
@ -214,18 +182,22 @@ async def async_setup_entry(
|
||||
) -> None:
|
||||
"""Add HERE travel time entities from a config_entry."""
|
||||
|
||||
entry_id = config_entry.entry_id
|
||||
name = config_entry.data[CONF_NAME]
|
||||
coordinator = hass.data[DOMAIN][entry_id]
|
||||
|
||||
sensors: list[HERETravelTimeSensor] = []
|
||||
for sensor_description in sensor_descriptions(config_entry.data[CONF_MODE]):
|
||||
sensors.append(
|
||||
HERETravelTimeSensor(
|
||||
config_entry.entry_id,
|
||||
config_entry.data[CONF_NAME],
|
||||
entry_id,
|
||||
name,
|
||||
sensor_description,
|
||||
hass.data[DOMAIN][config_entry.entry_id],
|
||||
coordinator,
|
||||
)
|
||||
)
|
||||
sensors.append(create_origin_sensor(config_entry, hass))
|
||||
sensors.append(create_destination_sensor(config_entry, hass))
|
||||
sensors.append(OriginSensor(entry_id, name, coordinator))
|
||||
sensors.append(DestinationSensor(entry_id, name, coordinator))
|
||||
async_add_entities(sensors)
|
||||
|
||||
|
||||
@ -278,6 +250,20 @@ class HERETravelTimeSensor(SensorEntity, CoordinatorEntity):
|
||||
class OriginSensor(HERETravelTimeSensor):
|
||||
"""Sensor holding information about the route origin."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
unique_id_prefix: str,
|
||||
name: str,
|
||||
coordinator: HereTravelTimeDataUpdateCoordinator,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
sensor_description = SensorEntityDescription(
|
||||
name="Origin",
|
||||
icon="mdi:store-marker",
|
||||
key=ATTR_ORIGIN_NAME,
|
||||
)
|
||||
super().__init__(unique_id_prefix, name, sensor_description, coordinator)
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
||||
"""GPS coordinates."""
|
||||
@ -292,6 +278,20 @@ class OriginSensor(HERETravelTimeSensor):
|
||||
class DestinationSensor(HERETravelTimeSensor):
|
||||
"""Sensor holding information about the route destination."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
unique_id_prefix: str,
|
||||
name: str,
|
||||
coordinator: HereTravelTimeDataUpdateCoordinator,
|
||||
) -> None:
|
||||
"""Initialize the sensor."""
|
||||
sensor_description = SensorEntityDescription(
|
||||
name="Destination",
|
||||
icon="mdi:store-marker",
|
||||
key=ATTR_DESTINATION_NAME,
|
||||
)
|
||||
super().__init__(unique_id_prefix, name, sensor_description, coordinator)
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> Mapping[str, Any] | None:
|
||||
"""GPS coordinates."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user