Skip to content

Commit

Permalink
Merge master
Browse files Browse the repository at this point in the history
  • Loading branch information
CM000n committed Nov 9, 2023
2 parents b22dd35 + 9defcab commit 1a9ff89
Show file tree
Hide file tree
Showing 25 changed files with 1,669 additions and 1,455 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
name: Pre-commit
strategy:
matrix:
python-version: [3.8, 3.9]
python-version: [3.11]
runs-on: ubuntu-latest
steps:
- name: Check out the repository
Expand All @@ -23,9 +23,7 @@ jobs:
python-version: ${{ matrix.python-version }}

- name: Install Poetry
uses: abatilo/[email protected]
with:
poetry-version: 1.1.5
uses: abatilo/[email protected]

- name: Install dependencies
run: poetry install
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ jobs:
name: HACS
steps:
- name: Check out the repository
uses: "actions/[email protected]"

uses: "actions/checkout@v3"
- name: HACS validation
uses: "hacs/action@21.2.2"
uses: "hacs/action@main"
with:
category: "integration"

Expand All @@ -26,7 +25,6 @@ jobs:
name: Hassfest
steps:
- name: Check out the repository
uses: "actions/[email protected]"

uses: "actions/checkout@v3"
- name: Hassfest validation
uses: "home-assistant/actions/hassfest@master"
17 changes: 3 additions & 14 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,9 @@ repos:
language: system
types: [python]
require_serial: true
- id: isort
name: isort
entry: poetry run isort
language: system
types: [python]
require_serial: true
- id: flake8
name: flake8
entry: poetry run flake8
language: system
types: [python]
- id: pylint
name: pylint
entry: poetry run pylint
- id: ruff
name: ruff
entry: poetry run ruff
language: system
types: [python]
- repo: https://github.com/pre-commit/mirrors-prettier
Expand Down
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
[![GitHub Activity][commits-shield]][commits]
[![Installs][installs-shield]][installs]

# IMPORTANT! --> This only works with old Myt by Toyota app. If you have moved to the new app this wont work for you unfortunatly.

<p align="center">
<img src="https://brands.home-assistant.io/_/toyota/[email protected]" alt="logo" height="200">
</p>

<h2 align="center">Toyota community integration</h2>
<h2 align="center">Toyota EU community integration</h2>

<p align="center">
This custom integration aims to provide plug-and-play integration for your Toyota vehicle.
Expand All @@ -17,13 +19,14 @@
## About

This is a custom integration the retrieves' data from the
Toyota MyT API and makes them available in Home Assistant as different types of sensors.
Toyota EU MyT API and makes them available in Home Assistant as different types of sensors.
As there is no official API from Toyota, I will try my best to keep
it working, but there are no promises.

## Features

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).
Only Europe is supported.
See [here](https://github.com/widewing/ha-toyota-na) for North America.

**Disclaimer: Features available depends on your car model and year.**

Expand Down Expand Up @@ -60,7 +63,8 @@ Only Europe is supported right now. If you want to help add other regions, pleas
| <div style="width:250px">Name</div> | Description |
| ------------------------------------ | ------------------------------------------------------------------------ |
| `sensor.corolla` | Static data about your car. |
| `sensor.corolla_hvac` | EV battery information |
| `sensor.corolla_ev_battery_status` | EV battery information |
| `sensor.corolla_ev_remaining_charge` | EV battery remaining charge (in per cent of full capacity) |
| `sensor.corolla_fuel_tank` | Fuel tank information. |
| `sensor.corolla_hvac` | HVAC sensor showing current and target temperature, including other data |
| `sensor.corolla_odometer` | Odometer information. |
Expand Down Expand Up @@ -183,7 +187,7 @@ Under the hood this integration uses the [mytoyota](https://github.com/DurgNomis
[hacsbadge]: https://img.shields.io/badge/HACS-Default-orange.svg?style=for-the-badge
[releases-shield]: https://img.shields.io/github/release/DurgNomis-drol/ha_toyota.svg?style=for-the-badge
[releases]: https://github.com/DurgNomis-drol/ha_toyota/releases
[workflow-shield]: https://img.shields.io/github/workflow/status/DurgNomis-drol/ha_toyota/Linting?style=for-the-badge
[workflow-shield]: https://img.shields.io/github/actions/workflow/status/DurgNomis-drol/ha_toyota/linting.yml?branch=master&style=for-the-badge
[workflow]: https://github.com/DurgNomis-drol/ha_toyota/actions
[installs-shield]: https://img.shields.io/endpoint?style=for-the-badge&url=https%3A%2F%2Ftoyota-installs-for-shield-io-b910bxm1lt58.runkit.sh%2F
[installs]: https://analytics.home-assistant.io/custom_integrations.json
Expand Down
40 changes: 33 additions & 7 deletions custom_components/toyota/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,26 @@

import asyncio
import asyncio.exceptions as asyncioexceptions
from datetime import timedelta
import logging
from typing import Any, TypedDict
from datetime import timedelta
from typing import Any, Coroutine, Dict, List, TypedDict

import async_timeout
import httpcore
import httpx
from mytoyota import MyT
from mytoyota.exceptions import ToyotaApiError, ToyotaInternalError, ToyotaLoginError
from mytoyota.models.vehicle import Vehicle

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import (
CONF_EMAIL,
CONF_PASSWORD,
CONF_UNIT_SYSTEM_IMPERIAL,
CONF_UNIT_SYSTEM_METRIC,
LENGTH_MILES,
)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from mytoyota.client import MyT, Vehicle
from mytoyota.exceptions import ToyotaApiError, ToyotaInternalError, ToyotaLoginError

from .const import (
CONF_UNIT_SYSTEM_IMPERIAL_LITERS,
Expand Down Expand Up @@ -51,12 +50,36 @@ class VehicleData(TypedDict):
statistics: StatisticsData | None


async def with_timeout(task, timeout_seconds=15):
async def with_timeout(
task: Coroutine[Any, Any, List[Dict[str, Any]]], timeout_seconds: int = 25
) -> List[Dict[str, Any]]:
"""Run an async task with a timeout."""
async with async_timeout.timeout(timeout_seconds):
return await task


def _get_unit_system_from_odometer(vehicle: Vehicle, use_liters: bool) -> str:
if vehicle.odometer is not None:
if vehicle.odometer.unit == LENGTH_MILES:
_LOGGER.debug("The car is reporting data in imperial")
if use_liters:
_LOGGER.debug("Get statistics in imperial and L/100 miles")
unit = CONF_UNIT_SYSTEM_IMPERIAL_LITERS
else:
_LOGGER.debug("Get statistics in imperial and MPG")
unit = CONF_UNIT_SYSTEM_IMPERIAL
else:
_LOGGER.debug("The car is reporting data in metric")
unit = CONF_UNIT_SYSTEM_METRIC
else:
_LOGGER.debug(
"Could not get any 'odometer' information. \
Falling back to metric data"
)
unit = CONF_UNIT_SYSTEM_METRIC
return unit


async def async_setup_entry( # pylint: disable=too-many-statements
hass: HomeAssistant, entry: ConfigEntry
) -> bool:
Expand Down Expand Up @@ -94,6 +117,9 @@ async def async_get_vehicle_data() -> list[VehicleData]:
cars = await with_timeout(client.get_vehicles())

for car in cars:
if not car["vin"]:
continue

vehicle = await client.get_vehicle_status(car)

car = VehicleData(data=vehicle, statistics=None)
Expand Down
Loading

0 comments on commit 1a9ff89

Please sign in to comment.