Skip to content

Commit

Permalink
Remove use of bin_type in Tesla component (home-assistant#30315)
Browse files Browse the repository at this point in the history
* Remove use of bin_type

* Convert _unit attribute to units

* Remove unnecessary variable assignment

* Change to using util library convert
  • Loading branch information
alandtse authored and MartinHjelmare committed Jan 1, 2020
1 parent bb2d8e3 commit 272c00e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 25 deletions.
39 changes: 16 additions & 23 deletions homeassistant/components/tesla/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
TEMP_FAHRENHEIT,
)
from homeassistant.helpers.entity import Entity
from homeassistant.util.distance import convert

from . import DOMAIN as TESLA_DOMAIN, TeslaDevice

Expand All @@ -24,10 +25,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
controller = hass.data[TESLA_DOMAIN][config_entry.entry_id]["controller"]
entities = []
for device in hass.data[TESLA_DOMAIN][config_entry.entry_id]["devices"]["sensor"]:
if device.bin_type == 0x4:
if device.type == "temperature sensor":
entities.append(TeslaSensor(device, controller, config_entry, "inside"))
entities.append(TeslaSensor(device, controller, config_entry, "outside"))
elif device.bin_type in [0xA, 0xB, 0x5]:
else:
entities.append(TeslaSensor(device, controller, config_entry))
async_add_entities(entities, True)

Expand All @@ -38,7 +39,7 @@ class TeslaSensor(TeslaDevice, Entity):
def __init__(self, tesla_device, controller, config_entry, sensor_type=None):
"""Initialize of the sensor."""
self.current_value = None
self._unit = None
self.units = None
self.last_changed_time = None
self.type = sensor_type
super().__init__(tesla_device, controller, config_entry)
Expand All @@ -61,40 +62,32 @@ def state(self):
@property
def unit_of_measurement(self):
"""Return the unit_of_measurement of the device."""
return self._unit
return self.units

async def async_update(self):
"""Update the state from the sensor."""
_LOGGER.debug("Updating sensor: %s", self._name)
await super().async_update()
units = self.tesla_device.measurement

if self.tesla_device.bin_type == 0x4:
if self.tesla_device.type == "temperature sensor":
if self.type == "outside":
self.current_value = self.tesla_device.get_outside_temp()
else:
self.current_value = self.tesla_device.get_inside_temp()
if units == "F":
self._unit = TEMP_FAHRENHEIT
self.units = TEMP_FAHRENHEIT
else:
self._unit = TEMP_CELSIUS
elif self.tesla_device.bin_type == 0xA or self.tesla_device.bin_type == 0xB:
self.units = TEMP_CELSIUS
elif self.tesla_device.type in ["range sensor", "mileage sensor"]:
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
if units == "LENGTH_MILES":
self.units = LENGTH_MILES
else:
self._unit = LENGTH_KILOMETERS
self.current_value /= 0.621371
self.current_value = round(self.current_value, 2)
self.units = LENGTH_KILOMETERS
self.current_value = round(
convert(self.current_value, LENGTH_MILES, LENGTH_KILOMETERS), 2
)
else:
self.current_value = self.tesla_device.get_value()
if self.tesla_device.bin_type == 0x5:
self._unit = units
elif self.tesla_device.bin_type in (0xA, 0xB):
if units == "LENGTH_MILES":
self._unit = LENGTH_MILES
else:
self._unit = LENGTH_KILOMETERS
self.current_value /= 0.621371
self.current_value = round(self.current_value, 2)
self.units = units
4 changes: 2 additions & 2 deletions homeassistant/components/tesla/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
controller = hass.data[TESLA_DOMAIN][config_entry.entry_id]["controller"]
entities = []
for device in hass.data[TESLA_DOMAIN][config_entry.entry_id]["devices"]["switch"]:
if device.bin_type == 0x8:
if device.type == "charger switch":
entities.append(ChargerSwitch(device, controller, config_entry))
entities.append(UpdateSwitch(device, controller, config_entry))
elif device.bin_type == 0x9:
elif device.type == "maxrange switch":
entities.append(RangeSwitch(device, controller, config_entry))
async_add_entities(entities, True)

Expand Down

0 comments on commit 272c00e

Please sign in to comment.