core/homeassistant/components/sensor/torque.py

116 lines
3.0 KiB
Python
Raw Normal View History

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/
"""
import re
2016-02-19 05:27:50 +00:00
2015-12-20 16:20:40 +00:00
from homeassistant.const import HTTP_OK
2015-12-19 04:55:37 +00:00
from homeassistant.helpers.entity import Entity
DOMAIN = 'torque'
DEPENDENCIES = ['http']
SENSOR_EMAIL_FIELD = 'eml'
DEFAULT_NAME = 'vehicle'
ENTITY_NAME_FORMAT = '{0} {1}'
2015-12-20 16:20:40 +00:00
API_PATH = '/api/torque'
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)
def decode(value):
2016-02-23 05:21:49 +00:00
"""Double-decode required."""
2015-12-19 04:55:37 +00:00
return value.encode('raw_unicode_escape').decode('utf-8')
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):
2016-03-08 15:46:34 +00:00
"""Setup Torque platform."""
2015-12-19 04:55:37 +00:00
vehicle = config.get('name', DEFAULT_NAME)
email = config.get('email', None)
sensors = {}
def _receive_data(handler, path_match, data):
2016-02-23 05:21:49 +00:00
"""Received data from Torque."""
2015-12-20 16:20:40 +00:00
handler.send_response(HTTP_OK)
handler.end_headers()
2015-12-19 04:55:37 +00:00
if email is not None and email != data[SENSOR_EMAIL_FIELD]:
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))
names[pid] = decode(data[key])
elif is_unit:
pid = convert_pid(is_unit.group(1))
units[pid] = decode(data[key])
elif is_value:
pid = convert_pid(is_value.group(1))
if pid in sensors:
sensors[pid].on_update(data[key])
for pid in names:
if pid not in sensors:
sensors[pid] = TorqueSensor(
ENTITY_NAME_FORMAT.format(vehicle, names[pid]),
units.get(pid, None))
add_devices([sensors[pid]])
2015-12-20 16:20:40 +00:00
hass.http.register_path('GET', API_PATH, _receive_data)
2015-12-19 04:55:37 +00:00
return True
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'
def 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
self.update_ha_state()