Skip to content

Commit

Permalink
Update docs on using the library fingerprints in HA (#599)
Browse files Browse the repository at this point in the history
* Update fingerprints_in_homeassistant.rst

* Update fingerprints_in_homeassistant.rst

* Update fingerprints_in_homeassistant.rst
  • Loading branch information
rikroe authored Feb 18, 2024
1 parent f59746b commit bd4b15a
Showing 1 changed file with 18 additions and 41 deletions.
59 changes: 18 additions & 41 deletions docs/source/development/fingerprints_in_homeassistant.rst
Original file line number Diff line number Diff line change
@@ -1,52 +1,29 @@
Using fingerprints in Home Assistant
====================================
Sometimes it can be useful to load the **fingerprints** used for our **pytest suite** in the development of the Home Assistant component.
Sometimes it can be useful to load the **fingerprints** used for our **pytest suite** in the development of the Home Assistant component.
This enables debugging of the UI in Home Assistant which is not possible from pytest alone.

Set up bimmer_connected
-----------------------
Clone the repository to ``~/bimmer_connected``:

.. code-block:: bash
.. warning::
This is for the `Home Assistant development environment <https://developers.home-assistant.io/docs/development_environment>`_ only! Do not do this on your live instance!

git clone https://github.com/bimmerconnected/bimmer_connected.git ~/bimmer_connected
Setup and start Home Assistant in the `development environment <https://developers.home-assistant.io/docs/development_environment>`_ at least once and let all python packages install (``hass -c ./config``).
If not already done, set up the **BMW Connected Drive Integration**. You need to login a MyBMW account at least once. Shut down Homeassistant afterwards.

.. note::

Make sure that you can access this path from inside your Home Assistant virtual environment!

Edit the ``_get_vehicles()`` function in `account.py#L370-L392 <https://github.com/bimmerconnected/bimmer_connected/blob/master/bimmer_connected/account.py#L370-L392>`_
and add the following code between ``self._get_oauth_token()`` and ``for brand in CarBrand:``:

.. code-block:: python
from pathlib import Path
files = (Path().home() / "bimmer_connected" / "test" / "responses").rglob("vehicles_v2_*_0.json")
for file in files:
for vehicle_dict in json.load(open(file, 'r')):
# If vehicle already exists, just update it's state
existing_vehicle = self.get_vehicle(vehicle_dict["vin"])
if existing_vehicle:
existing_vehicle.update_state(vehicle_dict)
else:
self._vehicles.append(MyBMWVehicle(self, vehicle_dict))
The MyBMW account does not need to contain vehicles, a demo account without attached vehicles is sufficient.

Set up Home Assistant
---------------------
If not already done, `set up the Home Assistant development environment <https://developers.home-assistant.io/docs/development_environment>`_.
Now, we have to "hack" our mocked backend calls into Home Assistant.

Now start Home Assistant at least once and let all python packages install (``hass -c ./config``).
If not already done, set up the **BMW Connected Drive Integration** in Home Assistant.
Shut down Homeassistant afterwards.
Edit ``homeassistant/components/bmw_connected_drive/coordinator.py`` and locate the function ``def _async_update_data()``. We now have to replace ``await self.account.get_vehicles()``. The ``try .. except`` block should look like this:

In the Home Assistant virtual environment, install the freshly adjusted version of ``bimmer_connected``:

.. code-block:: bash
pip3 install -e ~/bimmer_connected
Start Home Assistant using ``hass -c ./config --skip-pip`` and see all cars we have fingerprints of + your own cars.
.. code-block:: python
.. warning::
If ``--skip-pip`` is omitted when starting Home Assistant, the version of ``bimmer_connected`` defined in
``homeassistant/components/bmw_connected_drive/manifest.json`` will be loaded and the Home Assistant last two steps have to be executed again.
...
try:
from bimmer_connected.tests.conftest import MyBMWMockRouter, ALL_STATES, ALL_CHARGING_SETTINGS
with MyBMWMockRouter(["WBY00000000REXI01"], ALL_STATES, ALL_CHARGING_SETTINGS):
await self.account.get_vehicles()
except:
...
As the first parameter, you can specify a list of VINs for debugging or leave it empty (``None`` or ``[]``) to load all vehicles of our test suite.

0 comments on commit bd4b15a

Please # to comment.