Skip to content

Commit

Permalink
Version 2. (#14)
Browse files Browse the repository at this point in the history
* BREAKING CHANGE: Changed unique_id to be based on vin instead of alias.

* BREAKING CHANGES. v2
  • Loading branch information
DurgNomis-drol authored Apr 1, 2021
1 parent e9156f5 commit fb94159
Show file tree
Hide file tree
Showing 11 changed files with 337 additions and 261 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ dmypy.json

# Pyre type checker
.pyre/
/tests
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<p align="center">
<img src="https://logoeps.com/wp-content/uploads/2011/04/toyota-logo-vector.png" alt="logo" height="200">
<img src="https://brands.home-assistant.io/_/toyota/icon@2x.png" alt="logo" height="200">
</p>

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

## Features

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).
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).

This component will set up the following platforms:

| Platform | Sample sensor | Description |
| ---------------- | ----------------------- | --------------------------------- |
| `sensor` | `sensor.aygo` | Static data about your car |
| `sensor` | `sensor.aygo_fuel_tank` | Fuel tank information |
| `sensor` | `sensor.aygo_odometer` | Odometer information |
| `device_tracker` | `device_tracker.aygo` | Shows you last parked information |
| Platform | Sample sensor | Description |
| ---------------- | ----------------------------- | --------------------------------- |
| `sensor` | `sensor.aygo` | Static data about your car |
| `sensor` | `sensor.aygo_odometer` | Odometer information |
| `sensor` | `sensor.aygo_fuel_tank` | Fuel tank information |
| `sensor` | `sensor.aygo_starter_battery` | Starter battery health |
| `device_tracker` | `device_tracker.aygo` | Shows you last parked information |

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

Expand Down
18 changes: 8 additions & 10 deletions custom_components/toyota/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from mytoyota.exceptions import ToyotaLoginError

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_API_TOKEN, CONF_EMAIL, CONF_PASSWORD, CONF_REGION
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_REGION
from homeassistant.core import Config, HomeAssistant
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import (
Expand All @@ -18,21 +18,20 @@
from .const import (
ALIAS,
CONF_LOCALE,
CONF_UUID,
DATA_CLIENT,
DATA_COORDINATOR,
DETAILS,
DOMAIN,
HYBRID,
IMAGE,
MODEL,
PLATFORMS,
STARTUP_MESSAGE,
VIN,
)

_LOGGER = logging.getLogger(__name__)

PLATFORMS = ["sensor", "device_tracker"]

# Update sensors every 5 minutes
UPDATE_INTERVAL = timedelta(seconds=300)

Expand All @@ -51,26 +50,24 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry):
email = entry.data[CONF_EMAIL]
password = entry.data[CONF_PASSWORD]
locale = entry.data[CONF_LOCALE]
uuid = entry.data[CONF_UUID]
token = entry.data[CONF_API_TOKEN]
region = entry.data[CONF_REGION]

client = MyT(
username=email,
password=password,
locale=locale,
uuid=uuid,
region=region.lower(),
token=token,
)

await client.login()

async def async_update_data():
"""Fetch data from Toyota API."""

vehicles = []

try:
vehicles = await client.gather_information()
vehicles = await client.gather_all_information()
except ToyotaLoginError as ex:
_LOGGER.error(ex)
except Exception as ex: # pylint: disable=broad-except
Expand Down Expand Up @@ -134,6 +131,7 @@ def __init__(self, coordinator, index):
self.alias = self.coordinator.data[self.index][ALIAS]
self.model = self.coordinator.data[self.index][DETAILS][MODEL]
self.hybrid = self.coordinator.data[self.index][DETAILS][HYBRID]
self.image = self.coordinator.data[self.index][DETAILS][IMAGE]

@property
def device_info(self):
Expand All @@ -142,5 +140,5 @@ def device_info(self):
"identifiers": {(DOMAIN, self.vin)},
"name": self.alias,
"model": self.model,
"manufacturer": "ha_toyota",
"manufacturer": DOMAIN,
}
16 changes: 8 additions & 8 deletions custom_components/toyota/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import CONF_API_TOKEN, CONF_EMAIL, CONF_PASSWORD, CONF_REGION
from homeassistant.const import CONF_EMAIL, CONF_PASSWORD, CONF_REGION

from .const import CONF_LOCALE, CONF_REGION_SUPPORTED, CONF_UUID
from .const import CONF_LOCALE, CONF_REGION_SUPPORTED

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

_LOGGER = logging.getLogger(__name__)

supported_regions = CONF_REGION_SUPPORTED

DATA_SCHEMA = vol.Schema(
{
vol.Required(CONF_EMAIL): str,
vol.Required(CONF_PASSWORD): str,
vol.Required(CONF_LOCALE): str,
vol.Required(CONF_REGION): vol.In(CONF_REGION_SUPPORTED),
vol.Required(CONF_REGION): vol.In(
[region.capitalize() for region in supported_regions]
),
}
)

Expand Down Expand Up @@ -53,11 +57,7 @@ async def async_step_user(self, user_input=None):
region=region.lower(),
)

token = await client.get_token()
uuid = client.get_uuid()

data = user_input
data.update({CONF_API_TOKEN: token, CONF_UUID: uuid})
await client.login()

except ToyotaLoginError as ex:
errors["base"] = "invalid_auth"
Expand Down
46 changes: 24 additions & 22 deletions custom_components/toyota/const.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,47 @@
"""Constants for the Toyota Connected Services integration."""

PLATFORMS = ["sensor", "device_tracker"]

DOMAIN = "toyota"
NAME = "Toyota Connected Services"
ISSUES_URL = "https://github.com/DurgNomis-drol/ha_toyota/issues"

DATA_CLIENT = "toyota_client"
DATA_COORDINATOR = "coordinator"

# CONF
CONF_LOCALE = "locale"
CONF_UUID = "uuid"
CONF_REGION_SUPPORTED = [
"Europe",
"europe",
]

# COORDINATOR DATA ATTRIBUTES
DETAILS = "details"
MODEL = "model"
# DATA COORDINATOR
DATA_CLIENT = "toyota_client"
DATA_COORDINATOR = "coordinator"

# DATA COORDINATOR ATTRIBUTES
ALIAS = "alias"
VIN = "vin"
STATUS = "status"
ODOMETER = "odometer"
BATTERY_HEALTH = "batteryHealth"
CONNECTED_SERVICES = "connectedServices"
DETAILS = "details"
ENGINE = "engine"
FUEL = "Fuel"
FUEL_TYPE = "fuel"
HYBRID = "hybrid"
IMAGE = "imageUrl"
LICENSE_PLATE = "licensePlate"
MILEAGE = "mileage"
MILEAGE_UNIT = "mileage_unit"
FUEL = "Fuel"
FUEL_TYPE = "fuel_type"
MODEL = "modelName"
ODOMETER = "odometer"
PARKING = "parking"
BATTERY = "battery"
HVAC = "hvac"
HVAC_TEMPERATURE = "InsideTemperature"
HYBRID = "hybrid"
LAST_UPDATED = "last_updated"
IMAGE = "image"
STATUS = "status"
SERVICES = "servicesEnabled"
VIN = "vin"

# ICONS
ICON_BATTERY = "mdi:car-battery"
ICON_CAR = "mdi:car"
ICON_FUEL = "mdi:gas-station"
ICON_ODOMETER = "mdi:speedometer"
ICON_HVAC = "mdi:hvac"
ICON_ODOMETER = "mdi:counter"
ICON_PARKING = "mdi:map-marker"
ICON_BATTERY = "mdi:car-battery"


STARTUP_MESSAGE = f"""
Expand Down
30 changes: 18 additions & 12 deletions custom_components/toyota/device_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@
from homeassistant.components.device_tracker.config_entry import TrackerEntity

from . import ToyotaEntity
from .const import DATA_COORDINATOR, DOMAIN, ICON_CAR, PARKING, STATUS
from .const import (
CONNECTED_SERVICES,
DATA_COORDINATOR,
DOMAIN,
ICON_CAR,
PARKING,
SERVICES,
STATUS,
)

_LOGGER = logging.getLogger(__name__)


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

tracker = []

def check_if_enabled(service):
"""Check if Toyota Connected Services is enabled for the car."""
if "error" in service:
return False

return True
coordinator = hass.data[DOMAIN][config_entry.entry_id][DATA_COORDINATOR]

for index, _ in enumerate(coordinator.data):
if check_if_enabled(coordinator.data[index][STATUS][PARKING]):
if coordinator.data[index][SERVICES][CONNECTED_SERVICES]:
tracker.append(ToyotaParkingTracker(coordinator, index))

async_add_devices(tracker, True)
Expand All @@ -46,12 +47,12 @@ def longitude(self):
@property
def name(self):
"""Return the name of the sensor."""
return f"{self.alias}"
return f"{self.alias} parking location"

@property
def unique_id(self):
"""Return a unique identifier for this entity."""
return f"{self.alias}/last_parked"
return f"{self.vin}/parking_location"

@property
def source_type(self):
Expand All @@ -67,3 +68,8 @@ def icon(self):
def force_update(self):
"""All updates do not need to be written to the state machine."""
return False

@property
def entity_picture(self):
"""Return entity picture."""
return self.image
4 changes: 2 additions & 2 deletions custom_components/toyota/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"documentation": "https://github.com/DurgNomis-drol/ha_toyota",
"issue_tracker": "https://github.com/DurgNomis-drol/ha_toyota/issues",
"codeowners": ["@DurgNomis-drol"],
"requirements": ["mytoyota==0.1.4"],
"version": "0.1.0"
"requirements": ["mytoyota==0.2.1"],
"version": "0.2.0"
}
Loading

0 comments on commit fb94159

Please sign in to comment.