Skip to content

Commit

Permalink
Merge pull request #32 from spin83/master
Browse files Browse the repository at this point in the history
Add pin number setting functionality.
  • Loading branch information
AdamStrojek authored Nov 13, 2024
2 parents d19b89a + 3d1fead commit 490a9a9
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
19 changes: 18 additions & 1 deletion libetrv/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def time_to_str(datetime):


class CLI:
def __init__(self, pin=b'0000', secret=None):
def __init__(self, pin=None, secret=None):
self._pin = pin
if secret is not None:
self._secret = bytes.fromhex(secret)
Expand Down Expand Up @@ -51,6 +51,11 @@ def battery(self):
result = self._device.battery
print("Battery level: {}%".format(result))

def pin_settings(self):
result = self._device.pin_settings
print('Pin number: {:04d}'.format(result.pin_number))
print('Pin enabled: {}'.format(result.pin_enabled))

def settings(self):
result = self._device.settings
print('Frost protection temperature: {:.1f}°C'.format(result.frost_protection_temperature))
Expand All @@ -74,6 +79,18 @@ def current_time(self):

def set_setpoint(self, setpoint):
self._device.temperature.set_point_temperature = setpoint

def set_pin(self, pin):
if pin==0:
self._device.pin_settings.pin_number = 0
self._device.pin_settings.pin_enabled = False
self._device.pin_settings.save()
elif pin>0 and pin<=9999:
self._device.pin_settings.pin_number = pin
self._device.pin_settings.pin_enabled = True
self._device.pin_settings.save()
else:
print('Invalid pin number')


if __name__ == "__main__":
Expand Down
12 changes: 12 additions & 0 deletions libetrv/data_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ class Meta:
read_only = True
direct_field = 'battery'

class PinSettingsData(eTRVData):
pin_number = eTRVField()
pin_enabled = BitField(name='pin_enabled', bit_position=0)

class Meta:
structure = {
0x27: """
unsigned int pin_number;
unsigned char pin_enabled;
unsigned char padding[3];
"""
}

class ScheduleMode(enum.Enum):
MANUAL = 0
Expand Down
6 changes: 4 additions & 2 deletions libetrv/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .bluetooth import btle
from loguru import logger

from .data_struct import BatteryData, SettingsData, TemperatureData, CurrentTimeData, SecretKeyData, NameData
from .data_struct import BatteryData, PinSettingsData, SettingsData, TemperatureData, CurrentTimeData, SecretKeyData, NameData
from .properties import eTRVProperty
from .utils import etrv_read, etrv_write

Expand All @@ -26,7 +26,7 @@ def __init__(self, address, secret=None, pin=None, retry_limit=None):
"""
self.address = address
self.secret = secret
self.pin = b'0000' if pin is None else pin
self.pin = b'\0\0\0\0' if pin is None else pin.to_bytes(4, byteorder='big')
self.ble_device = None
self.__pin_already_sent = False

Expand Down Expand Up @@ -117,6 +117,8 @@ def send_pin(self):
self.__pin_already_sent = True

battery = eTRVProperty(BatteryData)

pin_settings = eTRVProperty(PinSettingsData)

settings = eTRVProperty(SettingsData)

Expand Down
42 changes: 42 additions & 0 deletions tests/unit/test_device_pin_settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pytest

from tests.utils.device_mock import DeviceMock

@pytest.fixture()
def device():
dev = DeviceMock({
0x27: bytes.fromhex('e5b4e8de36e711a9')
})
return dev

@pytest.fixture()
def settings(device):
return device.pin_settings

class TestEncryptionPinSettings:
def test_pin_enabled(self, settings):
assert settings.pin_enabled == True

def test_pin_number(self, settings):
assert settings.pin_number == 4617

def test_disable_pin(self, device):
device.pin_settings.pin_number = 0
device.pin_settings.pin_enabled = False
device.pin_settings.save()

assert device.ble_device.handlers_history.pop() == 0x27
assert device.fields['pin_settings'].raw_data[0x27].pin_number == 0
assert device.fields['pin_settings'].raw_data[0x27].pin_enabled == False
assert device.ble_device.sent_data_history.pop() == bytes.fromhex('19854b04365f7f62')

def test_enable_pin(self, device):
device.pin_settings.pin_number = 9021
device.pin_settings.pin_enabled = True
device.pin_settings.save()

assert device.ble_device.handlers_history.pop() == 0x27
assert device.fields['pin_settings'].raw_data[0x27].pin_number == 9021
assert device.fields['pin_settings'].raw_data[0x27].pin_enabled == True
assert device.ble_device.sent_data_history.pop() == bytes.fromhex('e28c1ff50892b8a1')

0 comments on commit 490a9a9

Please sign in to comment.