Handle the case where no registration number is available (instead display VIN (vehicle identification number)). (#9073)
parent
e4bb8b0444
commit
418ccc820a
|
@ -20,11 +20,12 @@ def setup_scanner(hass, config, see, discovery_info=None):
|
|||
return
|
||||
|
||||
vin, _ = discovery_info
|
||||
vehicle = hass.data[DATA_KEY].vehicles[vin]
|
||||
voc = hass.data[DATA_KEY]
|
||||
vehicle = voc.vehicles[vin]
|
||||
|
||||
def see_vehicle(vehicle):
|
||||
"""Handle the reporting of the vehicle position."""
|
||||
host_name = vehicle.registration_number
|
||||
host_name = voc.vehicle_name(vehicle)
|
||||
dev_id = 'volvo_{}'.format(slugify(host_name))
|
||||
see(dev_id=dev_id,
|
||||
host_name=host_name,
|
||||
|
|
|
@ -73,14 +73,7 @@ def setup(hass, config):
|
|||
|
||||
interval = config[DOMAIN].get(CONF_UPDATE_INTERVAL)
|
||||
|
||||
class state: # pylint:disable=invalid-name
|
||||
"""Namespace to hold state for each vehicle."""
|
||||
|
||||
entities = {}
|
||||
vehicles = {}
|
||||
names = config[DOMAIN].get(CONF_NAME)
|
||||
|
||||
hass.data[DATA_KEY] = state
|
||||
state = hass.data[DATA_KEY] = VolvoData(config)
|
||||
|
||||
def discover_vehicle(vehicle):
|
||||
"""Load relevant platforms."""
|
||||
|
@ -120,6 +113,31 @@ def setup(hass, config):
|
|||
return update(utcnow())
|
||||
|
||||
|
||||
class VolvoData:
|
||||
"""Hold component state."""
|
||||
|
||||
def __init__(self, config):
|
||||
"""Initialize the component state."""
|
||||
self.entities = {}
|
||||
self.vehicles = {}
|
||||
self.names = config[DOMAIN].get(CONF_NAME)
|
||||
|
||||
def vehicle_name(self, vehicle):
|
||||
"""Provide a friendly name for a vehicle."""
|
||||
if (vehicle.registration_number and
|
||||
vehicle.registration_number.lower()) in self.names:
|
||||
return self.names[vehicle.registration_number.lower()]
|
||||
elif (vehicle.vin and
|
||||
vehicle.vin.lower() in self.names):
|
||||
return self.names[vehicle.vin.lower()]
|
||||
elif vehicle.registration_number:
|
||||
return vehicle.registration_number
|
||||
elif vehicle.vin:
|
||||
return vehicle.vin
|
||||
else:
|
||||
return ''
|
||||
|
||||
|
||||
class VolvoEntity(Entity):
|
||||
"""Base class for all VOC entities."""
|
||||
|
||||
|
@ -139,17 +157,14 @@ class VolvoEntity(Entity):
|
|||
"""Return vehicle."""
|
||||
return self._state.vehicles[self._vin]
|
||||
|
||||
@property
|
||||
def _vehicle_name(self):
|
||||
return (self._state.names.get(self._vin.lower()) or
|
||||
self._state.names.get(
|
||||
self.vehicle.registration_number.lower()) or
|
||||
self.vehicle.registration_number)
|
||||
|
||||
@property
|
||||
def _entity_name(self):
|
||||
return RESOURCES[self._attribute][1]
|
||||
|
||||
@property
|
||||
def _vehicle_name(self):
|
||||
return self._state.vehicle_name(self.vehicle)
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return full name of the entity."""
|
||||
|
|
Loading…
Reference in New Issue