core/homeassistant/components/blink/sensor.py

86 lines
2.5 KiB
Python
Raw Normal View History

"""
Support for Blink system camera sensors.
For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/sensor.blink/
"""
import logging
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
from homeassistant.components.blink import BLINK_DATA, SENSORS
from homeassistant.helpers.entity import Entity
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
from homeassistant.const import CONF_MONITORED_CONDITIONS
_LOGGER = logging.getLogger(__name__)
DEPENDENCIES = ['blink']
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up a Blink sensor."""
if discovery_info is None:
return
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
data = hass.data[BLINK_DATA]
devs = []
for camera in data.cameras:
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
for sensor_type in discovery_info[CONF_MONITORED_CONDITIONS]:
devs.append(BlinkSensor(data, camera, sensor_type))
add_entities(devs, True)
class BlinkSensor(Entity):
"""A Blink camera sensor."""
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
def __init__(self, data, camera, sensor_type):
"""Initialize sensors from Blink camera."""
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
name, units, icon = SENSORS[sensor_type]
self._name = "{} {} {}".format(
BLINK_DATA, camera, name)
self._camera_name = name
self._type = sensor_type
self.data = data
self._camera = data.cameras[camera]
self._state = None
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
self._unit_of_measurement = units
self._icon = icon
self._unique_id = "{}-{}".format(self._camera.serial, self._type)
self._sensor_key = self._type
if self._type == 'temperature':
self._sensor_key = 'temperature_calibrated'
@property
def name(self):
"""Return the name of the camera."""
return self._name
@property
def unique_id(self):
"""Return the unique id for the camera sensor."""
return self._unique_id
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
@property
def icon(self):
"""Return the icon of the sensor."""
return self._icon
@property
def state(self):
"""Return the camera's current state."""
return self._state
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return self._unit_of_measurement
def update(self):
"""Retrieve sensor data from the camera."""
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
self.data.refresh()
try:
self._state = self._camera.attributes[self._sensor_key]
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
except KeyError:
self._state = None
Overhaul of Blink platform (#16942) * Using new methods for blink camera - Refactored blink platform (breaking change) - Camera needs to be uniquely enabled in config from now on - Added motion detection enable/disable to camera platform * Fix motion detection - bumped blinkpy to 0.8.1 - Added wifi strength sensor * Added platform schema to sensor - Added global variables for brand and attribution to main platform * Removed blink binary sensor * Add alarm control panel * Fixed dependency, added alarm_home * Update requirements * Fix lint errors * Updated throttle times * Add trigger_camera service (replaced snap_picture) * Add refresh after camera trigger * Update blinkpy version * Wait for valid camera response before returning image - Motion detection now working! * Updated for new blinkpy 0.9.0 * Add refresh control and other fixes for new blinkpy release * Add save video service * Pushing to force bot to update * Changed based on first review - Pass blink as BLINK_DATA instead of DOMAIN - Remove alarm_arm_home from alarm_control_panel - Re-add discovery with schema for sensors/binar_sensors - Change motion_detected to a binary_sensor - Added camera_armed binary sensor - Update camera device_state_attributes rather than state_attributes * Moved blink.py to own folder. Added service hints. * Updated coveragerc to reflect previous change * Register services with DOMAIN - Change device add for loop order in binary_sensor * Fix lint error * services.async_register -> services.register
2018-10-03 02:17:14 +00:00
_LOGGER.error(
"%s not a valid camera attribute. Did the API change?",
self._sensor_key)