2017-03-09 13:35:04 +00:00
|
|
|
"""Mock helpers for Z-Wave component."""
|
|
|
|
from unittest.mock import MagicMock
|
|
|
|
|
|
|
|
from pydispatch import dispatcher
|
|
|
|
|
|
|
|
SIGNAL_VALUE_CHANGED = 'mock_value_changed'
|
|
|
|
|
|
|
|
|
|
|
|
def value_changed(value):
|
|
|
|
"""Fire a value changed."""
|
|
|
|
dispatcher.send(
|
|
|
|
SIGNAL_VALUE_CHANGED,
|
|
|
|
value=value,
|
|
|
|
node=value.node,
|
|
|
|
network=value.node._network
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class MockNode(MagicMock):
|
|
|
|
"""Mock Z-Wave node."""
|
|
|
|
|
|
|
|
def __init__(self, *,
|
|
|
|
node_id='567',
|
|
|
|
name='Mock Node',
|
|
|
|
manufacturer_id='ABCD',
|
|
|
|
product_id='123',
|
2017-03-13 05:08:53 +00:00
|
|
|
product_type='678',
|
2017-03-09 13:35:04 +00:00
|
|
|
command_classes=None):
|
|
|
|
"""Initialize a Z-Wave mock node."""
|
|
|
|
super().__init__()
|
|
|
|
self.node_id = node_id
|
|
|
|
self.name = name
|
|
|
|
self.manufacturer_id = manufacturer_id
|
|
|
|
self.product_id = product_id
|
2017-03-13 05:08:53 +00:00
|
|
|
self.product_type = product_type
|
2017-03-09 13:35:04 +00:00
|
|
|
self._command_classes = command_classes or []
|
|
|
|
|
|
|
|
def has_command_class(self, command_class):
|
|
|
|
"""Test if mock has a command class."""
|
|
|
|
return command_class in self._command_classes
|
|
|
|
|
|
|
|
def _get_child_mock(self, **kw):
|
|
|
|
"""Create child mocks with right MagicMock class."""
|
|
|
|
return MagicMock(**kw)
|
|
|
|
|
|
|
|
|
|
|
|
class MockValue(MagicMock):
|
|
|
|
"""Mock Z-Wave value."""
|
|
|
|
|
|
|
|
_mock_value_id = 1234
|
|
|
|
|
|
|
|
def __init__(self, *,
|
|
|
|
label='Mock Value',
|
|
|
|
data=None,
|
2017-03-15 03:24:35 +00:00
|
|
|
data_items=None,
|
2017-03-09 13:35:04 +00:00
|
|
|
node=None,
|
|
|
|
instance=0,
|
|
|
|
index=0,
|
2017-03-13 05:08:53 +00:00
|
|
|
command_class=None,
|
2017-03-15 03:24:35 +00:00
|
|
|
units=None,
|
2017-03-09 13:35:04 +00:00
|
|
|
value_id=None):
|
|
|
|
"""Initialize a Z-Wave mock value."""
|
|
|
|
super().__init__()
|
|
|
|
self.label = label
|
|
|
|
self.data = data
|
2017-03-15 03:24:35 +00:00
|
|
|
self.data_items = data_items
|
2017-03-09 13:35:04 +00:00
|
|
|
self.node = node
|
|
|
|
self.instance = instance
|
|
|
|
self.index = 0
|
2017-03-13 05:08:53 +00:00
|
|
|
self.command_class = command_class
|
2017-03-15 03:24:35 +00:00
|
|
|
self.units = units
|
2017-03-09 13:35:04 +00:00
|
|
|
if value_id is None:
|
|
|
|
MockValue._mock_value_id += 1
|
|
|
|
value_id = MockValue._mock_value_id
|
|
|
|
self.value_id = value_id
|
|
|
|
|
|
|
|
def _get_child_mock(self, **kw):
|
|
|
|
"""Create child mocks with right MagicMock class."""
|
|
|
|
return MagicMock(**kw)
|
2017-03-14 23:55:33 +00:00
|
|
|
|
2017-03-20 05:22:13 +00:00
|
|
|
def refresh(self):
|
|
|
|
"""Mock refresh of node value."""
|
|
|
|
value_changed(self)
|
|
|
|
|
2017-03-14 23:55:33 +00:00
|
|
|
|
|
|
|
class MockEntityValues():
|
|
|
|
"""Mock Z-Wave entity values."""
|
|
|
|
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
"""Initialize the mock zwave values."""
|
|
|
|
self.primary = None
|
|
|
|
self.wakeup = None
|
|
|
|
self.battery = None
|
|
|
|
self.power = None
|
|
|
|
for name in kwargs:
|
|
|
|
setattr(self, name, kwargs[name])
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
"""Allow iteration over all values."""
|
|
|
|
return iter(self.__dict__.values())
|