Remove Amcrest deprecated sensors and switches (#30308)

This commit is contained in:
Phil Bruckner 2019-12-31 08:16:41 -06:00 committed by GitHub
parent e68cd339b9
commit 609bf445f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 200 deletions

View File

@ -11,7 +11,6 @@ from homeassistant.auth.permissions.const import POLICY_CONTROL
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR
from homeassistant.components.camera import DOMAIN as CAMERA
from homeassistant.components.sensor import DOMAIN as SENSOR
from homeassistant.components.switch import DOMAIN as SWITCH
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_AUTHENTICATION,
@ -22,7 +21,6 @@ from homeassistant.const import (
CONF_PORT,
CONF_SCAN_INTERVAL,
CONF_SENSORS,
CONF_SWITCHES,
CONF_USERNAME,
ENTITY_MATCH_ALL,
HTTP_BASIC_AUTHENTICATION,
@ -34,12 +32,11 @@ from homeassistant.helpers.dispatcher import async_dispatcher_send, dispatcher_s
from homeassistant.helpers.event import track_time_interval
from homeassistant.helpers.service import async_extract_entity_ids
from .binary_sensor import BINARY_SENSOR_MOTION_DETECTED, BINARY_SENSORS
from .binary_sensor import BINARY_SENSORS
from .camera import CAMERA_SERVICES, STREAM_SOURCE_LIST
from .const import CAMERAS, DATA_AMCREST, DEVICES, DOMAIN, SERVICE_UPDATE
from .helpers import service_signal
from .sensor import SENSOR_MOTION_DETECTOR, SENSORS
from .switch import SWITCHES
from .sensor import SENSORS
_LOGGER = logging.getLogger(__name__)
@ -65,68 +62,36 @@ SCAN_INTERVAL = timedelta(seconds=10)
AUTHENTICATION_LIST = {"basic": "basic"}
def _deprecated_sensor_values(sensors):
if SENSOR_MOTION_DETECTOR in sensors:
_LOGGER.warning(
"The '%s' option value '%s' is deprecated, "
"please remove it from your configuration and use "
"the '%s' option with value '%s' instead",
CONF_SENSORS,
SENSOR_MOTION_DETECTOR,
CONF_BINARY_SENSORS,
BINARY_SENSOR_MOTION_DETECTED,
)
return sensors
def _deprecated_switches(config):
if CONF_SWITCHES in config:
_LOGGER.warning(
"The '%s' option (with value %s) is deprecated, "
"please remove it from your configuration and use "
"services and attributes instead",
CONF_SWITCHES,
config[CONF_SWITCHES],
)
return config
def _has_unique_names(devices):
names = [device[CONF_NAME] for device in devices]
vol.Schema(vol.Unique())(names)
return devices
AMCREST_SCHEMA = vol.All(
vol.Schema(
{
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.Optional(
CONF_AUTHENTICATION, default=HTTP_BASIC_AUTHENTICATION
): vol.All(vol.In(AUTHENTICATION_LIST)),
vol.Optional(CONF_RESOLUTION, default=DEFAULT_RESOLUTION): vol.All(
vol.In(RESOLUTION_LIST)
),
vol.Optional(CONF_STREAM_SOURCE, default=STREAM_SOURCE_LIST[0]): vol.All(
vol.In(STREAM_SOURCE_LIST)
),
vol.Optional(CONF_FFMPEG_ARGUMENTS, default=DEFAULT_ARGUMENTS): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): cv.time_period,
vol.Optional(CONF_BINARY_SENSORS): vol.All(
cv.ensure_list, [vol.In(BINARY_SENSORS)]
),
vol.Optional(CONF_SENSORS): vol.All(
cv.ensure_list, [vol.In(SENSORS)], _deprecated_sensor_values
),
vol.Optional(CONF_SWITCHES): vol.All(cv.ensure_list, [vol.In(SWITCHES)]),
vol.Optional(CONF_CONTROL_LIGHT, default=True): cv.boolean,
}
),
_deprecated_switches,
AMCREST_SCHEMA = vol.Schema(
{
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.Optional(CONF_AUTHENTICATION, default=HTTP_BASIC_AUTHENTICATION): vol.All(
vol.In(AUTHENTICATION_LIST)
),
vol.Optional(CONF_RESOLUTION, default=DEFAULT_RESOLUTION): vol.All(
vol.In(RESOLUTION_LIST)
),
vol.Optional(CONF_STREAM_SOURCE, default=STREAM_SOURCE_LIST[0]): vol.All(
vol.In(STREAM_SOURCE_LIST)
),
vol.Optional(CONF_FFMPEG_ARGUMENTS, default=DEFAULT_ARGUMENTS): cv.string,
vol.Optional(CONF_SCAN_INTERVAL, default=SCAN_INTERVAL): cv.time_period,
vol.Optional(CONF_BINARY_SENSORS): vol.All(
cv.ensure_list, [vol.In(BINARY_SENSORS)]
),
vol.Optional(CONF_SENSORS): vol.All(cv.ensure_list, [vol.In(SENSORS)]),
vol.Optional(CONF_CONTROL_LIGHT, default=True): cv.boolean,
}
)
CONFIG_SCHEMA = vol.Schema(
@ -216,7 +181,6 @@ def setup(hass, config):
resolution = RESOLUTION_LIST[device[CONF_RESOLUTION]]
binary_sensors = device.get(CONF_BINARY_SENSORS)
sensors = device.get(CONF_SENSORS)
switches = device.get(CONF_SWITCHES)
stream_source = device[CONF_STREAM_SOURCE]
control_light = device.get(CONF_CONTROL_LIGHT)
@ -252,11 +216,6 @@ def setup(hass, config):
hass, SENSOR, DOMAIN, {CONF_NAME: name, CONF_SENSORS: sensors}, config
)
if switches:
discovery.load_platform(
hass, SWITCH, DOMAIN, {CONF_NAME: name, CONF_SWITCHES: switches}, config
)
if not hass.data[DATA_AMCREST][DEVICES]:
return False

View File

@ -15,12 +15,10 @@ _LOGGER = logging.getLogger(__name__)
SCAN_INTERVAL = timedelta(seconds=SENSOR_SCAN_INTERVAL_SECS)
SENSOR_MOTION_DETECTOR = "motion_detector"
SENSOR_PTZ_PRESET = "ptz_preset"
SENSOR_SDCARD = "sdcard"
# Sensor types are defined like: Name, units, icon
SENSORS = {
SENSOR_MOTION_DETECTOR: ["Motion Detected", None, "mdi:run"],
SENSOR_PTZ_PRESET: ["PTZ Preset", None, "mdi:camera-iris"],
SENSOR_SDCARD: ["SD Used", "%", "mdi:sd"],
}
@ -94,11 +92,7 @@ class AmcrestSensor(Entity):
_LOGGER.debug("Updating %s sensor", self._name)
try:
if self._sensor_type == SENSOR_MOTION_DETECTOR:
self._state = self._api.is_motion_detected
self._attrs["Record Mode"] = self._api.record_mode
elif self._sensor_type == SENSOR_PTZ_PRESET:
if self._sensor_type == SENSOR_PTZ_PRESET:
self._state = self._api.ptz_presets_count
elif self._sensor_type == SENSOR_SDCARD:

View File

@ -1,126 +0,0 @@
"""Support for toggling Amcrest IP camera settings."""
import logging
from amcrest import AmcrestError
from homeassistant.const import CONF_NAME, CONF_SWITCHES
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import ToggleEntity
from .const import DATA_AMCREST, DEVICES, SERVICE_UPDATE
from .helpers import log_update_error, service_signal
_LOGGER = logging.getLogger(__name__)
MOTION_DETECTION = "motion_detection"
MOTION_RECORDING = "motion_recording"
# Switch types are defined like: Name, icon
SWITCHES = {
MOTION_DETECTION: ["Motion Detection", "mdi:run-fast"],
MOTION_RECORDING: ["Motion Recording", "mdi:record-rec"],
}
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
"""Set up the IP Amcrest camera switch platform."""
if discovery_info is None:
return
name = discovery_info[CONF_NAME]
device = hass.data[DATA_AMCREST][DEVICES][name]
async_add_entities(
[
AmcrestSwitch(name, device, setting)
for setting in discovery_info[CONF_SWITCHES]
],
True,
)
class AmcrestSwitch(ToggleEntity):
"""Representation of an Amcrest IP camera switch."""
def __init__(self, name, device, setting):
"""Initialize the Amcrest switch."""
self._name = "{} {}".format(name, SWITCHES[setting][0])
self._signal_name = name
self._api = device.api
self._setting = setting
self._state = False
self._icon = SWITCHES[setting][1]
self._unsub_dispatcher = None
@property
def name(self):
"""Return the name of the switch if any."""
return self._name
@property
def is_on(self):
"""Return true if switch is on."""
return self._state
def turn_on(self, **kwargs):
"""Turn setting on."""
if not self.available:
return
try:
if self._setting == MOTION_DETECTION:
self._api.motion_detection = "true"
elif self._setting == MOTION_RECORDING:
self._api.motion_recording = "true"
except AmcrestError as error:
log_update_error(_LOGGER, "turn on", self.name, "switch", error)
def turn_off(self, **kwargs):
"""Turn setting off."""
if not self.available:
return
try:
if self._setting == MOTION_DETECTION:
self._api.motion_detection = "false"
elif self._setting == MOTION_RECORDING:
self._api.motion_recording = "false"
except AmcrestError as error:
log_update_error(_LOGGER, "turn off", self.name, "switch", error)
@property
def available(self):
"""Return True if entity is available."""
return self._api.available
def update(self):
"""Update setting state."""
if not self.available:
return
_LOGGER.debug("Updating %s switch", self._name)
try:
if self._setting == MOTION_DETECTION:
detection = self._api.is_motion_detector_on()
elif self._setting == MOTION_RECORDING:
detection = self._api.is_record_on_motion_detection()
self._state = detection
except AmcrestError as error:
log_update_error(_LOGGER, "update", self.name, "switch", error)
@property
def icon(self):
"""Return the icon for the switch."""
return self._icon
async def async_on_demand_update(self):
"""Update state."""
self.async_schedule_update_ha_state(True)
async def async_added_to_hass(self):
"""Subscribe to update signal."""
self._unsub_dispatcher = async_dispatcher_connect(
self.hass,
service_signal(SERVICE_UPDATE, self._signal_name),
self.async_on_demand_update,
)
async def async_will_remove_from_hass(self):
"""Disconnect from update signal."""
self._unsub_dispatcher()