Skip to content

Commit

Permalink
voc update
Browse files Browse the repository at this point in the history
  • Loading branch information
molobrakos committed Nov 26, 2018
1 parent d24ea7d commit bf35ecb
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 148 deletions.
21 changes: 10 additions & 11 deletions homeassistant/components/binary_sensor/volvooncall.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
"""
import logging

from homeassistant.components.volvooncall import VolvoEntity
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.volvooncall import VolvoEntity, DATA_KEY
from homeassistant.components.binary_sensor import (
BinarySensorDevice, DEVICE_CLASSES)

_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the Volvo sensors."""
if discovery_info is None:
return
add_entities([VolvoSensor(hass, *discovery_info)])
async_add_entities([VolvoSensor(hass.data[DATA_KEY], *discovery_info)])


class VolvoSensor(VolvoEntity, BinarySensorDevice):
Expand All @@ -25,14 +27,11 @@ class VolvoSensor(VolvoEntity, BinarySensorDevice):
@property
def is_on(self):
"""Return True if the binary sensor is on."""
val = getattr(self.vehicle, self._attribute)
if self._attribute == 'bulb_failures':
return bool(val)
if self._attribute in ['doors', 'windows']:
return any([val[key] for key in val if 'Open' in key])
return val != 'Normal'
return self.instrument.is_on

@property
def device_class(self):
"""Return the class of this sensor, from DEVICE_CLASSES."""
return 'safety'
if self.instrument.device_class in DEVICE_CLASSES:
return self.instrument.device_class
return None
33 changes: 16 additions & 17 deletions homeassistant/components/device_tracker/volvooncall.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,32 @@
import logging

from homeassistant.util import slugify
from homeassistant.helpers.dispatcher import (
dispatcher_connect, dispatcher_send)
from homeassistant.components.volvooncall import DATA_KEY, SIGNAL_VEHICLE_SEEN
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.components.device_tracker import SOURCE_TYPE_GPS
from homeassistant.components.volvooncall import DATA_KEY, SIGNAL_STATE_UPDATED

_LOGGER = logging.getLogger(__name__)


def setup_scanner(hass, config, see, discovery_info=None):
async def async_setup_scanner(hass, config, async_see, discovery_info=None):
"""Set up the Volvo tracker."""
if discovery_info is None:
return

vin, _ = discovery_info
voc = hass.data[DATA_KEY]
vehicle = voc.vehicles[vin]
vin, component, attr = discovery_info
data = hass.data[DATA_KEY]
instrument = data.instrument(vin, component, attr)

def see_vehicle(vehicle):
async def see_vehicle():
"""Handle the reporting of the vehicle position."""
host_name = voc.vehicle_name(vehicle)
host_name = instrument.vehicle_name
dev_id = 'volvo_{}'.format(slugify(host_name))
see(dev_id=dev_id,
host_name=host_name,
gps=(vehicle.position['latitude'],
vehicle.position['longitude']),
icon='mdi:car')

dispatcher_connect(hass, SIGNAL_VEHICLE_SEEN, see_vehicle)
dispatcher_send(hass, SIGNAL_VEHICLE_SEEN, vehicle)
await async_see(dev_id=dev_id,
host_name=host_name,
source_type=SOURCE_TYPE_GPS,
gps=instrument.state,
icon='mdi:car')

async_dispatcher_connect(hass, SIGNAL_STATE_UPDATED, see_vehicle)

return True
17 changes: 9 additions & 8 deletions homeassistant/components/lock/volvooncall.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,18 @@
import logging

from homeassistant.components.lock import LockDevice
from homeassistant.components.volvooncall import VolvoEntity
from homeassistant.components.volvooncall import VolvoEntity, DATA_KEY

_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the Volvo On Call lock."""
if discovery_info is None:
return

add_entities([VolvoLock(hass, *discovery_info)])
async_add_entities([VolvoLock(hass.data[DATA_KEY], *discovery_info)])


class VolvoLock(VolvoEntity, LockDevice):
Expand All @@ -26,12 +27,12 @@ class VolvoLock(VolvoEntity, LockDevice):
@property
def is_locked(self):
"""Return true if lock is locked."""
return self.vehicle.is_locked
return self.instrument.is_locked

def lock(self, **kwargs):
async def async_lock(self, **kwargs):
"""Lock the car."""
self.vehicle.lock()
await self.instrument.lock()

def unlock(self, **kwargs):
async def async_unlock(self, **kwargs):
"""Unlock the car."""
self.vehicle.unlock()
await self.instrument.unlock()
43 changes: 7 additions & 36 deletions homeassistant/components/sensor/volvooncall.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,58 +6,29 @@
"""
import logging
from math import floor

from homeassistant.components.volvooncall import (
VolvoEntity, RESOURCES, CONF_SCANDINAVIAN_MILES)
from homeassistant.components.volvooncall import VolvoEntity, DATA_KEY

_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the Volvo sensors."""
if discovery_info is None:
return
add_entities([VolvoSensor(hass, *discovery_info)])
async_add_entities([VolvoSensor(hass.data[DATA_KEY], *discovery_info)])


class VolvoSensor(VolvoEntity):
"""Representation of a Volvo sensor."""

@property
def state(self):
"""Return the state of the sensor."""
val = getattr(self.vehicle, self._attribute)

if val is None:
return val

if self._attribute == 'odometer':
val /= 1000 # m -> km

if 'mil' in self.unit_of_measurement:
val /= 10 # km -> mil

if self._attribute == 'average_fuel_consumption':
val /= 10 # L/1000km -> L/100km
if 'mil' in self.unit_of_measurement:
return round(val, 2)
return round(val, 1)
if self._attribute == 'distance_to_empty':
return int(floor(val))
return int(round(val))
"""Return the state."""
return self.instrument.state

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
unit = RESOURCES[self._attribute][3]
if self._state.config[CONF_SCANDINAVIAN_MILES] and 'km' in unit:
if self._attribute == 'average_fuel_consumption':
return 'L/mil'
return unit.replace('km', 'mil')
return unit

@property
def icon(self):
"""Return the icon."""
return RESOURCES[self._attribute][2]
return self.instrument.unit
22 changes: 9 additions & 13 deletions homeassistant/components/switch/volvooncall.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@
"""
import logging

from homeassistant.components.volvooncall import VolvoEntity, RESOURCES
from homeassistant.components.volvooncall import VolvoEntity, DATA_KEY
from homeassistant.helpers.entity import ToggleEntity

_LOGGER = logging.getLogger(__name__)


def setup_platform(hass, config, add_entities, discovery_info=None):
async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up a Volvo switch."""
if discovery_info is None:
return
add_entities([VolvoSwitch(hass, *discovery_info)])
async_add_entities([VolvoSwitch(hass.data[DATA_KEY], *discovery_info)])


class VolvoSwitch(VolvoEntity, ToggleEntity):
Expand All @@ -27,17 +28,12 @@ class VolvoSwitch(VolvoEntity, ToggleEntity):
@property
def is_on(self):
"""Return true if switch is on."""
return self.vehicle.is_heater_on
return self.instrument.state

def turn_on(self, **kwargs):
async def async_turn_on(self, **kwargs):
"""Turn the switch on."""
self.vehicle.start_heater()
await self.instrument.turn_on()

def turn_off(self, **kwargs):
async def async_turn_off(self, **kwargs):
"""Turn the switch off."""
self.vehicle.stop_heater()

@property
def icon(self):
"""Return the icon."""
return RESOURCES[self._attribute][2]
await self.instrument.turn_off()
Loading

0 comments on commit bf35ecb

Please sign in to comment.