mirror of
https://github.com/home-assistant/core.git
synced 2025-07-18 10:47:10 +00:00
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:
parent
bb2d8e3f7d
commit
272c00e81b
@ -8,6 +8,7 @@ from homeassistant.const import (
|
|||||||
TEMP_FAHRENHEIT,
|
TEMP_FAHRENHEIT,
|
||||||
)
|
)
|
||||||
from homeassistant.helpers.entity import Entity
|
from homeassistant.helpers.entity import Entity
|
||||||
|
from homeassistant.util.distance import convert
|
||||||
|
|
||||||
from . import DOMAIN as TESLA_DOMAIN, TeslaDevice
|
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"]
|
controller = hass.data[TESLA_DOMAIN][config_entry.entry_id]["controller"]
|
||||||
entities = []
|
entities = []
|
||||||
for device in hass.data[TESLA_DOMAIN][config_entry.entry_id]["devices"]["sensor"]:
|
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, "inside"))
|
||||||
entities.append(TeslaSensor(device, controller, config_entry, "outside"))
|
entities.append(TeslaSensor(device, controller, config_entry, "outside"))
|
||||||
elif device.bin_type in [0xA, 0xB, 0x5]:
|
else:
|
||||||
entities.append(TeslaSensor(device, controller, config_entry))
|
entities.append(TeslaSensor(device, controller, config_entry))
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
@ -38,7 +39,7 @@ class TeslaSensor(TeslaDevice, Entity):
|
|||||||
def __init__(self, tesla_device, controller, config_entry, sensor_type=None):
|
def __init__(self, tesla_device, controller, config_entry, sensor_type=None):
|
||||||
"""Initialize of the sensor."""
|
"""Initialize of the sensor."""
|
||||||
self.current_value = None
|
self.current_value = None
|
||||||
self._unit = None
|
self.units = None
|
||||||
self.last_changed_time = None
|
self.last_changed_time = None
|
||||||
self.type = sensor_type
|
self.type = sensor_type
|
||||||
super().__init__(tesla_device, controller, config_entry)
|
super().__init__(tesla_device, controller, config_entry)
|
||||||
@ -61,7 +62,7 @@ class TeslaSensor(TeslaDevice, Entity):
|
|||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
"""Return the unit_of_measurement of the device."""
|
"""Return the unit_of_measurement of the device."""
|
||||||
return self._unit
|
return self.units
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Update the state from the sensor."""
|
"""Update the state from the sensor."""
|
||||||
@ -69,32 +70,24 @@ class TeslaSensor(TeslaDevice, Entity):
|
|||||||
await super().async_update()
|
await super().async_update()
|
||||||
units = self.tesla_device.measurement
|
units = self.tesla_device.measurement
|
||||||
|
|
||||||
if self.tesla_device.bin_type == 0x4:
|
if self.tesla_device.type == "temperature sensor":
|
||||||
if self.type == "outside":
|
if self.type == "outside":
|
||||||
self.current_value = self.tesla_device.get_outside_temp()
|
self.current_value = self.tesla_device.get_outside_temp()
|
||||||
else:
|
else:
|
||||||
self.current_value = self.tesla_device.get_inside_temp()
|
self.current_value = self.tesla_device.get_inside_temp()
|
||||||
if units == "F":
|
if units == "F":
|
||||||
self._unit = TEMP_FAHRENHEIT
|
self.units = TEMP_FAHRENHEIT
|
||||||
else:
|
else:
|
||||||
self._unit = TEMP_CELSIUS
|
self.units = TEMP_CELSIUS
|
||||||
elif self.tesla_device.bin_type == 0xA or self.tesla_device.bin_type == 0xB:
|
elif self.tesla_device.type in ["range sensor", "mileage sensor"]:
|
||||||
self.current_value = self.tesla_device.get_value()
|
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
|
|
||||||
self.current_value /= 0.621371
|
|
||||||
self.current_value = round(self.current_value, 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":
|
if units == "LENGTH_MILES":
|
||||||
self._unit = LENGTH_MILES
|
self.units = LENGTH_MILES
|
||||||
else:
|
else:
|
||||||
self._unit = LENGTH_KILOMETERS
|
self.units = LENGTH_KILOMETERS
|
||||||
self.current_value /= 0.621371
|
self.current_value = round(
|
||||||
self.current_value = round(self.current_value, 2)
|
convert(self.current_value, LENGTH_MILES, LENGTH_KILOMETERS), 2
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self.current_value = self.tesla_device.get_value()
|
||||||
|
self.units = units
|
||||||
|
@ -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"]
|
controller = hass.data[TESLA_DOMAIN][config_entry.entry_id]["controller"]
|
||||||
entities = []
|
entities = []
|
||||||
for device in hass.data[TESLA_DOMAIN][config_entry.entry_id]["devices"]["switch"]:
|
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(ChargerSwitch(device, controller, config_entry))
|
||||||
entities.append(UpdateSwitch(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))
|
entities.append(RangeSwitch(device, controller, config_entry))
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user