core/homeassistant/components/tof/sensor.py

109 lines
3.2 KiB
Python
Raw Normal View History

Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
"""Platform for Time of Flight sensor VL53L1X from STMicroelectronics."""
import asyncio
from functools import partial
import logging
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
from VL53L1X2 import VL53L1X # pylint: disable=import-error
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
import voluptuous as vol
from homeassistant.components import rpi_gpio
from homeassistant.components.sensor import PLATFORM_SCHEMA
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
from homeassistant.const import CONF_NAME
import homeassistant.helpers.config_validation as cv
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
LENGTH_MILLIMETERS = "mm"
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
2019-07-31 19:25:30 +00:00
CONF_I2C_ADDRESS = "i2c_address"
CONF_I2C_BUS = "i2c_bus"
CONF_XSHUT = "xshut"
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "VL53L1X"
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
DEFAULT_I2C_ADDRESS = 0x29
DEFAULT_I2C_BUS = 1
DEFAULT_XSHUT = 16
DEFAULT_RANGE = 2
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_I2C_ADDRESS, default=DEFAULT_I2C_ADDRESS): vol.Coerce(int),
vol.Optional(CONF_I2C_BUS, default=DEFAULT_I2C_BUS): vol.Coerce(int),
vol.Optional(CONF_XSHUT, default=DEFAULT_XSHUT): cv.positive_int,
}
)
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
def init_tof_0(xshut, sensor):
"""XSHUT port LOW resets the device."""
sensor.open()
rpi_gpio.setup_output(xshut)
rpi_gpio.write_output(xshut, 0)
def init_tof_1(xshut):
"""XSHUT port HIGH enables the device."""
rpi_gpio.setup_output(xshut)
rpi_gpio.write_output(xshut, 1)
2019-07-31 19:25:30 +00:00
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
"""Reset and initialize the VL53L1X ToF Sensor from STMicroelectronics."""
name = config.get(CONF_NAME)
bus_number = config.get(CONF_I2C_BUS)
i2c_address = config.get(CONF_I2C_ADDRESS)
unit = LENGTH_MILLIMETERS
xshut = config.get(CONF_XSHUT)
2019-07-31 19:25:30 +00:00
sensor = await hass.async_add_executor_job(partial(VL53L1X, bus_number))
await hass.async_add_executor_job(init_tof_0, xshut, sensor)
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
await asyncio.sleep(0.01)
2019-07-31 19:25:30 +00:00
await hass.async_add_executor_job(init_tof_1, xshut)
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
await asyncio.sleep(0.01)
dev = [VL53L1XSensor(sensor, name, unit, i2c_address)]
async_add_entities(dev, True)
class VL53L1XSensor(Entity):
"""Implementation of VL53L1X sensor."""
def __init__(self, vl53l1x_sensor, name, unit, i2c_address):
"""Initialize the sensor."""
self._name = name
self._unit_of_measurement = unit
self.vl53l1x_sensor = vl53l1x_sensor
self.i2c_address = i2c_address
self._state = None
self.init = True
@property
def name(self) -> str:
"""Return the name of the sensor."""
return self._name
@property
def state(self) -> int:
"""Return the state of the sensor."""
return self._state
@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement."""
return self._unit_of_measurement
def update(self):
"""Get the latest measurement and update state."""
if self.init:
2019-07-31 19:25:30 +00:00
self.vl53l1x_sensor.add_sensor(self.i2c_address, self.i2c_address)
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
self.init = False
2019-07-31 19:25:30 +00:00
self.vl53l1x_sensor.start_ranging(self.i2c_address, DEFAULT_RANGE)
Add Time of Flight Sensor using VL53L1X (#21230) * Add Time of Flight Sensor using VL53L1X * Fix issues found by bot * Fix issues from bot * Remove extra logs * Keep removing logs dependencies not used * Remove log from update * Add logger info to async_update * Fix over-indented line * Fix pylint error * Remove logger reporting successful operation * Update requirements * Update requirements_all.txt * Update requirements_test_all.txt * Used isort to keep imports and added STMicroelectronics to docstring * Replace time.sleep by asyncio.sleep * Add requirements to COMMENT_REQUIREMENTS and fix typo * Using async_add_executor_job to schedule the call in the pool * Fix typo * Optimize async_update * Updated requirements files * Group and schedule calls that should be run sequentially * Fix lint errors * Revision showing development history * Cleaning and typos * Cleaning and typos * Fix wrong-import-order * Fix gen_requirements_all * Schedule rpi_gpio I/O on the executor thread pool * Fix partial parameters * Fix bot error - add blank line * Fix lint error * Remove dependencies from requirements * Review initial commits * Move all device I/O to async_update * Update requirements_all.txt * Revised header with no url to the docs * Use async_added_to_hass to add and initialize the sensor * Add docstring to init() * Move sensor.open() to async_setup_platform * Remove logging and async * Fix typo * Move sensor.open to safe initialization * Fix typo * Fix typo * Add the new tof module to .coveragerc * Move the sensor platform under a tof package * Update .coveragerc and requirements_all for tof package
2019-03-08 07:21:22 +00:00
self.vl53l1x_sensor.update(self.i2c_address)
self.vl53l1x_sensor.stop_ranging(self.i2c_address)
self._state = self.vl53l1x_sensor.distance