Skip to content

Commit

Permalink
Bump audiconnectpy to 1.4.7
Browse files Browse the repository at this point in the history
  • Loading branch information
cyr-ius committed Dec 28, 2023
1 parent 2ef62ee commit 641897f
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 82 deletions.
60 changes: 35 additions & 25 deletions custom_components/audiconnect/diagnostics.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
"""Diagnostics support for Audi Connect."""
from __future__ import annotations

from collections.abc import Callable
from contextlib import suppress
from typing import Any, Callable
from typing import Any

from homeassistant.components.diagnostics import async_redact_data
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -38,6 +39,12 @@
"userId",
"username",
"vin",
"firstName",
"lastName",
"dateOfBirth",
"nickname",
"placeOfBirth",
"carnetEnrollmentCountry",
}


Expand All @@ -46,7 +53,6 @@ async def async_get_config_entry_diagnostics(
) -> dict[str, Any]:
"""Return diagnostics for a config entry."""
coordinator = hass.data[DOMAIN][entry.entry_id]
services = coordinator.api.services
_datas = {}

async def diag(func: Callable[..., Any], *args: Any) -> None:
Expand All @@ -62,30 +68,34 @@ async def diag(func: Callable[..., Any], *args: Any) -> None:
_datas[i].update({func.__name__.replace("async_get_", ""): rslt})

i = 0
for k, v in coordinator.data.items():
for vehicle in coordinator.data.values():
i += 1
_datas.update({i: vars(v)})
await diag(services.async_get_vehicle_details, k)
await diag(services.async_get_vehicle, k)
await diag(services.async_get_stored_position, k)
await diag(services.async_get_destinations, k)
await diag(services.async_get_history, k)
await diag(services.async_get_vehicule_users, k)
await diag(services.async_get_charger, k)
await diag(services.async_get_tripdata, k, "cyclic")
await diag(services.async_get_tripdata, k, "longTerm")
await diag(services.async_get_tripdata, k, "shortTerm")
await diag(services.async_get_operations_list, k)
await diag(services.async_get_climater, k)
await diag(services.async_get_preheater, k)
await diag(services.async_get_climater_timer, k)
await diag(services.async_get_capabilities, k)
await diag(services.async_get_vehicle_information)
await diag(services.async_get_honkflash, k)
await diag(services.async_get_fences, k)
await diag(services.async_get_fences_config, k)
await diag(services.async_get_speed_alert, k)
await diag(services.async_get_speed_config, k)
_datas.update({i: vars(vehicle)})
await diag(vehicle.async_get_vehicle_details)
await diag(vehicle.async_get_vehicle)
await diag(vehicle.async_get_stored_position)
await diag(vehicle.async_get_destinations)
await diag(vehicle.async_get_history)
await diag(vehicle.async_get_vehicule_users)
await diag(vehicle.async_get_charger)
await diag(vehicle.async_get_tripdata, "cyclic")
await diag(vehicle.async_get_tripdata, "longTerm")
await diag(vehicle.async_get_tripdata, "shortTerm")
await diag(vehicle.async_get_operations_list)
await diag(vehicle.async_get_climater)
await diag(vehicle.async_get_preheater)
await diag(vehicle.async_get_climater_timer)
await diag(vehicle.async_get_capabilities)
await diag(vehicle.async_get_honkflash)
await diag(vehicle.async_get_personal_data)
await diag(vehicle.async_get_real_car_data)
await diag(vehicle.async_get_mbb_status)
await diag(vehicle.async_get_identity_data)
await diag(vehicle.async_get_users)
await diag(vehicle.async_get_fences)
await diag(vehicle.async_get_fences_config)
await diag(vehicle.async_get_speed_alert)
await diag(vehicle.async_get_speed_config)

return {
"entry": {
Expand Down
19 changes: 11 additions & 8 deletions custom_components/audiconnect/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
import logging

from audiconnectpy import AudiException
from homeassistant.components.lock import LockEntity

from homeassistant.components.binary_sensor import BinarySensorDeviceClass as dc
from homeassistant.components.lock import LockEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand All @@ -20,8 +21,8 @@
AudiLockDescription(
key="any_door_unlocked",
device_class=dc.LOCK,
turn_mode="async_lock",
translation_key="any_door_unlocked"
turn_mode="async_set_lock",
translation_key="any_door_unlocked",
),
)

Expand All @@ -34,7 +35,7 @@ async def async_setup_entry(

entities = []
for vin, vehicle in coordinator.data.items():
for name, data in vehicle.states.items():
for name in vehicle.states:
for description in SENSOR_TYPES:
if description.key == name:
entities.append(AudiLock(coordinator, vin, description))
Expand All @@ -54,8 +55,9 @@ async def async_lock(self):
"""Lock the car."""
try:
await getattr(
self.coordinator.api.services, self.entity_description.turn_mode
)(self.vin, True)
self.coordinator.api.vehicles.get(self.vin),
self.entity_description.turn_mode,
)(True)
await self.coordinator.async_request_refresh()
except AudiException as error:
_LOGGER.error("Error to turn on : %s", error)
Expand All @@ -64,8 +66,9 @@ async def async_unlock(self):
"""Unlock the car."""
try:
await getattr(
self.coordinator.api.services, self.entity_description.turn_mode
)(self.vin, False)
self.coordinator.api.vehicles.get(self.vin),
self.entity_description.turn_mode,
)(False)
await self.coordinator.async_request_refresh()
except AudiException as error:
_LOGGER.error("Error to turn on : %s", error)
4 changes: 2 additions & 2 deletions custom_components/audiconnect/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/cyr-ius/hass-audiconnect/issues",
"loggers": ["audiconnectpy"],
"requirements": ["audiconnectpy==1.4.5"],
"version": "1.3.2"
"requirements": ["audiconnectpy==1.4.7"],
"version": "1.3.3"
}
14 changes: 8 additions & 6 deletions custom_components/audiconnect/number.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

import logging

from homeassistant.components.number import NumberEntity, NumberDeviceClass as dc
from audiconnectpy import AudiException

from homeassistant.components.number import NumberDeviceClass as dc, NumberEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from audiconnectpy import AudiException
from .const import DOMAIN
from .entity import AudiEntity
from .helpers import AudiNumberDescription
Expand All @@ -31,7 +32,7 @@
icon="mdi:temperature-celsius",
native_unit_of_measurement="°C",
key="climatisation_target_temp",
turn_mode="async_climater_temp",
turn_mode="async_set_climater_temp",
value_fn=lambda x: round((int(x) - 2730) / 10, 1),
device_class=dc.TEMPERATURE,
native_max_value=40,
Expand All @@ -50,7 +51,7 @@ async def async_setup_entry(

entities = []
for vin, vehicle in coordinator.data.items():
for name, data in vehicle.states.items():
for name in vehicle.states:
for description in SENSOR_TYPES:
if description.key == name:
entities.append(AudiNumber(coordinator, vin, description))
Expand Down Expand Up @@ -78,8 +79,9 @@ async def async_set_native_value(self, value: float) -> None:
"""Set the text value."""
try:
await getattr(
self.coordinator.api.services, self.entity_description.turn_mode
)(self.vin, value)
self.coordinator.api.vehicles.get(self.vin),
self.entity_description.turn_mode,
)(value)
await self.coordinator.async_request_refresh()
except AudiException as error:
_LOGGER.error("Error to set value: %s", error)
18 changes: 3 additions & 15 deletions custom_components/audiconnect/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from audiconnectpy import AudiException
from .const import DOMAIN
from .entity import AudiEntity
from .helpers import AudiSelectDescription


_LOGGER = logging.getLogger(__name__)

SENSOR_TYPES: tuple[AudiSelectDescription, ...] = (
Expand All @@ -22,7 +20,7 @@
icon="mdi:air-conditioner",
turn_mode="set_heater_source",
options=["electric", "auxiliary", "automatic"],
translation_key="climatisation_heater_src"
translation_key="climatisation_heater_src",
),
)

Expand All @@ -35,7 +33,7 @@ async def async_setup_entry(

entities = []
for vin, vehicle in coordinator.data.items():
for name, data in vehicle.states.items():
for name in vehicle.states:
for description in SENSOR_TYPES:
if description.key == name:
entities.append(AudiSelect(coordinator, vin, description))
Expand All @@ -44,7 +42,7 @@ async def async_setup_entry(


class AudiSelect(AudiEntity, SelectEntity):
"""Representation of a Audi switch."""
"""Representation of a Audi select."""

@property
def current_option(self):
Expand All @@ -53,13 +51,3 @@ def current_option(self):
if value and self.entity_description.value_fn:
return self.entity_description.value_fn(value)
return value

async def async_select_option(self, option: str) -> None:
"""Change the selected option."""
try:
await getattr(
self.coordinator.api.services, self.entity_description.turn_mode
)(self.vin, option)
await self.coordinator.async_request_refresh()
except AudiException as error:
_LOGGER.error("Error to selected option: %s", error)
26 changes: 16 additions & 10 deletions custom_components/audiconnect/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import logging

from homeassistant.components.sensor import SensorEntity, SensorDeviceClass as dc
from homeassistant.components.sensor import SensorDeviceClass as dc, SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand All @@ -29,6 +29,11 @@
device_class=dc.TIMESTAMP,
translation_key="last_update_time",
),
AudiSensorDescription(
icon="mdi:ev-station",
key="charging_state",
translation_key="charging_state",
),
AudiSensorDescription(
icon="mdi:speedometer",
native_unit_of_measurement="km",
Expand Down Expand Up @@ -199,7 +204,7 @@
key="remaining_charging_time",
value_fn=lambda x: "n/a"
if int(x) == 65535
else "{r[0]:02d}:{r[1]:02d}".format(r=divmod(x, 60)),
else f"{divmod(x, 60)[0]:02d}:{divmod(x, 60)[1]:02d}",
native_unit_of_measurement="Min",
device_class=dc.DURATION,
translation_key="remaining_charging_time",
Expand All @@ -217,13 +222,14 @@
key="doors_trunk_status",
translation_key="doors_trunk_status",
),
# AudiSensorDescription(key="trip",translation_key="trip"),
# AudiSensorDescription(key="trip_short_reset",translation_key="trip_short_reset"),
# AudiSensorDescription(key="trip_short_current",translation_key="trip_short_current"),
# AudiSensorDescription(key="trip_long_reset",translation_key="trip_long_reset"),
# AudiSensorDescription(key="trip_long_current",translation_key="trip_long_current"),
# AudiSensorDescription(key="trip_cyclic_reset",translation_key="trip_cyclic_reset"),
# AudiSensorDescription(key="trip_cyclic_current",translation_key="trip_cyclic_current"),
AudiSensorDescription(key="trip", translation_key="trip"),
AudiSensorDescription(
key="support_trip_short", translation_key="support_trip_short"
),
AudiSensorDescription(key="support_trip_long", translation_key="support_trip_long"),
AudiSensorDescription(
key="support_trip_cyclic", translation_key="support_trip_cyclic"
),
)


Expand All @@ -235,7 +241,7 @@ async def async_setup_entry(

entities = []
for vin, vehicle in coordinator.data.items():
for name, data in vehicle.states.items():
for name in vehicle.states:
for description in SENSOR_TYPES:
if description.key == name:
entities.append(AudiSensor(coordinator, vin, description))
Expand Down
16 changes: 9 additions & 7 deletions custom_components/audiconnect/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ async def async_refresh_data(call: ServiceCall) -> None:
device_id = call.data.get(CONF_VIN).lower()
device = dr.async_get(hass).async_get(device_id)
vin = dict(device.identifiers).get(DOMAIN)
await coordinator.api.services.async_refresh_vehicle_data(vin)
vehicle = coordinator.api.vehicles.get(vin)
await vehicle.async_refresh_vehicle_data()
await coordinator.async_request_refresh()

async def async_turn_off_action(call: ServiceCall) -> None:
Expand All @@ -58,20 +59,21 @@ async def async_turn_on_action(call: ServiceCall) -> None:

async def async_actions(vin: str, action: str, mode: bool):
"""Execute action."""
vehicle = coordinator.api.vehicles.get(vin)
try:
match action:
case "lock":
await coordinator.api.services.async_lock(vin, mode)
await vehicle.async_set_lock(mode)
case "climater":
await coordinator.api.services.async_climater(vin, mode)
await vehicle.async_set_climater(mode)
case "charger":
await coordinator.api.services.async_charger(vin, mode)
await vehicle.async_set_charger(mode)
case "pre_heating":
await coordinator.api.services.async_pre_heating(vin, mode)
await vehicle.async_set_pre_heating(mode)
case "window_heating":
await coordinator.api.services.async_window_heating(vin, mode)
await vehicle.async_set_window_heating(mode)
case "ventilation":
await coordinator.api.services.async_ventilation(vin, mode)
await vehicle.async_set_ventilation(mode)
except AudiException as error:
_LOGGER.error(error)
else:
Expand Down
Loading

0 comments on commit 641897f

Please sign in to comment.