Skip to content

Commit

Permalink
Config Entry setup for zwave lock
Browse files Browse the repository at this point in the history
  • Loading branch information
cgarwood committed Nov 4, 2018
1 parent a39846b commit a902387
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
11 changes: 10 additions & 1 deletion homeassistant/components/lock/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def is_locked(hass, entity_id=None):

async def async_setup(hass, config):
"""Track states and offer events for locks."""
component = EntityComponent(
component = hass.data[DOMAIN] = EntityComponent(
_LOGGER, DOMAIN, hass, SCAN_INTERVAL, GROUP_NAME_ALL_LOCKS)

await component.async_setup(config)
Expand All @@ -79,6 +79,15 @@ async def async_setup(hass, config):
return True


async def async_setup_entry(hass, entry):
"""Set up a config entry."""
return await hass.data[DOMAIN].async_setup_entry(entry)


async def async_unload_entry(hass, entry):
"""Unload a config entry."""
return await hass.data[DOMAIN].async_unload_entry(entry)

class LockDevice(Entity):
"""Representation of a lock."""

Expand Down
17 changes: 14 additions & 3 deletions homeassistant/components/lock/zwave.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

import voluptuous as vol

from homeassistant.core import callback
from homeassistant.components.lock import DOMAIN, LockDevice
from homeassistant.components import zwave
from homeassistant.helpers.dispatcher import async_dispatcher_connect
import homeassistant.helpers.config_validation as cv

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -120,9 +122,18 @@

async def async_setup_platform(hass, config, async_add_entities,
discovery_info=None):
"""Set up the Z-Wave Lock platform."""
await zwave.async_setup_platform(
hass, config, async_add_entities, discovery_info)
"""Old method of setting up Z-Wave locks."""
pass


async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up Z-Wave Lock from Config Entry."""
@callback
def async_add_lock(lock):
"""Add Z-Wave Lock."""
async_add_entities([lock])

async_dispatcher_connect(hass, 'zwave_new_lock', async_add_lock)

network = hass.data[zwave.const.DATA_NETWORK]

Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/zwave/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
DEFAULT_CONF_REFRESH_DELAY = 5

SUPPORTED_PLATFORMS = ['binary_sensor', 'climate', 'cover', 'fan',
'light', 'sensor', 'switch']
'lock', 'light', 'sensor', 'switch']

RENAME_NODE_SCHEMA = vol.Schema({
vol.Required(const.ATTR_NODE_ID): vol.Coerce(int),
Expand Down
17 changes: 15 additions & 2 deletions tests/components/lock/test_zwave.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from unittest.mock import patch, MagicMock

from homeassistant import config_entries
from homeassistant.components.lock import zwave
from homeassistant.components.zwave import const

Expand Down Expand Up @@ -167,6 +168,16 @@ def test_lock_alarm_level(mock_openzwave):
assert device.device_state_attributes[zwave.ATTR_LOCK_STATUS] == \
'Tamper Alarm: Too many keypresses'

@asyncio.coroutine
def setup_ozw(hass, mock_openzwave):
"""Load the Z-Wave loc platform with the provided bridge."""
hass.config.components.add(zwave.DOMAIN)
config_entry = config_entries.ConfigEntry(1, zwave.DOMAIN, 'Mock Title', {
'usb_path': 'mock-path',
'network_key': 'mock-key'
}, 'test', config_entries.CONN_CLASS_LOCAL_PUSH)
yield from hass.config_entries.async_forward_entry_setup(config_entry, 'lock')
yield from hass.async_block_till_done()

@asyncio.coroutine
def test_lock_set_usercode_service(hass, mock_openzwave):
Expand All @@ -175,8 +186,10 @@ def test_lock_set_usercode_service(hass, mock_openzwave):
node = MockNode(node_id=12)
value0 = MockValue(data=' ', node=node, index=0)
value1 = MockValue(data=' ', node=node, index=1)
yield from zwave.async_setup_platform(
hass, {}, MagicMock())
#yield from zwave.async_setup_platform(
# hass, {}, MagicMock())
yield from setup_ozw(hass, mock_openzwave)
yield from hass.async_block_till_done()

node.get_values.return_value = {
value0.value_id: value0,
Expand Down

0 comments on commit a902387

Please sign in to comment.