Provide entity_id to avoid sensor mixup (fixes #7636). Use async_dispatcher. Provide icon. (#7946)

* Avoid sensor mixup. Fixes #7636. Also provide icon. Plus some smaller
fixes.

* fix async p1

* Create volvooncall.py
pull/8054/head
Erik Eriksson 2017-06-16 07:28:30 +02:00 committed by Paulus Schoutsen
parent 0eaad46d93
commit a119bd0056
2 changed files with 27 additions and 20 deletions

View File

@ -7,7 +7,10 @@ https://home-assistant.io/components/device_tracker.volvooncall/
import logging import logging
from homeassistant.util import slugify from homeassistant.util import slugify
from homeassistant.components.volvooncall import DOMAIN from homeassistant.helpers.dispatcher import (
dispatcher_connect, dispatcher_send)
from homeassistant.components.volvooncall import (
DATA_KEY, SIGNAL_VEHICLE_SEEN)
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -18,19 +21,19 @@ def setup_scanner(hass, config, see, discovery_info=None):
return return
vin, _ = discovery_info vin, _ = discovery_info
vehicle = hass.data[DOMAIN].vehicles[vin] vehicle = hass.data[DATA_KEY].vehicles[vin]
host_name = vehicle.registration_number
dev_id = 'volvo_' + slugify(host_name)
def see_vehicle(vehicle): def see_vehicle(vehicle):
"""Handle the reporting of the vehicle position.""" """Handle the reporting of the vehicle position."""
host_name = vehicle.registration_number
dev_id = 'volvo_{}'.format(slugify(host_name))
see(dev_id=dev_id, see(dev_id=dev_id,
host_name=host_name, host_name=host_name,
gps=(vehicle.position['latitude'], gps=(vehicle.position['latitude'],
vehicle.position['longitude'])) vehicle.position['longitude']),
icon='mdi:car')
hass.data[DOMAIN].entities[vin].append(see_vehicle) dispatcher_connect(hass, SIGNAL_VEHICLE_SEEN, see_vehicle)
see_vehicle(vehicle) dispatcher_send(hass, SIGNAL_VEHICLE_SEEN, vehicle)
return True return True

View File

@ -14,11 +14,14 @@ from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.helpers.event import track_point_in_utc_time from homeassistant.helpers.event import track_point_in_utc_time
from homeassistant.helpers.dispatcher import dispatcher_send
from homeassistant.util.dt import utcnow from homeassistant.util.dt import utcnow
import voluptuous as vol import voluptuous as vol
DOMAIN = 'volvooncall' DOMAIN = 'volvooncall'
DATA_KEY = DOMAIN
REQUIREMENTS = ['volvooncall==0.3.3'] REQUIREMENTS = ['volvooncall==0.3.3']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -28,12 +31,15 @@ MIN_UPDATE_INTERVAL = timedelta(minutes=1)
DEFAULT_UPDATE_INTERVAL = timedelta(minutes=1) DEFAULT_UPDATE_INTERVAL = timedelta(minutes=1)
CONF_SERVICE_URL = 'service_url' CONF_SERVICE_URL = 'service_url'
SIGNAL_VEHICLE_SEEN = '{}.vehicle_seen'.format(DOMAIN)
RESOURCES = {'position': ('device_tracker',), RESOURCES = {'position': ('device_tracker',),
'lock': ('lock', 'Lock'), 'lock': ('lock', 'Lock'),
'heater': ('switch', 'Heater', 'mdi:radiator'), 'heater': ('switch', 'Heater', 'mdi:radiator'),
'odometer': ('sensor', 'Odometer', 'mdi:speedometer', 'km'), 'odometer': ('sensor', 'Odometer', 'mdi:speedometer', 'km'),
'fuel_amount': ('sensor', 'Fuel', 'mdi:gas-station', 'L'), 'fuel_amount': ('sensor', 'Fuel amount', 'mdi:gas-station', 'L'),
'fuel_amount_level': ('sensor', 'Fuel', 'mdi:water-percent', '%'), 'fuel_amount_level': (
'sensor', 'Fuel level', 'mdi:water-percent', '%'),
'distance_to_empty': ('sensor', 'Range', 'mdi:ruler', 'km'), 'distance_to_empty': ('sensor', 'Range', 'mdi:ruler', 'km'),
'washer_fluid_level': ('binary_sensor', 'Washer fluid'), 'washer_fluid_level': ('binary_sensor', 'Washer fluid'),
'brake_fluid': ('binary_sensor', 'Brake Fluid'), 'brake_fluid': ('binary_sensor', 'Brake Fluid'),
@ -59,8 +65,7 @@ CONFIG_SCHEMA = vol.Schema({
def setup(hass, config): def setup(hass, config):
"""Set up the Volvo On Call component.""" """Set up the Volvo On Call component."""
from volvooncall import DEFAULT_SERVICE_URL from volvooncall import Connection, DEFAULT_SERVICE_URL
from volvooncall import Connection
connection = Connection( connection = Connection(
config[DOMAIN].get(CONF_USERNAME), config[DOMAIN].get(CONF_USERNAME),
config[DOMAIN].get(CONF_PASSWORD), config[DOMAIN].get(CONF_PASSWORD),
@ -75,7 +80,7 @@ def setup(hass, config):
vehicles = {} vehicles = {}
names = config[DOMAIN].get(CONF_NAME) names = config[DOMAIN].get(CONF_NAME)
hass.data[DOMAIN] = state hass.data[DATA_KEY] = state
def discover_vehicle(vehicle): def discover_vehicle(vehicle):
"""Load relevant platforms.""" """Load relevant platforms."""
@ -93,10 +98,9 @@ def setup(hass, config):
discover_vehicle(vehicle) discover_vehicle(vehicle)
for entity in state.entities[vehicle.vin]: for entity in state.entities[vehicle.vin]:
if isinstance(entity, Entity): entity.schedule_update_ha_state()
entity.schedule_update_ha_state()
else: dispatcher_send(hass, SIGNAL_VEHICLE_SEEN, vehicle)
entity(vehicle) # device tracker
def update(now): def update(now):
"""Update status from the online service.""" """Update status from the online service."""
@ -128,7 +132,7 @@ class VolvoEntity(Entity):
@property @property
def _state(self): def _state(self):
return self._hass.data[DOMAIN] return self._hass.data[DATA_KEY]
@property @property
def vehicle(self): def vehicle(self):
@ -149,7 +153,7 @@ class VolvoEntity(Entity):
@property @property
def name(self): def name(self):
"""Return full name of the entity.""" """Return full name of the entity."""
return '%s %s' % ( return '{} {}'.format(
self._vehicle_name, self._vehicle_name,
self._entity_name) self._entity_name)
@ -166,6 +170,6 @@ class VolvoEntity(Entity):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return device specific state attributes.""" """Return device specific state attributes."""
return dict(model='%s/%s' % ( return dict(model='{}/{}'.format(
self.vehicle.vehicle_type, self.vehicle.vehicle_type,
self.vehicle.model_year)) self.vehicle.model_year))