Remove use of bin_type in Tesla component (#30315)

* Remove use of bin_type

* Convert _unit attribute to units

* Remove unnecessary variable assignment

* Change to using util library convert
This commit is contained in:
Alan Tse 2020-01-01 03:23:29 -08:00 committed by Martin Hjelmare
parent bb2d8e3f7d
commit 272c00e81b
2 changed files with 18 additions and 25 deletions

View File

@ -8,6 +8,7 @@ from homeassistant.const import (
TEMP_FAHRENHEIT,
)
from homeassistant.helpers.entity import Entity
from homeassistant.util.distance import convert
from . import DOMAIN as TESLA_DOMAIN, TeslaDevice
@ -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)
@ -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)
@ -61,7 +62,7 @@ class TeslaSensor(TeslaDevice, Entity):
@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."""
@ -69,32 +70,24 @@ class TeslaSensor(TeslaDevice, Entity):
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

View File

@ -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)