-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from patman15/tests
Tests
- Loading branch information
Showing
21 changed files
with
1,349 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: Run tests for integrations | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
workflow_dispatch: | ||
schedule: | ||
- cron: '0 5 * * 6' | ||
|
||
jobs: | ||
validate: | ||
runs-on: "ubuntu-latest" | ||
steps: | ||
- name: "Checkout the repository" | ||
uses: "actions/checkout@v4" | ||
|
||
- name: "Set up Python" | ||
uses: actions/setup-python@main | ||
with: | ||
python-version: "3.11" | ||
cache: "pip" | ||
|
||
- name: Install dependencies | ||
run: pip install -r requirements_test.txt | ||
|
||
- name: Run tests and collect coverage | ||
run: pytest --cov-report term-missing --cov-fail-under=100 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Custom component.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
"""Module to support Dummy BMS.""" | ||
|
||
import logging | ||
from typing import Any | ||
|
||
from bleak.backends.device import BLEDevice | ||
|
||
from ..const import ( | ||
ATTR_BATTERY_CHARGING, | ||
# ATTR_BATTERY_LEVEL, | ||
ATTR_CURRENT, | ||
# ATTR_CYCLE_CAP, | ||
# ATTR_CYCLE_CHRG, | ||
# ATTR_CYCLES, | ||
ATTR_POWER, | ||
# ATTR_RUNTIME, | ||
# ATTR_TEMPERATURE, | ||
ATTR_VOLTAGE, | ||
) | ||
from .basebms import BaseBMS | ||
|
||
LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
class BMS(BaseBMS): | ||
"""Dummy battery class implementation.""" | ||
|
||
def __init__(self, ble_device: BLEDevice, reconnect: bool = False) -> None: | ||
"""Initialize BMS.""" | ||
LOGGER.debug("%s init(), BT address: %s", self.device_id(), ble_device.address) | ||
|
||
@staticmethod | ||
def matcher_dict_list() -> list[dict[str, Any]]: | ||
"""Provide BluetoothMatcher definition.""" | ||
return [{"local_name": "dummy", "connectable": True}] | ||
|
||
@staticmethod | ||
def device_info() -> dict[str, str]: | ||
"""Return device information for the battery management system.""" | ||
return {"manufacturer": "Dummy Manufacturer", "model": "dummy model"} | ||
|
||
async def disconnect(self) -> None: | ||
"""Disconnect connection to BMS if active.""" | ||
|
||
async def async_update(self) -> dict[str, int | float | bool]: | ||
"""Update battery status information.""" | ||
data = { | ||
ATTR_VOLTAGE: 12, | ||
ATTR_CURRENT: 1.5, | ||
} # set fixed values for dummy battery | ||
self.calc_values( | ||
data, {ATTR_POWER, ATTR_BATTERY_CHARGING} | ||
) # calculate further values from previously set ones | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "BLE Battery Management System (BMS)", | ||
"homeassistant": "2023.8.0", | ||
"homeassistant": "2024.3.0", | ||
"render_readme": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# pyproject.toml | ||
|
||
#[tool.setuptools.packages.find] | ||
#where = ["custom_components/"] | ||
#include = ["bms_ble"] | ||
|
||
[tool.pytest.ini_options] | ||
minversion = "6.0" | ||
pythonpath = [ | ||
".", | ||
"custom_components.bms_ble", | ||
] | ||
testpaths = [ | ||
"tests", | ||
] | ||
asyncio_mode = "auto" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
pip>=21.3.1 | ||
homeassistant==2024.3.0 | ||
wheel | ||
home-assistant-bluetooth | ||
habluetooth==2.4.0 | ||
bluetooth-adapters | ||
pytest>=8.0.2 | ||
pytest-cov>=4.0.0 | ||
pytest-socket>=0.5.0 | ||
pytest-asyncio | ||
sqlalchemy | ||
freezegun | ||
requests-mock | ||
syrupy>=4.1.0 | ||
aiohttp | ||
aiohttp_cors | ||
aiohttp-fast-url-dispatcher | ||
aiohttp-zlib-ng | ||
bleak>=0.19.0 | ||
bleak-retry-connector>=3.3.0 | ||
bluetooth-data-tools | ||
pyserial-asyncio | ||
pyudev | ||
pytest-homeassistant-custom-component==0.13.107 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""Tests for the BLE Battery Management System integration.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
"""Test helpers for bluetooth copied from HA 2024.3.0.""" | ||
|
||
from typing import Any | ||
|
||
from bleak import AdvertisementData, BLEDevice | ||
|
||
from homeassistant.components.bluetooth import ( | ||
SOURCE_LOCAL, | ||
BluetoothServiceInfoBleak, | ||
async_get_advertisement_callback, | ||
) | ||
from homeassistant.core import HomeAssistant | ||
|
||
ADVERTISEMENT_DATA_DEFAULTS = { | ||
"local_name": "", | ||
"manufacturer_data": {}, | ||
"service_data": {}, | ||
"service_uuids": [], | ||
"rssi": -127, | ||
"platform_data": ((),), | ||
"tx_power": -127, | ||
} | ||
|
||
BLE_DEVICE_DEFAULTS = { | ||
"name": None, | ||
"rssi": -127, | ||
"details": None, | ||
} | ||
|
||
|
||
def generate_advertisement_data(**kwargs: Any) -> AdvertisementData: | ||
"""Generate advertisement data with defaults.""" | ||
new = kwargs.copy() | ||
for key, value in ADVERTISEMENT_DATA_DEFAULTS.items(): | ||
new.setdefault(key, value) | ||
return AdvertisementData(**new) | ||
|
||
|
||
def generate_ble_device( | ||
address: str | None = None, | ||
name: str | None = None, | ||
details: Any | None = None, | ||
rssi: int | None = None, | ||
**kwargs: Any, | ||
) -> BLEDevice: | ||
"""Generate a BLEDevice with defaults.""" | ||
new = kwargs.copy() | ||
if address is not None: | ||
new["address"] = address | ||
if name is not None: | ||
new["name"] = name | ||
if details is not None: | ||
new["details"] = details | ||
if rssi is not None: | ||
new["rssi"] = rssi | ||
for key, value in BLE_DEVICE_DEFAULTS.items(): | ||
new.setdefault(key, value) | ||
return BLEDevice(**new) | ||
|
||
|
||
def inject_advertisement_with_time_and_source_connectable( | ||
hass: HomeAssistant, | ||
device: BLEDevice, | ||
adv: AdvertisementData, | ||
time: float, | ||
source: str, | ||
connectable: bool, | ||
) -> None: | ||
"""Inject an advertisement into the manager from a specific source at a time and connectable status.""" | ||
async_get_advertisement_callback(hass)( | ||
BluetoothServiceInfoBleak( | ||
name=adv.local_name or device.name or device.address, | ||
address=device.address, | ||
rssi=adv.rssi, | ||
manufacturer_data=adv.manufacturer_data, | ||
service_data=adv.service_data, | ||
service_uuids=adv.service_uuids, | ||
source=source, | ||
device=device, | ||
advertisement=adv, | ||
connectable=connectable, | ||
time=time, | ||
) | ||
) | ||
|
||
|
||
def inject_bluetooth_service_info_bleak( | ||
hass: HomeAssistant, info: BluetoothServiceInfoBleak | ||
) -> None: | ||
"""Inject an advertisement into the manager with connectable status.""" | ||
advertisement_data = generate_advertisement_data( | ||
local_name=None if info.name == "" else info.name, | ||
manufacturer_data=info.manufacturer_data, | ||
service_data=info.service_data, | ||
service_uuids=info.service_uuids, | ||
rssi=info.rssi, | ||
) | ||
device = generate_ble_device( # type: ignore[no-untyped-call] | ||
address=info.address, | ||
name=info.name, | ||
details={}, | ||
) | ||
inject_advertisement_with_time_and_source_connectable( | ||
hass, | ||
device, | ||
advertisement_data, | ||
info.time, | ||
SOURCE_LOCAL, | ||
connectable=info.connectable, | ||
) |
Oops, something went wrong.