Skip to content

Commit

Permalink
feat!: rewrite to add support for energy sites (#250)
Browse files Browse the repository at this point in the history
Added support for Tesla energy sites and updates to cars. 

- Move car specific code out of `TeslaBaseEntity` to a newly created `TeslaCarEntity`.
- New `TeslaEnergyEntity` class which also inherits from `TeslaBaseEntity`.
- New `TeslaEnergyPowerSensor` class used for creating power sensors (solar, grid, load and battery).
- New `TeslaEnergyBattery` class for Powerwall battery percentage sensor.
- New `TeslaEnergyBatteryRemaining` class for Powerewall battery Watt hour remaining sensor.
- New `TeslaEnergyBackupReserve` class for Powerwall backup reserve percentage setting sensor.
- New `TeslaEnergyBatteryCharging` class for Powerwall battery charging binary sensor.
- New `TeslaEnergyGridStatus` class for Powerwall grid status binary sensor.
- New `TeslaEnergyGridCharging` class for Powerwall grid charging select.
- New `TeslaEnergyExportRule` class for Powerwall export rule select.
- New `TeslaEnergyOperationMode` class for Powerwall operation mode select.
- New `TeslaCarChargerPower` class for car charger power (kW).
- Updated properties and method calls to teslajsonpy to reflect the changes made to teslajsonpy.
- Added additional checks to only add entities for what a car actually has (seat heaters, heated steering wheel, HomeLink, etc.)
- Updated naming to align with [current](https://developers.home-assistant.io/docs/core/entity#entity-naming) Home Assistant convention.
- Changed unique IDs to use VIN + entity type for vehicles.
- Update class naming to `TeslaCar*` and `TeslaEnergy*` for all vehicle and energy site related classes respectively.
- Renamed `TeslaCarMileage` to `TeslaCarOdometer`.
- `TeslaCarParkingBrake` will indicate "On" when the car is on and in "Park" but also when the car is off (parking brake still engaged).
- Updated icons to better reflect entities purpose.
- Added a check to the car energy added sensor to report 0 unless the car is charging, then report the actual charge energy added. This is due to the Tesla API reporting a decreasing value over time when not charging causing issues with the HA sensor state class "TOTAL_INCREASING".
- Removed extra state attributes that are already a separate entity (charge energy added, charge current request).
- Moved charger actual current, charger voltage and charger power extra state attributes from the charging rate to energy added entity.
- Updated `TeslaCarChargerConnection` device class to `PLUG`.
- Removed unused `helpers.py`.
- Cast vehicle ID and ID to a string in `TeslaCarOnline` extra state attributes to prevent HA from automatically formatting with commas.
- Check users HA unit system locale settings for determining to return miles vs kilometers.
- Vehicles will be forced to wake up the first time the integration is set up. For subsequent integration or HA restarts, vehicles will not be forced to wake up unless enabled in the configuration options (defaults to off).
- Added two new options, "Include Vehicles" and "Include Energy Sites" to the setup config flow. These will allow a user to include/exclude vehicles or energy sites. Defaults selected.
- Updated doc strings.
- Updated and added tests to cover changes.

- closes #79 
- closes #93
- closes #101
- closes #173
- closes #204
- closes #222 
- closes #226
- closes #271

BREAKING CHANGE: This is a rewrite. Multiple entitiy_ids were changed. It is recommended you remove and reinstall.
Changed trunk, frunk and charger door from lock to cover entities.
Created separate sensor (`TeslaCarChargerPower` class) for charger power.
Moved charger amps, charger volts and charger phases extra state attributes to the new charger power sensor.
Moved added range extra state attributes to the charger energy added sensor.
  • Loading branch information
shred86 authored Oct 16, 2022
1 parent 9ac4d8d commit 6a1f9c3
Show file tree
Hide file tree
Showing 52 changed files with 4,601 additions and 2,021 deletions.
2 changes: 1 addition & 1 deletion .devcontainer/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ logger:
authcaptureproxy: debug
teslajsonpy: debug
# If you need to debug uncommment the line below (doc: https://www.home-assistant.io/integrations/debugpy/)
debugpy:
# debugpy:
11 changes: 6 additions & 5 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"image": "ludeeus/container:integration-debian",
"name": "Tesla Custom Component development",
"context": "..",
"appPort": ["9123:8123"],
"postCreateCommand": "container install && pip install poetry && poetry config virtualenvs.create false && poetry install",
"image": "ghcr.io/ludeeus/devcontainer/integration:stable",
"appPort": [
"9123:8123"
],
"postCreateCommand": "bash .devcontainer/post-create.sh",
"extensions": [
"ms-python.python",
"github.vscode-pull-request-github",
Expand All @@ -29,4 +30,4 @@
"editor.formatOnType": true,
"files.trimTrailingWhitespace": true
}
}
}
35 changes: 35 additions & 0 deletions .devcontainer/post-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash

source /opt/container/helpers/common/paths.sh
mkdir -p /config

# Required to get automations to work
echo "Creating automations.yaml"
touch /config/automations.yaml

# source: /opt/container/helpers/commons/homeassistant/start.sh
if test -d "custom_components"; then
echo "Symlink the custom component directory"

if test -d "custom_components"; then
rm -R /config/custom_components
fi

ln -sf "$(workspacePath)custom_components/" /config/custom_components || echo "Could not copy the custom_component" exit 1
elif test -f "__init__.py"; then
echo "Having the component in the root is currently not supported"
fi

# Install
echo "Install home assistant"
container install

# Setup the Dev Stuff

pip install poetry
# We're in Docker, so we don't need a VENV
poetry config virtualenvs.create false
poetry install --no-interaction

# Keep this inline with any requirements that are in manifest.json
pip install git+https://github.com/zabuldon/teslajsonpy.git@dev#teslajsonpy==2.2.0
4 changes: 2 additions & 2 deletions .github/workflows/pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ jobs:
steps:
- name: Check out code from GitHub
uses: "actions/checkout@v2"
- name: Set up Python 3.9
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: 3.9
python-version: "3.10"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ jobs:
steps:
- name: Check out code from GitHub
uses: "actions/checkout@v2"
- name: Set up Python 3.9
- name: Set up Python 3.10
uses: actions/setup-python@v2
with:
python-version: 3.9
python-version: "3.10"
- name: Install Poetry
uses: snok/install-poetry@v1
with:
Expand Down
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ hs_err_pid*
*.jl.mem
deps/deps.jl

# User settings
/.vscode/settings.json


# Keep these config files
!/.gitignore
!/.travis.yml
Expand All @@ -72,3 +76,4 @@ deps/deps.jl
!/.scrutinizer.yml
!/.prospector.yml
!/.devcontainer
!/.vscode
35 changes: 35 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
// Example of attaching to local debug server
"name": "Python: Attach Local",
"type": "python",
"request": "attach",
"port": 5678,
"justMyCode": false,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
]
},
{
// Example of attaching to my production server
"name": "Python: Attach Remote",
"type": "python",
"request": "attach",
"port": 5678,
"host": "homeassistant.local",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/usr/src/homeassistant"
}
]
}
]
}
10 changes: 10 additions & 0 deletions .vscode/settings.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"files.associations": {
"*.yaml": "home-assistant"
}
}
35 changes: 35 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "Run Home Assistant on port 9123",
"type": "shell",
"command": "container start",
"problemMatcher": []
},
{
"label": "Run Home Assistant configuration against /config",
"type": "shell",
"command": "container check",
"problemMatcher": []
},
{
"label": "Upgrade Home Assistant to latest dev",
"type": "shell",
"command": "container install",
"problemMatcher": []
},
{
"label": "Install a specific version of Home Assistant",
"type": "shell",
"command": "container set-version",
"problemMatcher": []
},
{
"label": "Serve Documentation on port 8000",
"type": "shell",
"command": "mkdocs -v serve",
"problemMatcher": []
}
]
}
44 changes: 22 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
[![Discord][discord-shield]][discord]
[![Community Forum][forum-shield]][forum]

A fork of the [official Tesla integration](https://www.home-assistant.io/integrations/tesla/) in Home Assistant.

This is the successor to the core app which was removed due to Tesla login issues. Do not report issues to Home Assistant.
A fork of the previous official Tesla integration in Home Assistant which has been removed due to Tesla login issues. Do not report issues to Home Assistant.

To use the component, you will need an application to generate a Tesla refresh token:

Expand All @@ -36,32 +34,36 @@ To use the component, you will need an application to generate a Tesla refresh t
7. Restart Home Assistant.
8. [![Add Integration][add-integration-badge]][add-integration] or in the HA UI go to "Configuration" -> "Integrations" click "+" and search for "Tesla Custom Integration".

Note: This integration will wake up your vehicle(s) during installation.

<!---->

## Usage

The `Tesla` integration offers integration with the [Tesla](https://auth.tesla.com/login) cloud service and provides presence detection as well as sensors such as charger state and temperature.

This integration provides the following platforms:
This integration provides the following entities for vehicles:

- Binary sensors - charger connection, charging status, car online and parking brake.
- Buttons - horn, flash lights, wake up<sup>1</sup>, force data update<sup>1</sup> and trigger HomeLink. **Note:** The HomeLink button is disabled by default as some vehicles don't have this option. Enable via configuration/entities if desired.
- Climate - turn HVAC on/off, set target temperature, set preset modes (defrost, keep on, dog mode and camp mode).
- Device tracker - car location<sup>1</sup>.
- Locks - door lock, rear trunk lock, front trunk (frunk) lock and charger door lock.
**Note:** Set `state` to `heat_cool` or `off` to enable/disable your Tesla's climate system via a scene.
- Selects - seat heaters and cabin overheat protection<sup>2</sup>.
- Sensors - battery level, charge rate, energy added, inside/outside temperature, odometer and estimated range.
- Switches - heated steering wheel, charger, sentry mode and polling<sup>1</sup>.
- Update - software update<sup>2</sup>

- Binary sensors - such as update available, parking, and charger connection.
- Sensors - such as Battery level, Inside/Outside temperature, odometer, estimated range, charging rate, and vehicle data
- Device tracker - to track location of your car
- Locks - Door lock, rear trunk lock, front trunk (frunk) lock and charger door lock. Enables you to control Tesla's door, trunks and charger door lock.
- Climate - HVAC control. Allow you to control (turn on/off, set target temperature) your Tesla's HVAC system. Also enables preset modes to enable or disable max defrost mode `defrost` or `normal` operation mode. **NOTE:** Set `state` to `heat_cool` or `off` to enable/disable your Tesla's climate system via a scene.
- Switches - Charger and max range switch allow you to start/stop charging and set max range charging. Polling switch allows you to disable polling of vehicles to conserve battery. Sentry mode switch enables or disable Sentry mode.
- Buttons - Horn, Flash lights, and Trigger homelink. **Note:** The homelink button is disabled by default as many vehicles don't have the homelink option. Enable via configuration/entities if desired.
<sup>1</sup> *Diagnostics entities.*<br/>
<sup>2</sup> *Configuration entities.*

The following sensors provide all available vehicle data as attributes. These sensors are disabled by default and need to be enabled in HASS first. It is also recommended to exclude these sensors from [recorder](https://www.home-assistant.io/integrations/recorder/).

- Climate data sensor
- Charge data sensor
- Vehicle state data sensor
- Software update data sensor
- Speed Limit data sensor
- Vehicle Config data sensor
- Drive State data sensor
- GUI Settings data sensor
This integration provides the following entities for energy sites:

- Binary sensors - Powerwall charging and grid status.
- Selects - grid charging, export rule and operation mode.
- Sensors - solar power, grid power, load power, battery level, battery Wh remaining and backup reserve.

This integration provies the following platforms for solar systems:

Expand All @@ -72,9 +74,7 @@ This integration provies the following platforms for solar systems:
Tesla options are set via **Configuration** -> **Integrations** -> **Tesla** -> **Options**.

- Seconds between polling - referred to below as the `polling_interval`.

- Wake cars on start - Whether to wake sleeping cars on Home Assistant startup. This allows a user to choose whether cars should continue to sleep (and not update information) or to wake up the cars potentially interrupting long term hibernation and increasing vampire drain.

- Polling policy - When do we actively poll the car to get updates, and when do we try to allow the car to sleep. See [the Wiki](https://github.com/alandtse/tesla/wiki/Polling-policy) for more information.

## Potential Battery impacts
Expand Down
Loading

1 comment on commit 6a1f9c3

@purcell-lab
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic effort!

I have just loaded natively from dev though the UI and all appears good.

Screenshot_20221017-112310

Please sign in to comment.