From 36e9f523d1b22dbad6b01d76760eb2c7cd48eb87 Mon Sep 17 00:00:00 2001 From: Alan Tse Date: Sun, 4 Mar 2018 02:35:38 -0800 Subject: [PATCH] Adding additional switches and sensors for Tesla (#12241) * Adding switch for maxrange charging and sensors for odometer and range * Fixing style errors --- .../components/device_tracker/tesla.py | 23 +++++----- homeassistant/components/sensor/tesla.py | 8 ++++ homeassistant/components/switch/tesla.py | 43 +++++++++++++++++-- 3 files changed, 60 insertions(+), 14 deletions(-) diff --git a/homeassistant/components/device_tracker/tesla.py b/homeassistant/components/device_tracker/tesla.py index 4945e98a94d..ba9bc8c2631 100644 --- a/homeassistant/components/device_tracker/tesla.py +++ b/homeassistant/components/device_tracker/tesla.py @@ -44,14 +44,15 @@ class TeslaDeviceTracker(object): _LOGGER.debug("Updating device position: %s", name) dev_id = slugify(device.uniq_name) location = device.get_location() - lat = location['latitude'] - lon = location['longitude'] - attrs = { - 'trackr_id': dev_id, - 'id': dev_id, - 'name': name - } - self.see( - dev_id=dev_id, host_name=name, - gps=(lat, lon), attributes=attrs - ) + if location: + lat = location['latitude'] + lon = location['longitude'] + attrs = { + 'trackr_id': dev_id, + 'id': dev_id, + 'name': name + } + self.see( + dev_id=dev_id, host_name=name, + gps=(lat, lon), attributes=attrs + ) diff --git a/homeassistant/components/sensor/tesla.py b/homeassistant/components/sensor/tesla.py index 74e74262710..1ffc97bb137 100644 --- a/homeassistant/components/sensor/tesla.py +++ b/homeassistant/components/sensor/tesla.py @@ -78,6 +78,14 @@ class TeslaSensor(TeslaDevice, Entity): self._unit = TEMP_FAHRENHEIT else: self._unit = TEMP_CELSIUS + elif (self.tesla_device.bin_type == 0xA or + self.tesla_device.bin_type == 0xB): + self.current_value = self.tesla_device.get_value() + tesla_dist_unit = self.tesla_device.measurement + if tesla_dist_unit == 'LENGTH_MILES': + self._unit = LENGTH_MILES + else: + self._unit = LENGTH_KILOMETERS else: self.current_value = self.tesla_device.get_value() if self.tesla_device.bin_type == 0x5: diff --git a/homeassistant/components/switch/tesla.py b/homeassistant/components/switch/tesla.py index 2f105a709ad..0e1b7e819f7 100644 --- a/homeassistant/components/switch/tesla.py +++ b/homeassistant/components/switch/tesla.py @@ -17,8 +17,13 @@ DEPENDENCIES = ['tesla'] def setup_platform(hass, config, add_devices, discovery_info=None): """Set up the Tesla switch platform.""" - devices = [ChargerSwitch(device, hass.data[TESLA_DOMAIN]['controller']) - for device in hass.data[TESLA_DOMAIN]['devices']['switch']] + controller = hass.data[TESLA_DOMAIN]['devices']['controller'] + devices = [] + for device in hass.data[TESLA_DOMAIN]['devices']['switch']: + if device.bin_type == 0x8: + devices.append(ChargerSwitch(device, controller)) + elif device.bin_type == 0x9: + devices.append(RangeSwitch(device, controller)) add_devices(devices, True) @@ -38,7 +43,7 @@ class ChargerSwitch(TeslaDevice, SwitchDevice): def turn_off(self, **kwargs): """Send the off command.""" - _LOGGER.debug("Disable charging for: %s", self._name) + _LOGGER.debug("Disable charging for: %s", self._name) self.tesla_device.stop_charge() @property @@ -52,3 +57,35 @@ class ChargerSwitch(TeslaDevice, SwitchDevice): self.tesla_device.update() self._state = STATE_ON if self.tesla_device.is_charging() \ else STATE_OFF + + +class RangeSwitch(TeslaDevice, SwitchDevice): + """Representation of a Tesla max range charging switch.""" + + def __init__(self, tesla_device, controller): + """Initialise of the switch.""" + self._state = None + super().__init__(tesla_device, controller) + self.entity_id = ENTITY_ID_FORMAT.format(self.tesla_id) + + def turn_on(self, **kwargs): + """Send the on command.""" + _LOGGER.debug("Enable max range charging: %s", self._name) + self.tesla_device.set_max() + + def turn_off(self, **kwargs): + """Send the off command.""" + _LOGGER.debug("Disable max range charging: %s", self._name) + self.tesla_device.set_standard() + + @property + def is_on(self): + """Get whether the switch is in on state.""" + return self._state == STATE_ON + + def update(self): + """Update the state of the switch.""" + _LOGGER.debug("Updating state for: %s", self._name) + self.tesla_device.update() + self._state = STATE_ON if self.tesla_device.is_maxrange() \ + else STATE_OFF