Skip to content

Commit

Permalink
* Added check for Electric Cars
Browse files Browse the repository at this point in the history
* Removed VIN from integration name
  • Loading branch information
mitch-dc committed Feb 2, 2022
1 parent 404074e commit 0ddbe0d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
27 changes: 19 additions & 8 deletions custom_components/volkswagen_we_connect_id/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from weconnect import weconnect
from weconnect.elements.control_operation import ControlOperation
from weconnect.elements.range_status import RangeStatus

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
Expand All @@ -24,18 +25,28 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up Volkswagen We Connect ID from a config entry."""

hass.data.setdefault(DOMAIN, {})
api = weconnect.WeConnect(
_we_connect = weconnect.WeConnect(
username=entry.data["username"],
password=entry.data["password"],
updateAfterLogin=False,
loginOnInit=False,
)

await hass.async_add_executor_job(api.login)
await hass.async_add_executor_job(api.update)
await hass.async_add_executor_job(_we_connect.login)
await hass.async_add_executor_job(_we_connect.update)

hass.data[DOMAIN][entry.entry_id] = api
hass.data[DOMAIN][entry.entry_id + "_vehicles"] = api.vehicles
vehicles = []

for vin, vehicle in _we_connect.vehicles.items():
car_type = _we_connect.getByAddressString(
f"/vehicles/{vin}/domains/fuelStatus/rangeStatus/carType"
)
# car_type = get_object_value(car_type)
if car_type.value == RangeStatus.CarType.ELECTRIC:
vehicles.append(vehicle)

hass.data[DOMAIN][entry.entry_id] = _we_connect
hass.data[DOMAIN][entry.entry_id + "_vehicles"] = vehicles
hass.config_entries.async_setup_platforms(entry, PLATFORMS)

@callback
Expand All @@ -51,7 +62,7 @@ async def volkswagen_id_set_climatisation(call: ServiceCall) -> None:
await hass.async_add_executor_job(
set_climatisation,
vin,
api,
_we_connect,
start_stop,
target_temperature,
)
Expand All @@ -71,7 +82,7 @@ async def volkswagen_id_set_target_soc(call: ServiceCall) -> None:
await hass.async_add_executor_job(
set_target_soc,
vin,
api,
_we_connect,
target_soc,
)
is False
Expand All @@ -87,7 +98,7 @@ async def volkswagen_id_set_ac_charge_speed(call: ServiceCall) -> None:
await hass.async_add_executor_job(
set_ac_charging_speed,
vin,
api,
_we_connect,
call.data["maximum_reduced"],
)
is False
Expand Down
8 changes: 3 additions & 5 deletions custom_components/volkswagen_we_connect_id/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,10 @@ async def async_update_data():
)

entities: list[VolkswagenIDSensor] = []
for vin, vehicle in vehicles.items():
for vehicle in vehicles:
for sensor in SENSORS:
entities.append(
VolkswagenIDSensor(
vin,
vehicle,
sensor,
we_connect,
Expand All @@ -150,7 +149,6 @@ class VolkswagenIDSensor(VolkswagenIDBaseEntity, BinarySensorEntity):

def __init__(
self,
vin,
vehicle: weconnect.Vehicle,
sensor: VolkswagenIdBinaryEntityDescription,
we_connect: weconnect.WeConnect,
Expand All @@ -162,8 +160,8 @@ def __init__(
self.entity_description = sensor
self._coordinator = coordinator
self._attr_name = f"Volkswagen ID {vehicle.nickname} {sensor.name}"
self._attr_unique_id = f"{vin}-{sensor.key}"
self._data = f"/vehicles/{vin}/domains{sensor.local_address}"
self._attr_unique_id = f"{vehicle.vin}-{sensor.key}"
self._data = f"/vehicles/{vehicle.vin}/domains{sensor.local_address}"

@property
def is_on(self) -> bool:
Expand Down
14 changes: 7 additions & 7 deletions custom_components/volkswagen_we_connect_id/button.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
vehicles = hass.data[DOMAIN][config_entry.entry_id + "_vehicles"]

entities = []
for vin, vehicle in vehicles.items(): # weConnect.vehicles.items():
entities.append(VolkswagenIDStartClimateButton(vin, vehicle, we_connect))
entities.append(VolkswagenIDToggleACChargeSpeed(vin, vehicle, we_connect))
for vehicle in vehicles: # weConnect.vehicles.items():
entities.append(VolkswagenIDStartClimateButton(vehicle, we_connect))
entities.append(VolkswagenIDToggleACChargeSpeed(vehicle, we_connect))

async_add_entities(entities)

Expand All @@ -27,10 +27,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
class VolkswagenIDStartClimateButton(ButtonEntity):
"""Button for starting climate."""

def __init__(self, vin, vehicle, we_connect) -> None:
def __init__(self, vehicle, we_connect) -> None:
"""Initialize VolkswagenID vehicle sensor."""
self._attr_name = f"Volkswagen ID {vehicle.nickname} Start Climate"
self._attr_unique_id = f"{vin}-start_climate"
self._attr_unique_id = f"{vehicle.vin}-start_climate"
self._we_connect = we_connect
self._vehicle = vehicle

Expand All @@ -42,10 +42,10 @@ def press(self) -> None:
class VolkswagenIDToggleACChargeSpeed(ButtonEntity):
"""Button for toggling the charge speed."""

def __init__(self, vin, vehicle: Vehicle, we_connect: weconnect.WeConnect) -> None:
def __init__(self, vehicle: Vehicle, we_connect: weconnect.WeConnect) -> None:
"""Initialize VolkswagenID vehicle sensor."""
self._attr_name = f"Volkswagen ID {vehicle.nickname} Toggle AC Charge Speed"
self._attr_unique_id = f"{vin}-toggle_ac_charge_speed"
self._attr_unique_id = f"{vehicle.vin}-toggle_ac_charge_speed"
self._we_connect = we_connect
self._vehicle = vehicle

Expand Down
15 changes: 6 additions & 9 deletions custom_components/volkswagen_we_connect_id/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,9 @@


async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str, Any]:
"""Validate the user input allows us to connect.
"""Validate the user input allows us to connect."""

Data has the keys from STEP_USER_DATA_SCHEMA with values provided by the user.
"""

weConnect = weconnect.WeConnect(
we_connect = weconnect.WeConnect(
username=data["username"],
password=data["password"],
updateAfterLogin=False,
Expand All @@ -39,12 +36,12 @@ async def validate_input(hass: HomeAssistant, data: dict[str, Any]) -> dict[str,

# TODO: ADD Validation on credentials

await hass.async_add_executor_job(weConnect.login)
await hass.async_add_executor_job(weConnect.update)
await hass.async_add_executor_job(we_connect.login)
await hass.async_add_executor_job(we_connect.update)

vin = next(iter(weConnect.vehicles.items()))[0]
# vin = next(iter(we_connect.vehicles.items()))[0]

return {"title": "Volkswagen We Connect ID - " + vin}
return {"title": "Volkswagen We Connect ID"}


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
Expand Down
4 changes: 1 addition & 3 deletions custom_components/volkswagen_we_connect_id/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,10 @@ async def async_update_data():
)

entities: list[VolkswagenIDSensor] = []
for vin, vehicle in vehicles.items():
for vehicle in vehicles:
for sensor in SENSORS:
entities.append(
VolkswagenIDSensor(
vin,
vehicle,
sensor,
we_connect,
Expand All @@ -175,7 +174,6 @@ class VolkswagenIDSensor(VolkswagenIDBaseEntity, SensorEntity):

def __init__(
self,
vin,
vehicle: weconnect.Vehicle,
sensor: VolkswagenIdEntityDescription,
we_connect: weconnect.WeConnect,
Expand Down

0 comments on commit 0ddbe0d

Please sign in to comment.