Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ omit =
homeassistant/components/sensor/xbox_live.py
homeassistant/components/sensor/yweather.py
homeassistant/components/sensor/zamg.py
homeassistant/components/switch/amcrest.py
homeassistant/components/switch/acer_projector.py
homeassistant/components/switch/anel_pwrctrl.py
homeassistant/components/switch/arest.py
Expand Down
96 changes: 96 additions & 0 deletions homeassistant/components/switch/amcrest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
This component supports turning on/off motion detection on Amcrest IP cameras.

For more details about this platform, please refer to the documentation at
https://home-assistant.io/components/switch.amcrest/
"""

import logging
import voluptuous as vol
from requests.exceptions import HTTPError, ConnectTimeout

import homeassistant.helpers.config_validation as cv
from homeassistant.components.sensor import PLATFORM_SCHEMA
from homeassistant.const import (
CONF_HOST, CONF_NAME, CONF_USERNAME, CONF_PASSWORD,
CONF_PORT, STATE_UNKNOWN, STATE_OFF, STATE_ON)
from homeassistant.helpers.entity import ToggleEntity

REQUIREMENTS = ['amcrest==1.2.0']
_LOGGER = logging.getLogger(__name__)

DEFAULT_NAME = 'Amcrest'
DEFAULT_PORT = 80

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,
})


def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up a sensor for an Amcrest IP Camera."""
from amcrest import AmcrestCamera

camera = AmcrestCamera(
config.get(CONF_HOST), config.get(CONF_PORT),
config.get(CONF_USERNAME), config.get(CONF_PASSWORD)).camera

try:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

camera.current_time
except (ConnectTimeout, HTTPError) as ex:
_LOGGER.error("Unable to connect to Amcrest camera: %s", str(ex))
return False

add_devices([AmcrestMotionSwitch(config, camera)])
return True


class AmcrestMotionSwitch(ToggleEntity):
"""Representation of a switch to toggle on/off motion detection."""

def __init__(self, device_info, camera):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

too many blank lines (2)

"""Initialize the switch."""
self._camera = camera
self._name = device_info.get(CONF_NAME)
self._state = STATE_UNKNOWN

@property

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

too many blank lines (2)

def should_poll(self):
"""Poll for status regularly."""
return True

@property

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

too many blank lines (2)

def name(self):
"""Return the name of the device if any."""
return self._name

@property

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

too many blank lines (2)

def state(self):
"""Return the state of the motion detection."""
return self._state

@property

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

too many blank lines (2)

def is_on(self):
"""Return true if motion detection is on."""
return self._state == STATE_ON

def turn_on(self, **kwargs):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

too many blank lines (2)

"""Turn the device on."""
_LOGGER.info("Turning on Motion Detection")
self._camera.motion_detection = 'true'

def turn_off(self, **kwargs):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

too many blank lines (2)

"""Turn the device off."""
_LOGGER.info("Turning off Motion Detection")
self._camera.motion_detection = 'false'

def update(self):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

too many blank lines (2)

"""Update Motion Detection state."""
_LOGGER.debug("Pulling Motion Detection data from %s sensor.",
self._name)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

continuation line under-indented for visual indent

detection = self._camera.is_motion_detector_on()
self._state = STATE_ON if detection else STATE_OFF
1 change: 1 addition & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ alarmdecoder==0.12.1.0

# homeassistant.components.camera.amcrest
# homeassistant.components.sensor.amcrest
# homeassistant.components.switch.amcrest
amcrest==1.2.0

# homeassistant.components.media_player.anthemav
Expand Down