Skip to content

BME680 Sensor Component#11695

Merged
balloob merged 9 commits into
home-assistant:devfrom
arcsur:bme680
Jan 23, 2018
Merged

BME680 Sensor Component#11695
balloob merged 9 commits into
home-assistant:devfrom
arcsur:bme680

Conversation

@arcsur
Copy link
Copy Markdown
Contributor

@arcsur arcsur commented Jan 16, 2018

Description:

BME680 Sensor Component

Related issue (if applicable): N/A

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#4432

Example entry for configuration.yaml (if applicable):

sensor:
  - platform: bme680

Checklist:

  • The code change is tested and works locally.

If user exposed functionality or configuration variables are added/changed:

If the code communicates with devices, web services, or third-party tools:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • New dependencies have been added to the REQUIREMENTS variable (example).
  • New dependencies are only imported inside functions that use them (example).
  • New dependencies have been added to requirements_all.txt by running script/gen_requirements_all.py.
  • New files were added to .coveragerc.

If the code does not interact with devices:

  • Local tests with tox run successfully. Your PR cannot be merged unless tests pass
  • Tests have been added to verify that the new code works.

@arcsur arcsur requested a review from andrey-git as a code owner January 16, 2018 09:39
@homeassistant
Copy link
Copy Markdown
Contributor

Hi @arcsur,

It seems you haven't yet signed a CLA. Please do so here.

Once you do that we will be able to review and accept this pull request.

Thanks!

@arcsur
Copy link
Copy Markdown
Contributor Author

arcsur commented Jan 16, 2018

Please note that it is necessary to run the sensor update outside of the event loop in a thread if the gas sensor is enabled. There needs to be a consistent interval between the gas sensor reads to keep the readings stable. I tried many ways to do this but the only method that worked was to use a separate thread to read the sensor data at a fixed interval (every 1 second) and then replicate the data structure so that home-assistant can read it on the event loop.

The thread is only used if the gas sensor is enabled (also required for Air Quality calculations), otherwise it is entirely run on the event loop.


# pylint: disable=import-error
@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's very inefficient to keep yielding to the executor pool all the time. Better to just run the whole setup in 1 worker.

You can either make the setup_platform sync (by changing name to setup_platform) or define a function that runs all the I/O commands and run that in the executor pool.

Copy link
Copy Markdown
Contributor Author

@arcsur arcsur Jan 19, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@balloob I have refactored async_setup_platform to call a single function that runs the I/O commands.

Please review.

@arcsur arcsur changed the title Bme680 BME680 Sensor Component Jan 19, 2018
@arcsur
Copy link
Copy Markdown
Contributor Author

arcsur commented Jan 19, 2018

@balloob I have refactored async_setup_platform to call a single function that runs the I/O commands.

Please review.

@balloob balloob merged commit 09e3bf9 into home-assistant:dev Jan 23, 2018
@balloob
Copy link
Copy Markdown
Member

balloob commented Jan 23, 2018

Neat! Thanks 🐬 🌮

Copy link
Copy Markdown
Member

@MartinHjelmare MartinHjelmare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor things that would be good to adjust in a future PR. See below.


Temperature, humidity, pressure and volitile gas support.
Air Qaulity calucaltion based on humidity and volatile gas.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing link to docs here. Look at other modules.


sensor_handler = yield from hass.async_add_job(_setup_bme680, config)
if sensor_handler is None:
return False
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing is checking this return value, so just return here.

pass

async_add_devices(dev)
return True
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just return.

"""Set up and configure the BME680 sensor."""
from smbus import SMBus
import bme680
from time import sleep
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Standard library imports should be imported at the top of the module. Only platform specific imports need to be done inside functions.

self.sensor_data.air_quality = self._calculate_aq_score()
time.sleep(1)
else:
return
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could move this to the initial check if not self._gas_sensor_running after inverting that. Then you could outdent most of the lines in this method.

for variable in config[CONF_MONITORED_CONDITIONS]:
dev.append(BME680Sensor(
sensor_handler, variable, SENSOR_TYPES[variable][1], name))
except KeyError:
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't happen with config validation in place. You can remove the try... except.

@arcsur arcsur deleted the bme680 branch January 24, 2018 04:20
@arcsur
Copy link
Copy Markdown
Contributor Author

arcsur commented Jan 24, 2018

@MartinHjelmare I have implemented the changes you requested in PR#: 11892.
#11892

@arcsur arcsur mentioned this pull request Jan 24, 2018
3 tasks
@balloob balloob mentioned this pull request Jan 26, 2018
@home-assistant home-assistant locked and limited conversation to collaborators May 29, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants