2015-12-19 04:55:37 +00:00
|
|
|
"""
|
2016-02-23 05:21:49 +00:00
|
|
|
Support for the Torque OBD application.
|
2015-12-19 04:55:37 +00:00
|
|
|
|
|
|
|
For more details about this platform, please refer to the documentation at
|
|
|
|
https://home-assistant.io/components/sensor.torque/
|
|
|
|
"""
|
2016-09-04 16:32:12 +00:00
|
|
|
import logging
|
2015-12-19 04:55:37 +00:00
|
|
|
import re
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2016-09-04 16:32:12 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
from homeassistant.core import callback
|
2016-05-14 07:58:36 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
2016-09-04 16:32:12 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
|
|
|
from homeassistant.const import (CONF_EMAIL, CONF_NAME)
|
|
|
|
from homeassistant.helpers.entity import Entity
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
API_PATH = '/api/torque'
|
2015-12-19 04:55:37 +00:00
|
|
|
|
|
|
|
DEFAULT_NAME = 'vehicle'
|
2016-09-04 16:32:12 +00:00
|
|
|
DEPENDENCIES = ['http']
|
|
|
|
DOMAIN = 'torque'
|
|
|
|
|
2015-12-19 04:55:37 +00:00
|
|
|
ENTITY_NAME_FORMAT = '{0} {1}'
|
|
|
|
|
2016-09-04 16:32:12 +00:00
|
|
|
SENSOR_EMAIL_FIELD = 'eml'
|
2015-12-19 04:55:37 +00:00
|
|
|
SENSOR_NAME_KEY = r'userFullName(\w+)'
|
|
|
|
SENSOR_UNIT_KEY = r'userUnit(\w+)'
|
|
|
|
SENSOR_VALUE_KEY = r'k(\w+)'
|
|
|
|
|
|
|
|
NAME_KEY = re.compile(SENSOR_NAME_KEY)
|
|
|
|
UNIT_KEY = re.compile(SENSOR_UNIT_KEY)
|
|
|
|
VALUE_KEY = re.compile(SENSOR_VALUE_KEY)
|
|
|
|
|
2016-09-04 16:32:12 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
|
|
|
vol.Required(CONF_EMAIL): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
})
|
|
|
|
|
2015-12-19 04:55:37 +00:00
|
|
|
|
|
|
|
def convert_pid(value):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Convert pid from hex string to integer."""
|
2015-12-19 04:55:37 +00:00
|
|
|
return int(value, 16)
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=unused-argument
|
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Torque platform."""
|
2016-09-04 16:32:12 +00:00
|
|
|
vehicle = config.get(CONF_NAME)
|
|
|
|
email = config.get(CONF_EMAIL)
|
2015-12-19 04:55:37 +00:00
|
|
|
sensors = {}
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
hass.http.register_view(TorqueReceiveDataView(
|
2016-11-25 21:04:06 +00:00
|
|
|
email, vehicle, sensors, add_devices))
|
2016-05-14 07:58:36 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class TorqueReceiveDataView(HomeAssistantView):
|
|
|
|
"""Handle data from Torque requests."""
|
|
|
|
|
|
|
|
url = API_PATH
|
|
|
|
name = 'api:torque'
|
2015-12-19 04:55:37 +00:00
|
|
|
|
2016-11-25 21:04:06 +00:00
|
|
|
def __init__(self, email, vehicle, sensors, add_devices):
|
2016-05-14 07:58:36 +00:00
|
|
|
"""Initialize a Torque view."""
|
|
|
|
self.email = email
|
|
|
|
self.vehicle = vehicle
|
|
|
|
self.sensors = sensors
|
|
|
|
self.add_devices = add_devices
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
@callback
|
2016-05-14 07:58:36 +00:00
|
|
|
def get(self, request):
|
|
|
|
"""Handle Torque data request."""
|
2016-11-25 21:04:06 +00:00
|
|
|
hass = request.app['hass']
|
2016-10-24 06:48:01 +00:00
|
|
|
data = request.GET
|
2016-05-14 07:58:36 +00:00
|
|
|
|
|
|
|
if self.email is not None and self.email != data[SENSOR_EMAIL_FIELD]:
|
2015-12-19 04:55:37 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
names = {}
|
|
|
|
units = {}
|
|
|
|
for key in data:
|
|
|
|
is_name = NAME_KEY.match(key)
|
|
|
|
is_unit = UNIT_KEY.match(key)
|
|
|
|
is_value = VALUE_KEY.match(key)
|
|
|
|
|
|
|
|
if is_name:
|
|
|
|
pid = convert_pid(is_name.group(1))
|
2017-03-18 10:25:38 +00:00
|
|
|
names[pid] = data[key]
|
2015-12-19 04:55:37 +00:00
|
|
|
elif is_unit:
|
|
|
|
pid = convert_pid(is_unit.group(1))
|
2017-03-18 10:25:38 +00:00
|
|
|
units[pid] = data[key]
|
2015-12-19 04:55:37 +00:00
|
|
|
elif is_value:
|
|
|
|
pid = convert_pid(is_value.group(1))
|
2016-05-14 07:58:36 +00:00
|
|
|
if pid in self.sensors:
|
2016-10-24 06:48:01 +00:00
|
|
|
self.sensors[pid].async_on_update(data[key])
|
2015-12-19 04:55:37 +00:00
|
|
|
|
|
|
|
for pid in names:
|
2016-05-14 07:58:36 +00:00
|
|
|
if pid not in self.sensors:
|
|
|
|
self.sensors[pid] = TorqueSensor(
|
|
|
|
ENTITY_NAME_FORMAT.format(self.vehicle, names[pid]),
|
2015-12-19 04:55:37 +00:00
|
|
|
units.get(pid, None))
|
2016-11-25 21:04:06 +00:00
|
|
|
hass.async_add_job(self.add_devices, [self.sensors[pid]])
|
2015-12-19 04:55:37 +00:00
|
|
|
|
2017-03-18 10:25:38 +00:00
|
|
|
return "OK!"
|
2015-12-19 04:55:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TorqueSensor(Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Torque sensor."""
|
2015-12-19 04:55:37 +00:00
|
|
|
|
|
|
|
def __init__(self, name, unit):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2015-12-19 04:55:37 +00:00
|
|
|
self._name = name
|
|
|
|
self._unit = unit
|
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-12-19 04:55:37 +00:00
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement."""
|
2015-12-19 04:55:37 +00:00
|
|
|
return self._unit
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-10 07:34:38 +00:00
|
|
|
"""Return the state of the sensor."""
|
2015-12-19 04:55:37 +00:00
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the default icon of the sensor."""
|
2015-12-19 04:55:37 +00:00
|
|
|
return 'mdi:car'
|
|
|
|
|
2016-10-24 06:48:01 +00:00
|
|
|
@callback
|
|
|
|
def async_on_update(self, value):
|
2016-02-23 05:21:49 +00:00
|
|
|
"""Receive an update."""
|
2015-12-19 04:55:37 +00:00
|
|
|
self._state = value
|
2016-11-08 06:31:40 +00:00
|
|
|
self.hass.async_add_job(self.async_update_ha_state())
|