Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match Devices to the same VIN. #208

Merged
merged 4 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion custom_components/tesla_custom/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def refresh(self) -> None:
vin = self.tesla_device.vin()

# Get all the climate parameters so we can determine what is supported by this vin.
# pylint: disable=protected-access
climate_params = self.tesla_device._controller.get_climate_params(vin=vin)
if climate_params is None or len(climate_params) == 0:
# No data available
Expand Down Expand Up @@ -203,15 +204,18 @@ async def update_climate_related_devices(self):
# We have to manually update the controller becuase changing the HVAC state only updates its state in Home assistant,
# and not the underlying cache in cliamte_parms. This does mean we talk to Tesla, but we only do so Once.

# pylint: disable=protected-access
await self.tesla_device._controller.update(
self.tesla_device._id, wake_if_asleep=False, force=True
)

vin = self.tesla_device.vin()

for c_device in CLIMATE_DEVICES:
_LOGGER.debug("Refreshing Device: %s.%s", c_device[0], c_device[1])

device = await get_device(
self.hass, self.config_entry_id, c_device[0], c_device[1]
self.hass, self.config_entry_id, c_device[0], c_device[1], vin
)
if device is not None:
class_name = device.__class__.__name__
Expand Down
7 changes: 4 additions & 3 deletions custom_components/tesla_custom/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ async def get_device(
config_entry_id: str,
device_category: str,
device_type: str,
vin: str,
):
"""Get a tesla Device for a Config Entry ID."""

entry_data = hass.data[TESLA_DOMAIN][config_entry_id]
devices = entry_data["devices"].get(device_category, [])

for device in devices:
if device.type == device_type:
if device.type == device_type and device.vin() == vin:
return device

return None
Expand Down Expand Up @@ -68,14 +69,14 @@ def enable_entity(


async def wait_for_climate(
hass: HomeAssistant, config_entry_id: str, timeout: int = 30
hass: HomeAssistant, config_entry_id: str, vin: str, timeout: int = 30
):
"""Wait for HVac.

Optional Timeout. defaults to 30 seconds
"""
climate_device = await get_device(
hass, config_entry_id, "climate", "HVAC (climate) system"
hass, config_entry_id, "climate", "HVAC (climate) system", vin
)

if climate_device is None:
Expand Down
3 changes: 2 additions & 1 deletion custom_components/tesla_custom/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ async def async_select_option(self, option: str, **kwargs):
"""Change the selected option."""
level: int = OPTIONS.index(option)

await wait_for_climate(self.hass, self.config_entry_id)
vin = self.tesla_device.vin()
await wait_for_climate(self.hass, self.config_entry_id, vin)
_LOGGER.debug("Setting %s to %s", self.name, level)
await self.tesla_device.set_seat_heat_level(level)
self.async_write_ha_state()
Expand Down
5 changes: 4 additions & 1 deletion custom_components/tesla_custom/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ class HeatedSteeringWheelSwitch(TeslaDevice, SwitchEntity):
async def async_turn_on(self, **kwargs):
"""Send the on command."""
_LOGGER.debug("Turn on Heating Steering Wheel: %s", self.name)
await wait_for_climate(self.hass, self.config_entry_id)

vin = self.tesla_device.vin()
await wait_for_climate(self.hass, self.config_entry_id, vin)

await self.tesla_device.set_steering_wheel_heat(True)
self.async_write_ha_state()

Expand Down