-
-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Amcrest switch platform #7876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Amcrest switch platform #7876
Changes from 4 commits
6db93fe
d196144
a03b484
64b0717
1645903
f6aed01
11efcc3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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: | ||
| 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dparker18 could you add the persistent_notification feature as https://github.com/home-assistant/home-assistant/blob/dev/homeassistant/components/sensor/amcrest.py#L59-L70