1
mirror of https://github.com/home-assistant/core synced 2024-09-12 15:16:21 +02:00
ha-core/homeassistant/components/torque/sensor.py

142 lines
3.8 KiB
Python
Raw Normal View History

"""Support for the Torque OBD application."""
2015-12-19 05:55:37 +01:00
import re
2016-02-19 06:27:50 +01:00
2016-09-04 18:32:12 +02:00
import voluptuous as vol
2016-05-14 09:58:36 +02:00
from homeassistant.components.http import HomeAssistantView
from homeassistant.components.sensor import PLATFORM_SCHEMA, SensorEntity
from homeassistant.const import CONF_EMAIL, CONF_NAME, DEGREE
from homeassistant.core import callback
2016-09-04 18:32:12 +02:00
import homeassistant.helpers.config_validation as cv
2019-07-31 21:25:30 +02:00
API_PATH = "/api/torque"
2015-12-19 05:55:37 +01:00
2019-07-31 21:25:30 +02:00
DEFAULT_NAME = "vehicle"
DOMAIN = "torque"
2016-09-04 18:32:12 +02:00
2019-07-31 21:25:30 +02:00
ENTITY_NAME_FORMAT = "{0} {1}"
2015-12-19 05:55:37 +01:00
2019-07-31 21:25:30 +02: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 05:55:37 +01: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 21:25:30 +02:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_EMAIL): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
2016-09-04 18:32:12 +02:00
2015-12-19 05:55:37 +01:00
def convert_pid(value):
2016-02-23 06:21:49 +01:00
"""Convert pid from hex string to integer."""
2015-12-19 05:55:37 +01:00
return int(value, 16)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Torque platform."""
2016-09-04 18:32:12 +02:00
vehicle = config.get(CONF_NAME)
email = config.get(CONF_EMAIL)
2015-12-19 05:55:37 +01:00
sensors = {}
2019-07-31 21:25:30 +02:00
hass.http.register_view(
TorqueReceiveDataView(email, vehicle, sensors, add_entities)
)
2016-05-14 09:58:36 +02:00
return True
class TorqueReceiveDataView(HomeAssistantView):
"""Handle data from Torque requests."""
url = API_PATH
2019-07-31 21:25:30 +02:00
name = "api:torque"
2015-12-19 05:55:37 +01:00
def __init__(self, email, vehicle, sensors, add_entities):
2016-05-14 09:58:36 +02:00
"""Initialize a Torque view."""
self.email = email
self.vehicle = vehicle
self.sensors = sensors
self.add_entities = add_entities
2016-05-14 09:58:36 +02:00
@callback
2016-05-14 09:58:36 +02:00
def get(self, request):
"""Handle Torque data request."""
2019-07-31 21:25:30 +02:00
hass = request.app["hass"]
data = request.query
2016-05-14 09:58:36 +02:00
if self.email is not None and self.email != data[SENSOR_EMAIL_FIELD]:
2015-12-19 05:55:37 +01: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))
names[pid] = data[key]
2015-12-19 05:55:37 +01:00
elif is_unit:
pid = convert_pid(is_unit.group(1))
temp_unit = data[key]
if "\\xC2\\xB0" in temp_unit:
temp_unit = temp_unit.replace("\\xC2\\xB0", DEGREE)
units[pid] = temp_unit
2015-12-19 05:55:37 +01:00
elif is_value:
pid = convert_pid(is_value.group(1))
2016-05-14 09:58:36 +02:00
if pid in self.sensors:
self.sensors[pid].async_on_update(data[key])
2015-12-19 05:55:37 +01:00
for pid in names:
2016-05-14 09:58:36 +02:00
if pid not in self.sensors:
self.sensors[pid] = TorqueSensor(
ENTITY_NAME_FORMAT.format(self.vehicle, names[pid]), units.get(pid)
2019-07-31 21:25:30 +02:00
)
hass.async_add_job(self.add_entities, [self.sensors[pid]])
2015-12-19 05:55:37 +01:00
return "OK!"
2015-12-19 05:55:37 +01:00
class TorqueSensor(SensorEntity):
2016-03-08 16:46:34 +01:00
"""Representation of a Torque sensor."""
2015-12-19 05:55:37 +01:00
def __init__(self, name, unit):
2016-03-08 16:46:34 +01:00
"""Initialize the sensor."""
2015-12-19 05:55:37 +01:00
self._name = name
self._unit = unit
self._state = None
@property
def name(self):
2016-03-08 16:46:34 +01:00
"""Return the name of the sensor."""
2015-12-19 05:55:37 +01:00
return self._name
@property
def unit_of_measurement(self):
2016-03-08 16:46:34 +01:00
"""Return the unit of measurement."""
2015-12-19 05:55:37 +01:00
return self._unit
@property
def state(self):
2016-03-10 08:34:38 +01:00
"""Return the state of the sensor."""
2015-12-19 05:55:37 +01:00
return self._state
@property
def icon(self):
2016-03-08 16:46:34 +01:00
"""Return the default icon of the sensor."""
2019-07-31 21:25:30 +02:00
return "mdi:car"
2015-12-19 05:55:37 +01:00
@callback
def async_on_update(self, value):
2016-02-23 06:21:49 +01:00
"""Receive an update."""
2015-12-19 05:55:37 +01:00
self._state = value
self.async_write_ha_state()