Skip to content

Commit

Permalink
renamed module to mitemp_bt
Browse files Browse the repository at this point in the history
  • Loading branch information
ratcashdev committed Mar 27, 2018
1 parent ec5e373 commit bbd3ce4
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 50 deletions.
2 changes: 1 addition & 1 deletion .coveragerc
@@ -1,5 +1,5 @@
[run]
source = mitemp
source = mitemp_bt

omit =

Expand Down
19 changes: 9 additions & 10 deletions README.md
@@ -1,5 +1,4 @@
# mitemp - Library for Xiaomi Mi Temperature and Humidity Sensor (v2) with Bleutooth LE and the LCD display

# mitemp_bt - Library for Xiaomi Mi Temperature and Humidity Sensor (v2) with Bleutooth LE and the LCD display


This library lets you read sensor data from a Xiaomi Mi BluetoothLE Temperature and Humidity sensor.
Expand Down Expand Up @@ -27,10 +26,10 @@ binaray available on your machine.

Example to use the bluez/gatttool wrapper:
```python
from mitemp.mitemp_poller import MiTempPoller
from mitemp_bt.mitemp_bt_poller import MiTempBtPoller
from btlewrap.gatttool import GatttoolBackend

poller = MiTempPoller('some mac address', GatttoolBackend)
poller = MiTempBtPoller('some mac address', GatttoolBackend)
```

### bluepy
Expand All @@ -39,10 +38,10 @@ To use the [bluepy](https://github.com/IanHarvey/bluepy) library you have to ins

Example to use the bluepy backend:
```python
from mitemp.mitemp_poller import MiTempPoller
from mitemp_bt.mitemp_bt_poller import MiTempBtPoller
from btlewrap.bluepy import BluepyBackend

poller = MiTempPoller('some mac address', BluepyBackend)
poller = MiTempBtPoller('some mac address', BluepyBackend)
```

### pygatt
Expand All @@ -52,7 +51,7 @@ install the bluepy library on your machine. In most cases this can be done via:

Example to use the pygatt backend:
```python
from mitemp.mitemp_poller import MiTempPoller
from mitemp_bt.mitemp_bt_poller import MiTempBtPoller
from btlewrap.pygatt import PygattBackend

poller = MiTempPoller('some mac address', PygattBackend)
Expand All @@ -63,9 +62,9 @@ please have a look at [CONTRIBUTING.md](CONTRIBUTING.md)

----

## Projects Depending on `mitemp`
## Projects Depending on `mitemp_bt`

The following shows a selected list of projects using this library:

* https://github.com/ThomDietrich/miflora-mqtt-daemon - An MQTT Client/Daemon for Smart Home solution integration
* https://home-assistant.io/components/sensor.miflora/ - Integration in Home Assistant
* https://github.com/ThomDietrich/mitemp_bt-mqtt-daemon - An MQTT Client/Daemon for Smart Home solution integration
* https://home-assistant.io/components/sensor.mitemp_bt/ - Integration in Home Assistant
4 changes: 2 additions & 2 deletions demo.py
Expand Up @@ -7,7 +7,7 @@
import sys

from btlewrap import available_backends, BluepyBackend, GatttoolBackend, PygattBackend
from mitemp.mitemp_poller import MiTempPoller, \
from mitemp_bt.mitemp_bt_poller import MiTempBtPoller, \
MI_TEMPERATURE, MI_HUMIDITY, MI_BATTERY


Expand All @@ -21,7 +21,7 @@ def valid_mitemp_mac(mac, pat=re.compile(r"4C:65:A8:[0-9A-F]{2}:[0-9A-F]{2}:[0-9
def poll(args):
"""Poll data from the sensor."""
backend = _get_backend(args)
poller = MiTempPoller(args.mac, backend)
poller = MiTempBtPoller(args.mac, backend)
print("Getting data from Mi Temperature and Humidity Sensor")
print("FW: {}".format(poller.firmware_version()))
print("Name: {}".format(poller.name()))
Expand Down
20 changes: 10 additions & 10 deletions mitemp.py
Expand Up @@ -2,7 +2,7 @@
Support for Xiaomi Mi Temp BLE environmental sensor.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.mitemp/
https://home-assistant.io/components/sensor.mitemp_bt/
"""
import logging

Expand All @@ -16,7 +16,7 @@
)


REQUIREMENTS = ['mitemp==0.0.1']
REQUIREMENTS = ['mitemp_bt==0.0.1']

_LOGGER = logging.getLogger(__name__)

Expand All @@ -30,7 +30,7 @@
DEFAULT_UPDATE_INTERVAL = 300
DEFAULT_FORCE_UPDATE = False
DEFAULT_MEDIAN = 3
DEFAULT_NAME = 'Mi Temp'
DEFAULT_NAME = 'MiTemp BT'
DEFAULT_RETRIES = 2
DEFAULT_TIMEOUT = 10

Expand All @@ -57,19 +57,19 @@


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the MiTemp sensor."""
from mitemp import mitemp_poller
"""Set up the MiTempBt sensor."""
from mitemp_bt import mitemp_bt_poller
try:
import bluepy.btle # noqa: F401 # pylint: disable=unused-variable
from btlewrap import BluepyBackend
backend = BluepyBackend
except ImportError:
from btlewrap import GatttoolBackend
backend = GatttoolBackend
_LOGGER.debug('MiTemp is using %s backend.', backend.__name__)
_LOGGER.debug('MiTempBt is using %s backend.', backend.__name__)

cache = config.get(CONF_CACHE)
poller = mitemp_poller.MiTempPoller(
poller = mitemp_bt_poller.MiTempBtPoller(
config.get(CONF_MAC), cache_timeout=cache,
adapter=config.get(CONF_ADAPTER), backend=backend)
force_update = config.get(CONF_FORCE_UPDATE)
Expand All @@ -87,14 +87,14 @@ def setup_platform(hass, config, add_devices, discovery_info=None):
if prefix:
name = "{} {}".format(prefix, name)

devs.append(MiTempSensor(
devs.append(MiTempBtSensor(
poller, parameter, name, unit, force_update, median))

add_devices(devs)


class MiTempSensor(Entity):
"""Implementing the MiTemp sensor."""
class MiTempBtSensor(Entity):
"""Implementing the MiTempBt sensor."""

def __init__(self, poller, parameter, name, unit, force_update, median):
"""Initialize the sensor."""
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion mitemp/mitemp_poller.py → mitemp_bt/mitemp_bt_poller.py
Expand Up @@ -20,7 +20,7 @@
_LOGGER = logging.getLogger(__name__)


class MiTempPoller(object):
class MiTempBtPoller(object):
""""
A class to read data from Mi Temp plant sensors.
"""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -2,7 +2,7 @@
from setuptools import setup, find_packages

setup(
name='mitemp',
name='mitemp_bt',
version='0.0.1',
description='Library to read data from Mi Temperature and Humidity Sensor (V2) with LCD display',
url='https://github.com/open-homeautomation/mitemp',
Expand Down
6 changes: 3 additions & 3 deletions test/integration_tests/test_everything.py
Expand Up @@ -3,8 +3,8 @@
import pytest
from btlewrap.gatttool import GatttoolBackend
from btlewrap.bluepy import BluepyBackend
from mitemp.mitemp_poller import (MiTempPoller, MI_TEMPERATURE,
MI_HUMIDITY, MI_BATTERY)
from mitemp_bt.mitemp_bt_poller import (MiTempBtPoller, MI_TEMPERATURE,
MI_HUMIDITY, MI_BATTERY)


class TestEverythingGatt(unittest.TestCase):
Expand All @@ -24,7 +24,7 @@ def test_everything(self):
real sensor close by.
"""
assert hasattr(self, "mac")
poller = MiTempPoller(self.mac, self.backend_type)
poller = MiTempBtPoller(self.mac, self.backend_type)
self.assertIsNotNone(poller.firmware_version())
self.assertIsNotNone(poller.name())
self.assertIsNotNone(poller.parameter_value(MI_TEMPERATURE))
Expand Down
Expand Up @@ -3,11 +3,11 @@
from test.helper import MockBackend, ConnectExceptionBackend, RWExceptionBackend

from btlewrap.base import BluetoothBackendException
from mitemp.mitemp_poller import MiTempPoller, MI_TEMPERATURE, MI_HUMIDITY, MI_BATTERY
from mitemp_bt.mitemp_bt_poller import MiTempBtPoller, MI_TEMPERATURE, MI_HUMIDITY, MI_BATTERY


class TestMiTempPoller(unittest.TestCase):
"""Tests for the MiTempPoller class."""
class TestMiTempBtPoller(unittest.TestCase):
"""Tests for the MiTempBtPoller class."""

# access to protected members is fine in testing
# pylint: disable = protected-access
Expand All @@ -16,11 +16,11 @@ class TestMiTempPoller(unittest.TestCase):

def test_format_bytes(self):
"""Test conversion of bytes to string."""
self.assertEqual('AA BB 00', MiTempPoller._format_bytes([0xAA, 0xBB, 0x00]))
self.assertEqual('AA BB 00', MiTempBtPoller._format_bytes([0xAA, 0xBB, 0x00]))

def test_read_battery(self):
"""Test reading the battery level."""
poller = MiTempPoller(self.TEST_MAC, MockBackend)
poller = MiTempBtPoller(self.TEST_MAC, MockBackend)
backend = self._get_backend(poller)
backend.battery_level = 50
self.assertEqual(50, poller.battery_level())
Expand All @@ -29,7 +29,7 @@ def test_read_battery(self):

def test_read_version(self):
"""Test reading the version number."""
poller = MiTempPoller(self.TEST_MAC, MockBackend)
poller = MiTempBtPoller(self.TEST_MAC, MockBackend)
backend = self._get_backend(poller)
backend.set_version('00.00.11')
self.assertEqual('00.00.11', poller.firmware_version())
Expand All @@ -40,7 +40,7 @@ def test_read_measurements(self):
Here we expect some data being written to the sensor.
"""
poller = MiTempPoller(self.TEST_MAC, MockBackend)
poller = MiTempBtPoller(self.TEST_MAC, MockBackend)
backend = self._get_backend(poller)

backend.temperature = 56.7
Expand All @@ -49,15 +49,15 @@ def test_read_measurements(self):

def test_name(self):
"""Check reading of the sensor name."""
poller = MiTempPoller(self.TEST_MAC, MockBackend)
poller = MiTempBtPoller(self.TEST_MAC, MockBackend)
backend = self._get_backend(poller)
backend.name = 'my sensor name'

self.assertEqual(backend.name, poller.name())

def test_clear_cache(self):
"""Test with negative temperature."""
poller = MiTempPoller(self.TEST_MAC, MockBackend)
poller = MiTempBtPoller(self.TEST_MAC, MockBackend)
backend = self._get_backend(poller)

self.assertFalse(poller.cache_available())
Expand All @@ -82,7 +82,7 @@ def test_no_answer_data(self):
Check that this triggers the right exception.
"""
poller = MiTempPoller(self.TEST_MAC, MockBackend)
poller = MiTempBtPoller(self.TEST_MAC, MockBackend)
backend = self._get_backend(poller)
backend.handle_0x0010_raw = None
with self.assertRaises(BluetoothBackendException):
Expand All @@ -93,7 +93,7 @@ def test_no_answer_name(self):
Check that this triggers the right exception.
"""
poller = MiTempPoller(self.TEST_MAC, MockBackend)
poller = MiTempBtPoller(self.TEST_MAC, MockBackend)
backend = self._get_backend(poller)
backend.handle_0x03_raw = None
with self.assertRaises(BluetoothBackendException):
Expand All @@ -104,14 +104,14 @@ def test_no_answer_firmware_version(self):
Check that this triggers the right exception.
"""
poller = MiTempPoller(self.TEST_MAC, MockBackend)
poller = MiTempBtPoller(self.TEST_MAC, MockBackend)
backend = self._get_backend(poller)
backend.handle_0x0024_raw = None
self.assertTrue(poller.firmware_version() is None)

def test_connect_exception(self):
"""Test reaction when getting a BluetoothBackendException."""
poller = MiTempPoller(self.TEST_MAC, ConnectExceptionBackend, retries=0)
poller = MiTempBtPoller(self.TEST_MAC, ConnectExceptionBackend, retries=0)
with self.assertRaises(BluetoothBackendException):
poller.firmware_version()
with self.assertRaises(BluetoothBackendException):
Expand All @@ -123,7 +123,7 @@ def test_connect_exception(self):

def test_rw_exception(self):
"""Test reaction when getting a BluetoothBackendException."""
poller = MiTempPoller(self.TEST_MAC, RWExceptionBackend, retries=0)
poller = MiTempBtPoller(self.TEST_MAC, RWExceptionBackend, retries=0)
with self.assertRaises(BluetoothBackendException):
poller.firmware_version()
with self.assertRaises(BluetoothBackendException):
Expand All @@ -135,5 +135,5 @@ def test_rw_exception(self):

@staticmethod
def _get_backend(poller):
"""Get the backend from a MiTempPoller object."""
"""Get the backend from a MiTempBtPoller object."""
return poller._bt_interface._backend
6 changes: 3 additions & 3 deletions test/unit_tests/test_parse.py
Expand Up @@ -5,7 +5,7 @@
import unittest
from datetime import datetime
from test.helper import MockBackend
from mitemp.mitemp_poller import MiTempPoller, MI_TEMPERATURE, MI_HUMIDITY
from mitemp_bt.mitemp_bt_poller import MiTempBtPoller, MI_TEMPERATURE, MI_HUMIDITY


class KNXConversionTest(unittest.TestCase):
Expand All @@ -14,8 +14,8 @@ class KNXConversionTest(unittest.TestCase):
# pylint: disable=protected-access

def test_parsing(self):
"""Does the Mi TEMP data parser works correctly?"""
poller = MiTempPoller(None, MockBackend)
"""Does the Mi TEMP BT data parser works correctly?"""
poller = MiTempBtPoller(None, MockBackend)
data = bytes([0x54, 0x3d, 0x32, 0x35, 0x2e, 0x36, 0x20,
0x48, 0x3d, 0x32, 0x33, 0x2e, 0x36, 0x00])
poller._cache = data
Expand Down
4 changes: 2 additions & 2 deletions test/unit_tests/test_versioncheck.py
Expand Up @@ -15,7 +15,7 @@ def test_py2(self):
if sys.version_info >= self.MIN_SUPPORTED_VERSION:
return
try:
import mitemp # noqa: F401 # pylint: disable=unused-variable
import mitemp_bt # noqa: F401 # pylint: disable=unused-variable
self.fail('Should have thrown an exception')
except ValueError as val_err:
self.assertIn('version', str(val_err))
Expand All @@ -24,4 +24,4 @@ def test_py3(self):
"""Make sure newer python versions do not throw an exception."""
if sys.version_info < self.MIN_SUPPORTED_VERSION:
return
import mitemp # noqa: F401 # pylint: disable=unused-variable
import mitemp_bt # noqa: F401 # pylint: disable=unused-variable
4 changes: 2 additions & 2 deletions tox.ini
Expand Up @@ -9,7 +9,7 @@ deps =
-rrequirements.txt
-rrequirements-test.txt
passenv = TRAVIS TRAVIS_*
commands = pytest --cov=mitemp --timeout=10 test/unit_tests
commands = pytest --cov=mitemp_bt --timeout=10 test/unit_tests

[testenv:py27]
# only run unit tests as they do not need additional hardware
Expand All @@ -33,4 +33,4 @@ commands=flake8
[testenv:pylint]
basepython = python3
skip_install = true
commands = pylint -j4 mitemp test setup.py demo.py
commands = pylint -j4 mitemp_bt test setup.py demo.py

0 comments on commit bbd3ce4

Please sign in to comment.