Minor cleanup in Waze travel times (#55422)

* reduce imports and clean the attriburts

* add icons

* use entity class attributes

* fix icon

* add misc types

* fix update

* do not change icon yet

* address review
This commit is contained in:
Yuval Aboulafia 2021-09-03 11:54:32 +03:00 committed by GitHub
parent 4684ea2d14
commit 91cd6951f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 61 deletions

View File

@ -3,14 +3,6 @@ from homeassistant.const import CONF_UNIT_SYSTEM_IMPERIAL, CONF_UNIT_SYSTEM_METR
DOMAIN = "waze_travel_time" DOMAIN = "waze_travel_time"
ATTR_DESTINATION = "destination"
ATTR_DURATION = "duration"
ATTR_DISTANCE = "distance"
ATTR_ORIGIN = "origin"
ATTR_ROUTE = "route"
ATTRIBUTION = "Powered by Waze"
CONF_DESTINATION = "destination" CONF_DESTINATION = "destination"
CONF_ORIGIN = "origin" CONF_ORIGIN = "origin"
CONF_INCL_FILTER = "incl_filter" CONF_INCL_FILTER = "incl_filter"
@ -29,8 +21,6 @@ DEFAULT_AVOID_TOLL_ROADS = False
DEFAULT_AVOID_SUBSCRIPTION_ROADS = False DEFAULT_AVOID_SUBSCRIPTION_ROADS = False
DEFAULT_AVOID_FERRIES = False DEFAULT_AVOID_FERRIES = False
ICON = "mdi:car"
UNITS = [CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL] UNITS = [CONF_UNIT_SYSTEM_METRIC, CONF_UNIT_SYSTEM_IMPERIAL]
REGIONS = ["US", "NA", "EU", "IL", "AU"] REGIONS = ["US", "NA", "EU", "IL", "AU"]

View File

@ -22,16 +22,10 @@ from homeassistant.const import (
) )
from homeassistant.core import Config, CoreState, HomeAssistant from homeassistant.core import Config, CoreState, HomeAssistant
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import DiscoveryInfoType
from .const import ( from .const import (
ATTR_DESTINATION,
ATTR_DISTANCE,
ATTR_DURATION,
ATTR_ORIGIN,
ATTR_ROUTE,
ATTRIBUTION,
CONF_AVOID_FERRIES, CONF_AVOID_FERRIES,
CONF_AVOID_SUBSCRIPTION_ROADS, CONF_AVOID_SUBSCRIPTION_ROADS,
CONF_AVOID_TOLL_ROADS, CONF_AVOID_TOLL_ROADS,
@ -50,7 +44,6 @@ from .const import (
DEFAULT_VEHICLE_TYPE, DEFAULT_VEHICLE_TYPE,
DOMAIN, DOMAIN,
ENTITY_ID_PATTERN, ENTITY_ID_PATTERN,
ICON,
REGIONS, REGIONS,
UNITS, UNITS,
VEHICLE_TYPES, VEHICLE_TYPES,
@ -90,8 +83,11 @@ PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
async def async_setup_platform( async def async_setup_platform(
hass: HomeAssistant, config: Config, async_add_entities, discovery_info=None hass: HomeAssistant,
): config: Config,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Waze travel time sensor platform.""" """Set up the Waze travel time sensor platform."""
hass.async_create_task( hass.async_create_task(
@ -166,14 +162,23 @@ async def async_setup_entry(
class WazeTravelTime(SensorEntity): class WazeTravelTime(SensorEntity):
"""Representation of a Waze travel time sensor.""" """Representation of a Waze travel time sensor."""
_attr_native_unit_of_measurement = TIME_MINUTES
_attr_device_info = {
"name": "Waze",
"identifiers": {(DOMAIN, DOMAIN)},
"entry_type": "service",
}
def __init__(self, unique_id, name, origin, destination, waze_data): def __init__(self, unique_id, name, origin, destination, waze_data):
"""Initialize the Waze travel time sensor.""" """Initialize the Waze travel time sensor."""
self._unique_id = unique_id self._attr_unique_id = unique_id
self._waze_data = waze_data self._waze_data = waze_data
self._name = name self._attr_name = name
self._attr_icon = "mdi:car"
self._state = None self._state = None
self._origin_entity_id = None self._origin_entity_id = None
self._destination_entity_id = None self._destination_entity_id = None
cmpl_re = re.compile(ENTITY_ID_PATTERN) cmpl_re = re.compile(ENTITY_ID_PATTERN)
if cmpl_re.fullmatch(origin): if cmpl_re.fullmatch(origin):
_LOGGER.debug("Found origin source entity %s", origin) _LOGGER.debug("Found origin source entity %s", origin)
@ -197,12 +202,7 @@ class WazeTravelTime(SensorEntity):
await self.first_update() await self.first_update()
@property @property
def name(self): def native_value(self) -> float | None:
"""Return the name of the sensor."""
return self._name
@property
def native_value(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
if self._waze_data.duration is not None: if self._waze_data.duration is not None:
return round(self._waze_data.duration) return round(self._waze_data.duration)
@ -210,28 +210,19 @@ class WazeTravelTime(SensorEntity):
return None return None
@property @property
def native_unit_of_measurement(self): def extra_state_attributes(self) -> dict | None:
"""Return the unit of measurement."""
return TIME_MINUTES
@property
def icon(self):
"""Icon to use in the frontend, if any."""
return ICON
@property
def extra_state_attributes(self):
"""Return the state attributes of the last update.""" """Return the state attributes of the last update."""
if self._waze_data.duration is None: if self._waze_data.duration is None:
return None return None
res = {ATTR_ATTRIBUTION: ATTRIBUTION} return {
res[ATTR_DURATION] = self._waze_data.duration ATTR_ATTRIBUTION: "Powered by Waze",
res[ATTR_DISTANCE] = self._waze_data.distance "duration": self._waze_data.duration,
res[ATTR_ROUTE] = self._waze_data.route "distance": self._waze_data.distance,
res[ATTR_ORIGIN] = self._waze_data.origin "route": self._waze_data.route,
res[ATTR_DESTINATION] = self._waze_data.destination "origin": self._waze_data.origin,
return res "destination": self._waze_data.destination,
}
async def first_update(self, _=None): async def first_update(self, _=None):
"""Run first update and write state.""" """Run first update and write state."""
@ -240,7 +231,7 @@ class WazeTravelTime(SensorEntity):
def update(self): def update(self):
"""Fetch new state data for the sensor.""" """Fetch new state data for the sensor."""
_LOGGER.debug("Fetching Route for %s", self._name) _LOGGER.debug("Fetching Route for %s", self._attr_name)
# Get origin latitude and longitude from entity_id. # Get origin latitude and longitude from entity_id.
if self._origin_entity_id is not None: if self._origin_entity_id is not None:
self._waze_data.origin = get_location_from_entity( self._waze_data.origin = get_location_from_entity(
@ -263,20 +254,6 @@ class WazeTravelTime(SensorEntity):
self._waze_data.update() self._waze_data.update()
@property
def device_info(self) -> DeviceInfo:
"""Return device specific attributes."""
return {
"name": "Waze",
"identifiers": {(DOMAIN, DOMAIN)},
"entry_type": "service",
}
@property
def unique_id(self) -> str:
"""Return unique ID of entity."""
return self._unique_id
class WazeTravelTimeData: class WazeTravelTimeData:
"""WazeTravelTime Data object.""" """WazeTravelTime Data object."""