-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Eddystone Beacon Temperature Sensor #6789
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
96c1756
Added eddystone_temperature platform.
citruz 4b24608
Fixed style issues.
citruz ec7c80f
Fixed style issues #2.
citruz 472c370
Fixed style issues #3.
citruz aac5d88
Added new platform to .coveragerc
citruz 581ad6f
Refactored platform to use the beacontools package.
citruz cd1b001
Fixed style issues and added beacontools to excluded requirements.
citruz c77ebf9
Removed obsolete constants and added pylint exception.
citruz 968d4d8
Added blank line
citruz b457b0a
Updated beacontools to version 1.0.0
citruz b8aea72
Updated beacontools to version 1.0.1
citruz f485e23
Forgot to regenerate requirements_all
citruz a36bb24
Minor changes
citruz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
159 changes: 159 additions & 0 deletions
159
homeassistant/components/sensor/eddystone_temperature.py
This file contains hidden or 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,159 @@ | ||
| """Read temperature information from Eddystone beacons. | ||
|
|
||
| Your beacons must be configured to transmit UID (for identification) and TLM | ||
| (for temperature) frames. | ||
|
|
||
| For more details about this platform, please refer to the documentation at | ||
| https://home-assistant.io/components/sensor.eddystone_temperature/ | ||
|
|
||
| Original version of this code (for Skybeacons) by anpetrov. | ||
| https://github.com/anpetrov/skybeacon | ||
| """ | ||
| import logging | ||
|
|
||
| import voluptuous as vol | ||
|
|
||
| import homeassistant.helpers.config_validation as cv | ||
| from homeassistant.helpers.entity import Entity | ||
| from homeassistant.components.sensor import PLATFORM_SCHEMA | ||
| from homeassistant.const import ( | ||
| CONF_NAME, TEMP_CELSIUS, STATE_UNKNOWN, EVENT_HOMEASSISTANT_STOP, | ||
| CONF_NAMESPACE, CONF_INSTANCE, CONF_BT_DEVICE_ID, CONF_BEACONS) | ||
|
|
||
| REQUIREMENTS = ['beacontools[scan]==1.0.1'] | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
|
|
||
| BEACON_SCHEMA = vol.Schema({ | ||
| vol.Required(CONF_NAMESPACE): cv.string, | ||
| vol.Required(CONF_INSTANCE): cv.string, | ||
| vol.Optional(CONF_NAME): cv.string | ||
| }) | ||
|
|
||
| PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({ | ||
| vol.Optional(CONF_BT_DEVICE_ID, default=0): cv.positive_int, | ||
| vol.Required(CONF_BEACONS): vol.Schema({cv.string: BEACON_SCHEMA}), | ||
| }) | ||
|
|
||
|
|
||
| # pylint: disable=unused-argument | ||
| def setup_platform(hass, config, add_devices, discovery_info=None): | ||
| """Validate configuration, create devices and start monitoring thread.""" | ||
| _LOGGER.debug("Setting up...") | ||
|
|
||
| bt_device_id = config.get("bt_device_id") | ||
|
|
||
| beacons = config.get("beacons") | ||
| devices = [] | ||
|
|
||
| for dev_name, properties in beacons.items(): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too many blank lines (2) |
||
| namespace = get_from_conf(properties, "namespace", 20) | ||
| instance = get_from_conf(properties, "instance", 12) | ||
| name = properties.get(CONF_NAME, dev_name) | ||
|
|
||
| if instance is None or namespace is None: | ||
| _LOGGER.error("Skipping %s", dev_name) | ||
| continue | ||
| else: | ||
| devices.append(EddystoneTemp(name, namespace, instance)) | ||
|
|
||
| if len(devices) > 0: | ||
| mon = Monitor(hass, devices, bt_device_id) | ||
|
|
||
| def monitor_stop(_service_or_event): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. expected 1 blank line before a nested definition, found 0 |
||
| """Stop the monitor thread.""" | ||
| _LOGGER.info("Stopping scanner for eddystone beacons") | ||
| mon.stop() | ||
|
|
||
| add_devices(devices) | ||
| mon.start() | ||
| hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, monitor_stop) | ||
| else: | ||
| _LOGGER.warning("No devices were added") | ||
|
|
||
|
|
||
| def get_from_conf(config, config_key, length): | ||
| """Retrieve value from config and validate length.""" | ||
| string = config.get(config_key) | ||
| if len(string) != length: | ||
| _LOGGER.error("Error in config parameter \"%s\": Must be exactly %d " | ||
| "bytes. Device will not be added.", | ||
| config_key, length/2) | ||
| return None | ||
| else: | ||
| return string | ||
|
|
||
|
|
||
| class EddystoneTemp(Entity): | ||
| """Representation of a temperature sensor.""" | ||
|
|
||
| def __init__(self, name, namespace, instance): | ||
| """Initialize a sensor.""" | ||
| self._name = name | ||
| self.namespace = namespace | ||
| self.instance = instance | ||
| self.bt_addr = None | ||
| self.temperature = STATE_UNKNOWN | ||
|
|
||
| @property | ||
| def name(self): | ||
| """Return the name of the sensor.""" | ||
| return self._name | ||
|
|
||
| @property | ||
| def state(self): | ||
| """Return the state of the device.""" | ||
| return self.temperature | ||
|
|
||
| @property | ||
| def unit_of_measurement(self): | ||
| """Return the unit the value is expressed in.""" | ||
| return TEMP_CELSIUS | ||
|
|
||
|
|
||
| class Monitor(object): | ||
| """Continously scan for BLE advertisements.""" | ||
|
|
||
| def __init__(self, hass, devices, bt_device_id): | ||
| """Construct interface object.""" | ||
| self.hass = hass | ||
|
|
||
| # list of beacons to monitor | ||
| self.devices = devices | ||
| # number of the bt device (hciX) | ||
| self.bt_device_id = bt_device_id | ||
|
|
||
| def callback(bt_addr, _, packet, additional_info): | ||
| """Callback for new packets.""" | ||
| self.process_packet(additional_info['namespace'], | ||
| additional_info['instance'], | ||
| packet.temperature) | ||
|
|
||
| # pylint: disable=import-error | ||
| from beacontools import (BeaconScanner, EddystoneFilter, | ||
| EddystoneTLMFrame) | ||
| # Create a device filter for each device | ||
| device_filters = [EddystoneFilter(d.namespace, d.instance) | ||
| for d in devices] | ||
|
|
||
| self.scanner = BeaconScanner(callback, bt_device_id, device_filters, | ||
| EddystoneTLMFrame) | ||
|
|
||
| def start(self): | ||
| """Continously scan for BLE advertisements.""" | ||
| self.scanner.start() | ||
|
|
||
| def process_packet(self, namespace, instance, temperature): | ||
| """Assign temperature to hass device.""" | ||
| _LOGGER.debug("Received temperature for <%s,%s>: %d", | ||
| namespace, instance, temperature) | ||
|
|
||
| for dev in self.devices: | ||
| if dev.namespace == namespace and dev.instance == instance: | ||
| dev.temperature = temperature | ||
|
|
||
| def stop(self): | ||
| """Signal runner to stop and join thread.""" | ||
| _LOGGER.debug("Stopping...") | ||
| self.scanner.stop() | ||
| _LOGGER.debug("Stopped") | ||
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| 'Adafruit_BBIO', | ||
| 'fritzconnection', | ||
| 'pybluez', | ||
| 'beacontools', | ||
| 'bluepy', | ||
| 'python-lirc', | ||
| 'gattlib', | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expected 2 blank lines, found 1