Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix battery calculation #27

Merged
merged 25 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions airthings_ble/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Parser for Airthings BLE advertisements."""

from __future__ import annotations

from .parser import AirthingsBluetoothDeviceData, AirthingsDevice
Expand Down
16 changes: 5 additions & 11 deletions airthings_ble/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@
CHAR_UUID_WAVE_PLUS_DATA = UUID("b42e2a68-ade7-11e4-89d3-123b93f75cba")
CHAR_UUID_WAVE_2_DATA = UUID("b42e4dcc-ade7-11e4-89d3-123b93f75cba")
CHAR_UUID_WAVEMINI_DATA = UUID("b42e3b98-ade7-11e4-89d3-123b93f75cba")
COMMAND_UUID = UUID(
"b42e2d06-ade7-11e4-89d3-123b93f75cba"
) # "Access Control Point" Characteristic

COMMAND_UUID_WAVE_2 = UUID("b42e50d8-ade7-11e4-89d3-123b93f75cba")
COMMAND_UUID_WAVE_PLUS = UUID("b42e2d06-ade7-11e4-89d3-123b93f75cba")
COMMAND_UUID_WAVE_MINI = UUID("b42e3ef4-ade7-11e4-89d3-123b93f75cba")

"""
0 - 49 Bq/m3 (0 - 1.3 pCi/L):
Expand All @@ -48,13 +49,6 @@

CO2_MAX = 65534
VOC_MAX = 65534
HUMIDITY_MAX = 100
PERCENTAGE_MAX = 100
RADON_MAX = 16383
TEMPERATURE_MAX = 100

DEVICE_TYPE = {
"2900": "Wave gen. 1",
"2920": "Wave Mini",
"2930": "Wave Plus",
"2950": "Wave Radon",
}
113 changes: 113 additions & 0 deletions airthings_ble/device_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
"""Airthings device types."""

from enum import Enum


class AirthingsDeviceType(Enum):
"""Airthings device types."""

UNKNOWN = 0
WAVE_GEN_1 = "2900"
WAVE_MINI = "2920"
WAVE_PLUS = "2930"
WAVE_RADON = "2950"

raw_value: str # pylint: disable=invalid-name

def __new__(cls, value: str) -> "AirthingsDeviceType":
"""Create new device type."""
obj = object.__new__(cls)
obj.raw_value = value
return obj

@classmethod
def from_raw_value(cls, value: str) -> "AirthingsDeviceType":
"""Get device type from raw value."""
for device_type in cls:
if device_type.value == value:
device_type.raw_value = value
return device_type
unknown_device = AirthingsDeviceType.UNKNOWN
unknown_device.raw_value = value
return unknown_device

@property
def product_name(self) -> str:
"""Get product name."""
if self == AirthingsDeviceType.WAVE_GEN_1:
return "Wave Gen 1"
if self == AirthingsDeviceType.WAVE_MINI:
return "Wave Mini"
if self == AirthingsDeviceType.WAVE_PLUS:
return "Wave Plus"
if self == AirthingsDeviceType.WAVE_RADON:
return "Wave Radon"
return "Unknown"

def battery_percentage(self, voltage: float) -> int:
"""Calculate battery percentage based on voltage."""
if self == AirthingsDeviceType.WAVE_MINI:
return round(self._battery_percentage_wave_mini(voltage))
return round(self._battery_percentage_wave_radon_and_plus(voltage))

# pylint: disable=too-many-return-statements
def _battery_percentage_wave_radon_and_plus(self, voltage: float) -> float:
if voltage >= 3.00:
return 100
if 2.80 <= voltage < 3.00:
return self._interpolate(
voltage, voltage_range=(2.80, 3.00), percentage_range=(81, 100)
)
if 2.60 <= voltage < 2.80:
return self._interpolate(
voltage, voltage_range=(2.60, 2.80), percentage_range=(53, 81)
)
if 2.50 <= voltage < 2.60:
return self._interpolate(
voltage, voltage_range=(2.50, 2.60), percentage_range=(28, 53)
)
if 2.20 <= voltage < 2.50:
return self._interpolate(
voltage, voltage_range=(2.20, 2.50), percentage_range=(5, 28)
)
if 2.10 <= voltage < 2.20:
return self._interpolate(
voltage, voltage_range=(2.10, 2.20), percentage_range=(0, 5)
)
return 0

# pylint: disable=too-many-return-statements
def _battery_percentage_wave_mini(self, voltage: float) -> float:
if voltage >= 4.50:
return 100
if 4.20 <= voltage < 4.50:
return self._interpolate(
voltage=voltage, voltage_range=(4.20, 4.50), percentage_range=(85, 100)
)
if 3.90 <= voltage < 4.20:
return self._interpolate(
voltage=voltage, voltage_range=(3.90, 4.20), percentage_range=(62, 85)
)
if 3.75 <= voltage < 3.90:
return self._interpolate(
voltage=voltage, voltage_range=(3.75, 3.90), percentage_range=(42, 62)
)
if 3.30 <= voltage < 3.75:
return self._interpolate(
voltage=voltage, voltage_range=(3.30, 3.75), percentage_range=(23, 42)
)
if 2.40 <= voltage < 3.30:
return self._interpolate(
voltage=voltage, voltage_range=(2.40, 3.30), percentage_range=(0, 23)
)
return 0

def _interpolate(
self,
voltage: float,
voltage_range: tuple[float, float],
percentage_range: tuple[int, int],
) -> float:
return (voltage - voltage_range[0]) / (voltage_range[1] - voltage_range[0]) * (
percentage_range[1] - percentage_range[0]
) + percentage_range[0]
Loading
Loading