Skip to content

Add support for multiple Doorbird stations#13994

Merged
syssi merged 24 commits intohome-assistant:devfrom
oblogic7:multiple_doorbird_support
Jun 10, 2018
Merged

Add support for multiple Doorbird stations#13994
syssi merged 24 commits intohome-assistant:devfrom
oblogic7:multiple_doorbird_support

Conversation

@oblogic7
Copy link
Copy Markdown
Contributor

@oblogic7 oblogic7 commented Apr 19, 2018

Description:

Adds support for configuring multiple Doorbird stations.

Pull request in home-assistant.github.io with documentation (if applicable): home-assistant/home-assistant.io#5205

Example entry for configuration.yaml (if applicable):

doorbird:
  devices:
    - host: <host_ip>
      username: <username>
      password: <password>
      name: Side Entry
    - host: <host_ip>
      username: <username>
      password: <password>
      name: Front Door
      monitored_conditions:
         - doorbell
         - motion

Checklist:

  • The code change is tested and works locally.
  • Local tests pass with tox. Your PR cannot be merged unless tests pass

If user exposed functionality or configuration variables are added/changed:


return True

class ConfiguredDoorbird():
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

expected 2 blank lines, found 1

Comment thread homeassistant/components/doorbird.py Outdated
hass_url = doorstation_config.get(CONF_CUSTOM_URL)

# This will make HA the only service that gets doorbell events
url = '{}{}/{}/{}'.format(hass_url, API_URL, index + 1, SENSOR_DOORBELL)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (84 > 79 characters)

Comment thread homeassistant/components/doorbird.py Outdated
# Get the URL of this server
hass_url = hass.config.api.base_url

# Override it if another is specified in the component configuration
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (80 > 79 characters)

Comment thread homeassistant/components/doorbird.py Outdated
_LOGGER.info("Connected to DoorBird at %s as %s", device_ip, username)
doorstations.append(ConfiguredDoorbird(device, name))
elif status[1] == 401:
_LOGGER.error("Authorization rejected by DoorBird at %s", device_ip)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (80 > 79 characters)

Comment thread homeassistant/components/doorbird.py Outdated
status = device.ready()

if status[0]:
_LOGGER.info("Connected to DoorBird at %s as %s", device_ip, username)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (82 > 79 characters)

Comment thread homeassistant/components/doorbird.py Outdated
device_ip = doorstation_config.get(CONF_HOST)
username = doorstation_config.get(CONF_USERNAME)
password = doorstation_config.get(CONF_PASSWORD)
name = doorstation_config.get(CONF_NAME) or 'DoorBird {}'.format(index + 1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (83 > 79 characters)

Comment thread homeassistant/components/doorbird.py Outdated
import voluptuous as vol

from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD
from homeassistant.const import CONF_HOST, CONF_USERNAME, CONF_PASSWORD, \
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'homeassistant.const.CONF_DEVICES' imported but unused

device.history_image_url(1, 'doorbell'), _CAMERA_LAST_VISITOR.format(doorstation.name),
_LAST_VISITOR_INTERVAL),
DoorBirdCamera(
device.history_image_url(1, 'motionsensor'), _CAMERA_LAST_MOTION.format(doorstation.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.

line too long (106 > 79 characters)

async_add_devices([
DoorBirdCamera(device.live_image_url, _CAMERA_LIVE.format(doorstation.name), _LIVE_INTERVAL),
DoorBirdCamera(
device.history_image_url(1, 'doorbell'), _CAMERA_LAST_VISITOR.format(doorstation.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.

line too long (103 > 79 characters)

for doorstation in hass.data.get(DOORBIRD_DOMAIN):
device = doorstation.device
async_add_devices([
DoorBirdCamera(device.live_image_url, _CAMERA_LIVE.format(doorstation.name), _LIVE_INTERVAL),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (105 > 79 characters)

Comment thread homeassistant/components/doorbird.py Outdated
username = doorstation_config.get(CONF_USERNAME)
password = doorstation_config.get(CONF_PASSWORD)
name = doorstation_config.get(CONF_NAME) \
or 'DoorBird {}'.format(index + 1)
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 over-indented for visual indent

@syssi
Copy link
Copy Markdown
Member

syssi commented Apr 19, 2018

Did you see the lint errors at the travis builds?

lint runtests: commands[1] | flake8
./homeassistant/components/doorbird.py:58:12: E127 continuation line over-indented for visual indent
./homeassistant/components/doorbird.py:102:1: D101 Missing docstring in public class
./homeassistant/components/doorbird.py:103:1: D102 Missing docstring in public method
./homeassistant/components/doorbird.py:108:1: D102 Missing docstring in public method
./homeassistant/components/doorbird.py:112:1: D102 Missing docstring in public method
./homeassistant/components/doorbird.py:115:1: E302 expected 2 blank lines, found 1
ERROR: InvocationError for command '/home/travis/build/home-assistant/home-assistant/.tox/lint/bin/flake8' (exited with code 1)


CONFIG_SCHEMA = vol.Schema({
DOMAIN: vol.Schema({
vol.Required(CONF_HOST): cv.string,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would like to suggest this schema:

doorbird:
  gateways:
    - host: <host_ip>
      username: <username>
      password: <password>
      doorbell_events: 1
      name: Side Entry
    - host: <host_ip>
      username: <username>
      password: <password>
      doorbell_events: 1
      name: Front Door

In this case the schema can be extended in future with parameters applied to all gateways without another beaking change of the configuration.

cp. #13517

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.

This schema is better (extensible)
Although these are not really gateways I think. So gateways should be devices probably.

Comment thread homeassistant/components/doorbird.py Outdated
CONF_DOORBELL_EVENTS = 'doorbell_events'
CONF_CUSTOM_URL = 'hass_url_override'

DEVICES_SCHEMA = vol.All(cv.ensure_list,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Call it DEVICE_SCHEMA and move the vol.All(cv.ensure_list, [DEVICE_SCHEMA]) part to the CONFIG_SCHEMA.

Copy link
Copy Markdown
Member

@pvizeli pvizeli left a comment

Choose a reason for hiding this comment

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

Why you wrap it into a devices?

doorbird:
  - xy: fdsf
     kds: dfsd
  - ...

That allow a more clean struct if you have only one device.

@syssi
Copy link
Copy Markdown
Member

syssi commented Apr 21, 2018

I've suggested this schema to be future proof: #13994 (comment)

@syssi
Copy link
Copy Markdown
Member

syssi commented Apr 25, 2018

@pvizeli Do you agree with the devices key?

@pvizeli
Copy link
Copy Markdown
Member

pvizeli commented Apr 27, 2018

It was only a comment. With 2 devices it look okay but with only one device (and maybe the biggest case) it look a bit wired. Anyway it's only a comment :)

@syssi
Copy link
Copy Markdown
Member

syssi commented Apr 28, 2018

Do you like to improve the schema of the doorbell events? You could use the entity_id instead of enumerate the devices:

# cp. https://www.home-assistant.io/components/binary_sensor.xiaomi_aqara/#xiaomi-wireless-button
- alias: Doorbird ring
  trigger:
    platform: event
    event_type: doorbird
    event_data:
      entity_id: doorbird.front_door
      doorbird_type: doorbell
  action:
     service: script.turn_on
       entity_id: script.doorbell

@oblogic7
Copy link
Copy Markdown
Contributor Author

I think that is a good idea. I'll work on making those changes.

Use dispatcher for doorbird pushed events.
Comment thread homeassistant/components/doorbird.py Outdated
class ConfiguredDoorbird():
"""Attach additional information to pass along with configured device."""

def __init__(self, device, name, custom_url = None):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

unexpected spaces around keyword / parameter equals

doorstation.device.subscribe_notification(self._event_type, url)


@asyncio.coroutine
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 setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Doorbird doorbell sensor platform."""

doorbird = hass.data.get(DOORBIRD_DOMAIN)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

local variable 'doorbird' is assigned to but never used

import logging
import datetime
import voluptuous as vol
from homeassistant.core import callback
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

'homeassistant.core.callback' imported but unused

Comment thread homeassistant/components/doorbird.py Outdated
# Get the URL of this server
hass_url = hass.config.api.base_url

# Override url if another is specified in the doorstation configuration
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (83 > 79 characters)

Comment thread homeassistant/components/doorbird.py Outdated
for sensor_type in doorstation.monitored_events:
name = '{} {}'.format(doorstation.name,
SENSOR_TYPES[sensor_type][0])
device_class = SENSOR_TYPES[sensor_type][1]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

local variable 'device_class' is assigned to but never used

Comment thread homeassistant/components/doorbird.py Outdated
# Get the URL of this server
hass_url = hass.config.api.base_url

# Override url if another is specified in the doorstation configuration
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

line too long (83 > 79 characters)

Comment thread homeassistant/components/doorbird.py Outdated
for sensor_type in doorstation.monitored_events:
name = '{} {}'.format(doorstation.name,
SENSOR_TYPES[sensor_type][0])
device_class = SENSOR_TYPES[sensor_type][1]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

local variable 'device_class' is assigned to but never used

import asyncio
import logging

import asyncio
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please sort imports 🔤 within groups standard library, 3rd party and homeassistant. There should be a blank line between the three groups.

"""Support for powering relays in a DoorBird video doorbell."""
import datetime
import logging

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't remove blank line between standard library and 3rd party imports.


device = doorstation.device

switches = []
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Move this out of the loop. It's accessed outside the loop.

Comment thread homeassistant/components/doorbird.py Outdated
return self._monitored_events


class DoorbirdEventSubscriber():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this a class? Looks like you just need a function.


return True

def subscribe_events(hass, doorstation):
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

expected 2 blank lines, found 1

@oblogic7
Copy link
Copy Markdown
Contributor Author

@MartinHjelmare Thank you for the diligent code review. It is very helpful since I'm new to Python and HA development. Hopefully these latest changes are all that will be required.

@syssi
Copy link
Copy Markdown
Member

syssi commented May 23, 2018

I'm fine with the state of this PR. Nevertheless I would like to point you to this comment again: #13994 (comment)

It's important here/now because this is already a breaking change. The component shouldn't break again in the near future.

@oblogic7
Copy link
Copy Markdown
Contributor Author

@syssi How would I get the entity_id from within the subscribe_events method? This is before any entities (camera or switch) have been added via add_devices.

@oblogic7
Copy link
Copy Markdown
Contributor Author

I'm not familiar enough with HA core to know when/how an entity_id is assigned. This is ready to merge unless somebody can provide direction on @syssi's suggestion.

@syssi
Copy link
Copy Markdown
Member

syssi commented Jun 8, 2018

The subscribe_events call could be moved the setup_platform of the sensor/switch. The entity_id is available there and the code belongs somehow to the entity IMO. Please feel free to improve your PR. I will care about a soonish merge.

@syssi syssi mentioned this pull request Jun 9, 2018
6 tasks
CONF_CUSTOM_URL = 'hass_url_override'

DOORBELL_EVENT = 'doorbell'
MOTION_EVENT = 'motionsensor'
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you call the event motion? I will approved and merge the PR afterwards.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That is the event name required by the device API for event subscriptions.

image

https://www.doorbird.com/downloads/api_lan.pdf?rev=0.20

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Got it. Sounds a bit weird.

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.

@oblogic7 That's only for monitor.cgi. The new schedule.cgi uses 'motion'. No need to update this PR, I'm fixing it in mine 🙂

image

@syssi syssi changed the title Doorbird component enhancements Add support for multiple Doorbird stations Jun 10, 2018
@syssi syssi merged commit 1c561ea into home-assistant:dev Jun 10, 2018
@balloob balloob mentioned this pull request Jun 22, 2018
girlpunk pushed a commit to girlpunk/home-assistant that referenced this pull request Sep 4, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Dec 10, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants