Skip to content

Commit

Permalink
Merge pull request #124 from ad-ha/ad-ha-mg-0.9.4
Browse files Browse the repository at this point in the history
0.9.4
  • Loading branch information
ad-ha authored Feb 17, 2025
2 parents 57bca54 + 0e5edaa commit 8841066
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 46 deletions.
10 changes: 5 additions & 5 deletions custom_components/mg_saic/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
LOGGER.error("Failed to retrieve vehicle info.")
return

vin_info = coordinator.data["info"][0]
vin_info = coordinator.vin_info

# Determine if vehicle is RHD
lrd_value = None
Expand Down Expand Up @@ -241,7 +241,7 @@ def __init__(self, coordinator, entry, name, field, device_class, icon, data_typ
self._device_class = device_class
self._icon = icon
self._data_type = data_type
vin_info = self.coordinator.data["info"][0]
vin_info = self.coordinator.vin_info
self._unique_id = f"{entry.entry_id}_{vin_info.vin}_{field}_binary_sensor"

self._device_info = create_device_info(coordinator, entry.entry_id)
Expand All @@ -253,7 +253,7 @@ def unique_id(self):

@property
def name(self):
vin_info = self.coordinator.data["info"][0]
vin_info = self.coordinator.vin_info
return f"{vin_info.brandName} {vin_info.modelName} {self._name}"

@property
Expand Down Expand Up @@ -321,7 +321,7 @@ def __init__(
self._icon = icon
self._data_source = data_source
self._data_type = data_type
vin_info = self.coordinator.data["info"][0]
vin_info = self.coordinator.vin_info
self._unique_id = f"{entry.entry_id}_{vin_info.vin}_{field}_binary_sensor"

self._device_info = create_device_info(coordinator, entry.entry_id)
Expand All @@ -334,7 +334,7 @@ def unique_id(self):
@property
def name(self):
"""Return the name of the binary sensor."""
vin_info = self.coordinator.data["info"][0]
vin_info = self.coordinator.vin_info
return f"{vin_info.brandName} {vin_info.modelName} {self._name}"

@property
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mg_saic/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
LOGGER.error("Vehicle info is not available. Buttons cannot be set up.")
return

vin_info = coordinator.data["info"][0]
vin_info = coordinator.vin_info
vin = vin_info.vin

buttons = [
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mg_saic/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
LOGGER.error("Vehicle info is not available. Climate cannot be set up.")
return

vin_info = coordinator.data["info"][0]
vin_info = coordinator.vin_info
vin = vin_info.vin

climate_entity = SAICMGClimateEntity(coordinator, client, entry, vin_info, vin)
Expand Down
13 changes: 11 additions & 2 deletions custom_components/mg_saic/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ async def async_setup(self):
LOGGER.error(f"No vehicle data found for VIN: {vin}")
raise UpdateFailed("No matching vehicle data found.")

# Store the matching vehicle info
self.vin_info = vin_info

# Get vehicle series from API response
self.vehicle_series = getattr(vin_info, "series", "").upper()

Expand Down Expand Up @@ -441,9 +444,15 @@ async def _async_update_data(self):
raise UpdateFailed("Cannot proceed without vehicle info.")

vin = self.config_entry.data.get("vin")
vehicle_info = next((v for v in data["info"] if v.vin == vin), None)
filtered_info = [v for v in data["info"] if v.vin == vin]
if not filtered_info:
raise UpdateFailed(f"No data found for VIN: {vin}")

# Overwrite info with the filtered result and store it in an attribute
data["info"] = filtered_info
self.vin_info = filtered_info[0]

if not vehicle_info:
if not filtered_info:
raise UpdateFailed(f"No data found for VIN: {vin}")

# Fetch vehicle status with retries
Expand Down
4 changes: 2 additions & 2 deletions custom_components/mg_saic/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, coordinator, entry, field, name, data_type):
self._field = field
self._name = name
self._data_type = data_type
vin_info = self.coordinator.data["info"][0]
vin_info = self.coordinator.vin_info
self._unique_id = f"{entry.entry_id}_{vin_info.vin}_{field}_gps"

self._device_info = create_device_info(coordinator, entry.entry_id)
Expand All @@ -50,7 +50,7 @@ def unique_id(self):
@property
def name(self):
"""Return the name of the tracker."""
vin_info = self.coordinator.data["info"][0]
vin_info = self.coordinator.vin_info
return f"{vin_info.brandName} {vin_info.modelName} {self._name}"

@property
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mg_saic/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
LOGGER.error("Vehicle info is not available. Lock cannot be set up.")
return

vin_info = coordinator.data["info"][0]
vin_info = coordinator.vin_info
vin = vin_info.vin

lock_entities = [
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mg_saic/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/ad-ha/mg-saic-ha/issues",
"requirements": ["requests", "pycryptodome", "saic-ismart-client-ng==0.7.1"],
"version": "0.9.3"
"version": "0.9.4"
}
2 changes: 1 addition & 1 deletion custom_components/mg_saic/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
LOGGER.error("Vehicle info is not available. Numbers cannot be set up.")
return

vin_info = coordinator.data["info"][0]
vin_info = coordinator.vin_info
vin = vin_info.vin

number_entities = []
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mg_saic/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
LOGGER.error("Vehicle info is not available. Select entities cannot be set up.")
return

vin_info = coordinator.data["info"][0]
vin_info = coordinator.vin_info
vin = vin_info.vin

select_entities = [
Expand Down
Loading

0 comments on commit 8841066

Please sign in to comment.