Refactored Amcrest to use central hub component (#8184)
* Refactored Amcrest to use central hub component * Set default streaming source to snapshot * Simplified code by using discovery platforms * Makes lint happy * Update authentication method to basic * Fixed lint issues * Makes Amcrest hub async * Make Amcrest hub IO synchronous and disabled ffmpeg dependency on sensor/amcrest. * Removed async to load component * Organized import order * Update amcrest.py
This commit is contained in:
1 parent
ac72dea09a
commit
f5e24cb0bb
5 files changed
+203
-145
No files matched your search
@@ -4,92 +4,50 @@ This component provides HA sensor support for Amcrest IP cameras.
|
||||
For more details about this platform, please refer to the documentation at
|
||||
https://home-assistant.io/components/sensor.amcrest/
|
||||
"""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
||||
from homeassistant.const import (
|
||||
CONF_HOST, CONF_NAME, CONF_MONITORED_CONDITIONS,
|
||||
CONF_USERNAME, CONF_PASSWORD, CONF_PORT, STATE_UNKNOWN)
|
||||
from homeassistant.components.amcrest import SENSORS
|
||||
from homeassistant.helpers.entity import Entity
|
||||
import homeassistant.loader as loader
|
||||
from homeassistant.const import STATE_UNKNOWN
|
||||
|
||||
from requests.exceptions import HTTPError, ConnectTimeout
|
||||
|
||||
REQUIREMENTS = ['amcrest==1.2.0']
|
||||
DEPENDENCIES = ['amcrest']
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
NOTIFICATION_ID = 'amcrest_notification'
|
||||
NOTIFICATION_TITLE = 'Amcrest Sensor Setup'
|
||||
|
||||
DEFAULT_NAME = 'Amcrest'
|
||||
DEFAULT_PORT = 80
|
||||
SCAN_INTERVAL = timedelta(seconds=10)
|
||||
|
||||
# Sensor types are defined like: Name, units, icon
|
||||
SENSOR_TYPES = {
|
||||
'motion_detector': ['Motion Detected', None, 'run'],
|
||||
'sdcard': ['SD Used', '%', 'sd'],
|
||||
'ptz_preset': ['PTZ Preset', None, 'camera-iris'],
|
||||
}
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend({
|
||||
vol.Required(CONF_HOST): cv.string,
|
||||
vol.Required(CONF_USERNAME): cv.string,
|
||||
vol.Required(CONF_PASSWORD): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||
vol.Required(CONF_MONITORED_CONDITIONS, default=[]):
|
||||
vol.All(cv.ensure_list, [vol.In(SENSOR_TYPES)]),
|
||||
})
|
||||
|
||||
|
||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||
@asyncio.coroutine
|
||||
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
|
||||
"""Set up a sensor for an Amcrest IP Camera."""
|
||||
from amcrest import AmcrestCamera
|
||||
if discovery_info is None:
|
||||
return
|
||||
|
||||
camera = AmcrestCamera(
|
||||
config.get(CONF_HOST), config.get(CONF_PORT),
|
||||
config.get(CONF_USERNAME), config.get(CONF_PASSWORD)).camera
|
||||
device = discovery_info['device']
|
||||
name = discovery_info['name']
|
||||
sensors = discovery_info['sensors']
|
||||
|
||||
persistent_notification = loader.get_component('persistent_notification')
|
||||
try:
|
||||
camera.current_time
|
||||
except (ConnectTimeout, HTTPError) as ex:
|
||||
_LOGGER.error("Unable to connect to Amcrest camera: %s", str(ex))
|
||||
persistent_notification.create(
|
||||
hass, 'Error: {}<br />'
|
||||
'You will need to restart hass after fixing.'
|
||||
''.format(ex),
|
||||
title=NOTIFICATION_TITLE,
|
||||
notification_id=NOTIFICATION_ID)
|
||||
return False
|
||||
|
||||
sensors = []
|
||||
for sensor_type in config.get(CONF_MONITORED_CONDITIONS):
|
||||
sensors.append(AmcrestSensor(config, camera, sensor_type))
|
||||
|
||||
add_devices(sensors, True)
|
||||
amcrest_sensors = []
|
||||
for sensor_type in sensors:
|
||||
amcrest_sensors.append(AmcrestSensor(name, device, sensor_type))
|
||||
|
||||
async_add_devices(amcrest_sensors, True)
|
||||
return True
|
||||
|
||||
|
||||
class AmcrestSensor(Entity):
|
||||
"""A sensor implementation for Amcrest IP camera."""
|
||||
|
||||
def __init__(self, device_info, camera, sensor_type):
|
||||
def __init__(self, name, camera, sensor_type):
|
||||
"""Initialize a sensor for Amcrest camera."""
|
||||
super(AmcrestSensor, self).__init__()
|
||||
self._attrs = {}
|
||||
self._camera = camera
|
||||
self._sensor_type = sensor_type
|
||||
self._name = '{0}_{1}'.format(device_info.get(CONF_NAME),
|
||||
SENSOR_TYPES.get(self._sensor_type)[0])
|
||||
self._icon = 'mdi:{}'.format(SENSOR_TYPES.get(self._sensor_type)[2])
|
||||
self._name = '{0}_{1}'.format(name,
|
||||
SENSORS.get(self._sensor_type)[0])
|
||||
self._icon = 'mdi:{}'.format(SENSORS.get(self._sensor_type)[2])
|
||||
self._state = STATE_UNKNOWN
|
||||
|
||||
@property
|
||||
@@ -115,7 +73,7 @@ class AmcrestSensor(Entity):
|
||||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the units of measurement."""
|
||||
return SENSOR_TYPES.get(self._sensor_type)[1]
|
||||
return SENSORS.get(self._sensor_type)[1]
|
||||
|
||||
def update(self):
|
||||
"""Get the latest data and updates the state."""
|
||||
|
||||
Reference in new issue
Block a user