2019-04-03 15:40:03 +00:00
|
|
|
"""Support for the Torque OBD application."""
|
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-05-14 07:58:36 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
2021-03-22 18:47:44 +00:00
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
|
2020-04-21 17:45:53 +00:00
|
|
|
from homeassistant.const import CONF_EMAIL, CONF_NAME, DEGREE
|
2019-12-09 13:41:48 +00:00
|
|
|
from homeassistant.core import callback
|
2016-09-04 16:32:12 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
API_PATH = "/api/torque"
|
2015-12-19 04:55:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DEFAULT_NAME = "vehicle"
|
|
|
|
DOMAIN = "torque"
|
2016-09-04 16:32:12 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ENTITY_NAME_FORMAT = "{0} {1}"
|
2015-12-19 04:55:37 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_EMAIL_FIELD = "eml"
|
|
|
|
SENSOR_NAME_KEY = r"userFullName(\w+)"
|
|
|
|
SENSOR_UNIT_KEY = r"userUnit(\w+)"
|
|
|
|
SENSOR_VALUE_KEY = r"k(\w+)"
|
2015-12-19 04:55:37 +00:00
|
|
|
|
|
|
|
NAME_KEY = re.compile(SENSOR_NAME_KEY)
|
|
|
|
UNIT_KEY = re.compile(SENSOR_UNIT_KEY)
|
|
|
|
VALUE_KEY = re.compile(SENSOR_VALUE_KEY)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_EMAIL): cv.string,
|
|
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
|
|
}
|
|
|
|
)
|
2016-09-04 16:32:12 +00:00
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, 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 = {}
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.http.register_view(
|
|
|
|
TorqueReceiveDataView(email, vehicle, sensors, add_entities)
|
|
|
|
)
|
2016-05-14 07:58:36 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class TorqueReceiveDataView(HomeAssistantView):
|
|
|
|
"""Handle data from Torque requests."""
|
|
|
|
|
|
|
|
url = API_PATH
|
2019-07-31 19:25:30 +00:00
|
|
|
name = "api:torque"
|
2015-12-19 04:55:37 +00:00
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def __init__(self, email, vehicle, sensors, add_entities):
|
2016-05-14 07:58:36 +00:00
|
|
|
"""Initialize a Torque view."""
|
|
|
|
self.email = email
|
|
|
|
self.vehicle = vehicle
|
|
|
|
self.sensors = sensors
|
2018-08-24 14:37:30 +00:00
|
|
|
self.add_entities = add_entities
|
2016-05-14 07:58:36 +00:00
|
|
|
|
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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
hass = request.app["hass"]
|
2017-05-26 20:12:17 +00:00
|
|
|
data = request.query
|
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))
|
2019-09-18 18:11:26 +00:00
|
|
|
|
|
|
|
temp_unit = data[key]
|
|
|
|
if "\\xC2\\xB0" in temp_unit:
|
2020-04-21 17:45:53 +00:00
|
|
|
temp_unit = temp_unit.replace("\\xC2\\xB0", DEGREE)
|
2019-09-18 18:11:26 +00:00
|
|
|
|
|
|
|
units[pid] = temp_unit
|
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
|
|
|
|
2021-07-15 04:44:57 +00:00
|
|
|
for pid, name in names.items():
|
2016-05-14 07:58:36 +00:00
|
|
|
if pid not in self.sensors:
|
|
|
|
self.sensors[pid] = TorqueSensor(
|
2021-07-15 04:44:57 +00:00
|
|
|
ENTITY_NAME_FORMAT.format(self.vehicle, name), units.get(pid)
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2018-08-24 14:37:30 +00:00
|
|
|
hass.async_add_job(self.add_entities, [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
|
|
|
|
|
|
|
|
2021-03-22 18:47:44 +00:00
|
|
|
class TorqueSensor(SensorEntity):
|
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
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_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
|
2021-08-11 16:57:50 +00:00
|
|
|
def native_value(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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return "mdi:car"
|
2015-12-19 04:55:37 +00:00
|
|
|
|
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
|
2020-04-01 21:19:51 +00:00
|
|
|
self.async_write_ha_state()
|