Skip to content

Commit

Permalink
Add support for different image url (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasddn committed Dec 20, 2024
1 parent a928859 commit 845b4c7
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 23 deletions.
5 changes: 1 addition & 4 deletions custom_components/volvo_cars/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ async def async_setup_entry(hass: HomeAssistant, entry: VolvoCarsConfigEntry) ->

# Setup entities
await coordinator.async_config_entry_first_refresh()
_remove_old_entities(hass, coordinator)
await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

# Register events
Expand Down Expand Up @@ -113,6 +112,7 @@ async def async_migrate_entry(hass: HomeAssistant, entry: VolvoCarsConfigEntry)

if entry.minor_version < 2:
new_options[OPT_FUEL_CONSUMPTION_UNIT] = OPT_UNIT_LITER_PER_100KM
_remove_old_entities(hass, entry.runtime_data.coordinator)

if entry.minor_version < 3:
if CONF_ACCESS_TOKEN in new_data and "refresh_token" in new_data:
Expand Down Expand Up @@ -161,9 +161,6 @@ def _remove_old_entities(
hass: HomeAssistant, coordinator: VolvoCarsDataCoordinator
) -> None:
old_entities: tuple[tuple[Platform, str], ...] = (
#
# v0.2
#
(Platform.BINARY_SENSOR, "availability"),
(Platform.BINARY_SENSOR, "front_left_door"),
(Platform.BINARY_SENSOR, "front_right_door"),
Expand Down
3 changes: 1 addition & 2 deletions custom_components/volvo_cars/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .entity_description import VolvoCarsDescription

from .coordinator import VolvoCarsConfigEntry, VolvoCarsDataCoordinator
from .entity import VolvoCarsEntity, value_to_translation_key
from .entity_description import VolvoCarsDescription
from .volvo.models import VolvoCarsApiBaseModel, VolvoCarsValue

PARALLEL_UPDATES = 0
Expand Down
3 changes: 1 addition & 2 deletions custom_components/volvo_cars/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .entity_description import VolvoCarsDescription

from .const import ATTR_API_TIMESTAMP, ATTR_LAST_RESULT
from .coordinator import VolvoCarsConfigEntry, VolvoCarsDataCoordinator
from .entity import VolvoCarsEntity
from .entity_description import VolvoCarsDescription
from .volvo.models import VolvoApiException

PARALLEL_UPDATES = 0
Expand Down
3 changes: 1 addition & 2 deletions custom_components/volvo_cars/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from .entity_description import VolvoCarsDescription

from .const import ATTR_API_TIMESTAMP, ATTR_DIRECTION
from .coordinator import VolvoCarsConfigEntry, VolvoCarsDataCoordinator
from .entity import VolvoCarsEntity
from .entity_description import VolvoCarsDescription
from .volvo.models import VolvoCarsApiBaseModel, VolvoCarsLocation

PARALLEL_UPDATES = 0
Expand Down
45 changes: 32 additions & 13 deletions custom_components/volvo_cars/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.httpx_client import get_async_client

from .entity_description import VolvoCarsDescription

from .coordinator import VolvoCarsConfigEntry, VolvoCarsDataCoordinator
from .entity import VolvoCarsEntity
from .entity_description import VolvoCarsDescription
from .volvo.models import VolvoCarsApiBaseModel, VolvoCarsVehicle

_LOGGER = logging.getLogger(__name__)
Expand All @@ -26,6 +25,15 @@
"Sec-Fetch-User": "?1",
"User-Agent": "PostmanRuntime/7.43.0",
}
_IMAGE_ANGLE_MAP = {
"1": "right",
"3": "front",
"4": "threeQuartersFrontLeft",
"5": "threeQuartersRearLeft",
"6": "rear",
"7": "left",
}

PARALLEL_UPDATES = 0


Expand All @@ -37,15 +45,26 @@ class VolvoCarsImageDescription(VolvoCarsDescription, ImageEntityDescription):
image_url_fn: Callable[[VolvoCarsVehicle], str]


def _exterior_angle_image(exterior_url: str, angle: str) -> str:
def _exterior_angle_image_url(exterior_url: str, angle: str) -> str:
url_parts = parse.urlparse(exterior_url)
query = parse.parse_qs(url_parts.query, keep_blank_values=True)
query["angle"] = [angle]

return url_parts._replace(query=parse.urlencode(query, doseq=True)).geturl()
if url_parts.netloc.startswith("wizz"):
if new_angle := _IMAGE_ANGLE_MAP.get(angle):
current_angle = url_parts.path.split("/")[-2]
return exterior_url.replace(current_angle, new_angle)

return ""
else:
query = parse.parse_qs(url_parts.query, keep_blank_values=True)
query["angle"] = [angle]

return url_parts._replace(query=parse.urlencode(query, doseq=True)).geturl()


async def _async_image_exists(client: AsyncClient, url: str) -> bool:
if not url:
return False

try:
response = await client.get(url, timeout=10, follow_redirects=True)
response.raise_for_status()
Expand All @@ -66,49 +85,49 @@ async def _async_image_exists(client: AsyncClient, url: str) -> bool:
VolvoCarsImageDescription(
key="exterior_back",
translation_key="exterior_back",
image_url_fn=lambda vehicle: _exterior_angle_image(
image_url_fn=lambda vehicle: _exterior_angle_image_url(
vehicle.images.exterior_image_url, "6"
),
),
VolvoCarsImageDescription(
key="exterior_back_driver",
translation_key="exterior_back_driver",
image_url_fn=lambda vehicle: _exterior_angle_image(
image_url_fn=lambda vehicle: _exterior_angle_image_url(
vehicle.images.exterior_image_url, "5"
),
),
VolvoCarsImageDescription(
key="exterior_back_passenger",
translation_key="exterior_back_passenger",
image_url_fn=lambda vehicle: _exterior_angle_image(
image_url_fn=lambda vehicle: _exterior_angle_image_url(
vehicle.images.exterior_image_url, "2"
),
),
VolvoCarsImageDescription(
key="exterior_front",
translation_key="exterior_front",
image_url_fn=lambda vehicle: _exterior_angle_image(
image_url_fn=lambda vehicle: _exterior_angle_image_url(
vehicle.images.exterior_image_url, "3"
),
),
VolvoCarsImageDescription(
key="exterior_front_driver",
translation_key="exterior_front_driver",
image_url_fn=lambda vehicle: _exterior_angle_image(
image_url_fn=lambda vehicle: _exterior_angle_image_url(
vehicle.images.exterior_image_url, "4"
),
),
VolvoCarsImageDescription(
key="exterior_front_passenger",
translation_key="exterior_front_passenger",
image_url_fn=lambda vehicle: _exterior_angle_image(
image_url_fn=lambda vehicle: _exterior_angle_image_url(
vehicle.images.exterior_image_url, "0"
),
),
VolvoCarsImageDescription(
key="exterior_side_driver",
translation_key="exterior_side_driver",
image_url_fn=lambda vehicle: _exterior_angle_image(
image_url_fn=lambda vehicle: _exterior_angle_image_url(
vehicle.images.exterior_image_url, "7"
),
),
Expand Down

0 comments on commit 845b4c7

Please sign in to comment.