Skip to content

Commit fb94159

Browse files
Version 2. (#14)
* BREAKING CHANGE: Changed unique_id to be based on vin instead of alias. * BREAKING CHANGES. v2
1 parent e9156f5 commit fb94159

File tree

11 files changed

+337
-261
lines changed

11 files changed

+337
-261
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,4 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
/tests

README.md

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<p align="center">
2-
<img src="https://logoeps.com/wp-content/uploads/2011/04/toyota-logo-vector.png" alt="logo" height="200">
2+
<img src="https://brands.home-assistant.io/_/toyota/icon@2x.png" alt="logo" height="200">
33
</p>
44

55
<h2 align="center">Toyota community integration</h2>
@@ -21,16 +21,17 @@ it working but there are no promises.
2121

2222
## Features
2323

24-
Only Europe is supported right now. If you want to help to add other regions, please open an issue over at [`mytoyota`](https://github.com/DurgNomis-drol/mytoyota).
24+
Only Europe is supported right now. If you want to help add other regions, please open an issue over at [`mytoyota`](https://github.com/DurgNomis-drol/mytoyota).
2525

2626
This component will set up the following platforms:
2727

28-
| Platform | Sample sensor | Description |
29-
| ---------------- | ----------------------- | --------------------------------- |
30-
| `sensor` | `sensor.aygo` | Static data about your car |
31-
| `sensor` | `sensor.aygo_fuel_tank` | Fuel tank information |
32-
| `sensor` | `sensor.aygo_odometer` | Odometer information |
33-
| `device_tracker` | `device_tracker.aygo` | Shows you last parked information |
28+
| Platform | Sample sensor | Description |
29+
| ---------------- | ----------------------------- | --------------------------------- |
30+
| `sensor` | `sensor.aygo` | Static data about your car |
31+
| `sensor` | `sensor.aygo_odometer` | Odometer information |
32+
| `sensor` | `sensor.aygo_fuel_tank` | Fuel tank information |
33+
| `sensor` | `sensor.aygo_starter_battery` | Starter battery health |
34+
| `device_tracker` | `device_tracker.aygo` | Shows you last parked information |
3435

3536
**Sensors displaying statistical information are coming soon...**
3637

custom_components/toyota/__init__.py

+8-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from mytoyota.exceptions import ToyotaLoginError
88

99
from homeassistant.config_entries import ConfigEntry
10-
from homeassistant.const import CONF_API_TOKEN, CONF_EMAIL, CONF_PASSWORD, CONF_REGION
10+
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_REGION
1111
from homeassistant.core import Config, HomeAssistant
1212
from homeassistant.exceptions import ConfigEntryNotReady
1313
from homeassistant.helpers.update_coordinator import (
@@ -18,21 +18,20 @@
1818
from .const import (
1919
ALIAS,
2020
CONF_LOCALE,
21-
CONF_UUID,
2221
DATA_CLIENT,
2322
DATA_COORDINATOR,
2423
DETAILS,
2524
DOMAIN,
2625
HYBRID,
26+
IMAGE,
2727
MODEL,
28+
PLATFORMS,
2829
STARTUP_MESSAGE,
2930
VIN,
3031
)
3132

3233
_LOGGER = logging.getLogger(__name__)
3334

34-
PLATFORMS = ["sensor", "device_tracker"]
35-
3635
# Update sensors every 5 minutes
3736
UPDATE_INTERVAL = timedelta(seconds=300)
3837

@@ -51,26 +50,24 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
5150
email = entry.data[CONF_EMAIL]
5251
password = entry.data[CONF_PASSWORD]
5352
locale = entry.data[CONF_LOCALE]
54-
uuid = entry.data[CONF_UUID]
55-
token = entry.data[CONF_API_TOKEN]
5653
region = entry.data[CONF_REGION]
5754

5855
client = MyT(
5956
username=email,
6057
password=password,
6158
locale=locale,
62-
uuid=uuid,
6359
region=region.lower(),
64-
token=token,
6560
)
6661

62+
await client.login()
63+
6764
async def async_update_data():
6865
"""Fetch data from Toyota API."""
6966

7067
vehicles = []
7168

7269
try:
73-
vehicles = await client.gather_information()
70+
vehicles = await client.gather_all_information()
7471
except ToyotaLoginError as ex:
7572
_LOGGER.error(ex)
7673
except Exception as ex: # pylint: disable=broad-except
@@ -134,6 +131,7 @@ def __init__(self, coordinator, index):
134131
self.alias = self.coordinator.data[self.index][ALIAS]
135132
self.model = self.coordinator.data[self.index][DETAILS][MODEL]
136133
self.hybrid = self.coordinator.data[self.index][DETAILS][HYBRID]
134+
self.image = self.coordinator.data[self.index][DETAILS][IMAGE]
137135

138136
@property
139137
def device_info(self):
@@ -142,5 +140,5 @@ def device_info(self):
142140
"identifiers": {(DOMAIN, self.vin)},
143141
"name": self.alias,
144142
"model": self.model,
145-
"manufacturer": "ha_toyota",
143+
"manufacturer": DOMAIN,
146144
}

custom_components/toyota/config_flow.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,25 @@
1111
import voluptuous as vol
1212

1313
from homeassistant import config_entries
14-
from homeassistant.const import CONF_API_TOKEN, CONF_EMAIL, CONF_PASSWORD, CONF_REGION
14+
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_REGION
1515

16-
from .const import CONF_LOCALE, CONF_REGION_SUPPORTED, CONF_UUID
16+
from .const import CONF_LOCALE, CONF_REGION_SUPPORTED
1717

1818
# https://github.com/PyCQA/pylint/issues/3202
1919
from .const import DOMAIN # pylint: disable=unused-import
2020

2121
_LOGGER = logging.getLogger(__name__)
2222

23+
supported_regions = CONF_REGION_SUPPORTED
24+
2325
DATA_SCHEMA = vol.Schema(
2426
{
2527
vol.Required(CONF_EMAIL): str,
2628
vol.Required(CONF_PASSWORD): str,
2729
vol.Required(CONF_LOCALE): str,
28-
vol.Required(CONF_REGION): vol.In(CONF_REGION_SUPPORTED),
30+
vol.Required(CONF_REGION): vol.In(
31+
[region.capitalize() for region in supported_regions]
32+
),
2933
}
3034
)
3135

@@ -53,11 +57,7 @@ async def async_step_user(self, user_input=None):
5357
region=region.lower(),
5458
)
5559

56-
token = await client.get_token()
57-
uuid = client.get_uuid()
58-
59-
data = user_input
60-
data.update({CONF_API_TOKEN: token, CONF_UUID: uuid})
60+
await client.login()
6161

6262
except ToyotaLoginError as ex:
6363
errors["base"] = "invalid_auth"

custom_components/toyota/const.py

+24-22
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,47 @@
11
"""Constants for the Toyota Connected Services integration."""
22

3+
PLATFORMS = ["sensor", "device_tracker"]
4+
35
DOMAIN = "toyota"
46
NAME = "Toyota Connected Services"
57
ISSUES_URL = "https://github.com/DurgNomis-drol/ha_toyota/issues"
68

7-
DATA_CLIENT = "toyota_client"
8-
DATA_COORDINATOR = "coordinator"
9-
109
# CONF
1110
CONF_LOCALE = "locale"
12-
CONF_UUID = "uuid"
1311
CONF_REGION_SUPPORTED = [
14-
"Europe",
12+
"europe",
1513
]
1614

17-
# COORDINATOR DATA ATTRIBUTES
18-
DETAILS = "details"
19-
MODEL = "model"
15+
# DATA COORDINATOR
16+
DATA_CLIENT = "toyota_client"
17+
DATA_COORDINATOR = "coordinator"
18+
19+
# DATA COORDINATOR ATTRIBUTES
2020
ALIAS = "alias"
21-
VIN = "vin"
22-
STATUS = "status"
23-
ODOMETER = "odometer"
21+
BATTERY_HEALTH = "batteryHealth"
22+
CONNECTED_SERVICES = "connectedServices"
23+
DETAILS = "details"
24+
ENGINE = "engine"
25+
FUEL = "Fuel"
26+
FUEL_TYPE = "fuel"
27+
HYBRID = "hybrid"
28+
IMAGE = "imageUrl"
29+
LICENSE_PLATE = "licensePlate"
2430
MILEAGE = "mileage"
2531
MILEAGE_UNIT = "mileage_unit"
26-
FUEL = "Fuel"
27-
FUEL_TYPE = "fuel_type"
32+
MODEL = "modelName"
33+
ODOMETER = "odometer"
2834
PARKING = "parking"
29-
BATTERY = "battery"
30-
HVAC = "hvac"
31-
HVAC_TEMPERATURE = "InsideTemperature"
32-
HYBRID = "hybrid"
33-
LAST_UPDATED = "last_updated"
34-
IMAGE = "image"
35+
STATUS = "status"
36+
SERVICES = "servicesEnabled"
37+
VIN = "vin"
3538

3639
# ICONS
40+
ICON_BATTERY = "mdi:car-battery"
3741
ICON_CAR = "mdi:car"
3842
ICON_FUEL = "mdi:gas-station"
39-
ICON_ODOMETER = "mdi:speedometer"
40-
ICON_HVAC = "mdi:hvac"
43+
ICON_ODOMETER = "mdi:counter"
4144
ICON_PARKING = "mdi:map-marker"
42-
ICON_BATTERY = "mdi:car-battery"
4345

4446

4547
STARTUP_MESSAGE = f"""

custom_components/toyota/device_tracker.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@
55
from homeassistant.components.device_tracker.config_entry import TrackerEntity
66

77
from . import ToyotaEntity
8-
from .const import DATA_COORDINATOR, DOMAIN, ICON_CAR, PARKING, STATUS
8+
from .const import (
9+
CONNECTED_SERVICES,
10+
DATA_COORDINATOR,
11+
DOMAIN,
12+
ICON_CAR,
13+
PARKING,
14+
SERVICES,
15+
STATUS,
16+
)
917

1018
_LOGGER = logging.getLogger(__name__)
1119

1220

1321
async def async_setup_entry(hass, config_entry, async_add_devices):
1422
"""Set up the BMW ConnectedDrive tracker from config entry."""
15-
coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]
16-
1723
tracker = []
1824

19-
def check_if_enabled(service):
20-
"""Check if Toyota Connected Services is enabled for the car."""
21-
if "error" in service:
22-
return False
23-
24-
return True
25+
coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]
2526

2627
for index, _ in enumerate(coordinator.data):
27-
if check_if_enabled(coordinator.data[index][STATUS][PARKING]):
28+
if coordinator.data[index][SERVICES][CONNECTED_SERVICES]:
2829
tracker.append(ToyotaParkingTracker(coordinator, index))
2930

3031
async_add_devices(tracker, True)
@@ -46,12 +47,12 @@ def longitude(self):
4647
@property
4748
def name(self):
4849
"""Return the name of the sensor."""
49-
return f"{self.alias}"
50+
return f"{self.alias} parking location"
5051

5152
@property
5253
def unique_id(self):
5354
"""Return a unique identifier for this entity."""
54-
return f"{self.alias}/last_parked"
55+
return f"{self.vin}/parking_location"
5556

5657
@property
5758
def source_type(self):
@@ -67,3 +68,8 @@ def icon(self):
6768
def force_update(self):
6869
"""All updates do not need to be written to the state machine."""
6970
return False
71+
72+
@property
73+
def entity_picture(self):
74+
"""Return entity picture."""
75+
return self.image

custom_components/toyota/manifest.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
"documentation": "https://github.com/DurgNomis-drol/ha_toyota",
77
"issue_tracker": "https://github.com/DurgNomis-drol/ha_toyota/issues",
88
"codeowners": ["@DurgNomis-drol"],
9-
"requirements": ["mytoyota==0.1.4"],
10-
"version": "0.1.0"
9+
"requirements": ["mytoyota==0.2.1"],
10+
"version": "0.2.0"
1111
}

0 commit comments

Comments
 (0)