home-assistant-core/test/mock_toggledevice_platform.py

65 lines
1.6 KiB
Python
Raw Normal View History

2014-11-25 08:20:36 +00:00
"""
test.mock.switch_platform
~~~~~~~~~~~~~~~~~~~~~~~~~
Provides a mock switch platform.
2014-11-26 05:28:43 +00:00
Call init before using it in your tests to ensure clean test data.
2014-11-25 08:20:36 +00:00
"""
import homeassistant.components as components
class MockToggleDevice(components.ToggleDevice):
2014-11-25 08:20:36 +00:00
""" Fake switch. """
def __init__(self, name, state):
self.name = name
self.state = state
2014-11-26 05:28:43 +00:00
self.calls = []
2014-11-25 08:20:36 +00:00
def get_name(self):
""" Returns the name of the device if any. """
2014-11-26 05:28:43 +00:00
self.calls.append(('get_name', {}))
2014-11-25 08:20:36 +00:00
return self.name
def turn_on(self, **kwargs):
""" Turn the device on. """
2014-11-26 05:28:43 +00:00
self.calls.append(('turn_on', kwargs))
2014-11-25 08:20:36 +00:00
self.state = components.STATE_ON
def turn_off(self, **kwargs):
""" Turn the device off. """
2014-11-26 05:28:43 +00:00
self.calls.append(('turn_off', kwargs))
2014-11-25 08:20:36 +00:00
self.state = components.STATE_OFF
def is_on(self):
""" True if device is on. """
2014-11-26 05:28:43 +00:00
self.calls.append(('is_on', {}))
2014-11-25 08:20:36 +00:00
return self.state == components.STATE_ON
2014-11-26 05:28:43 +00:00
def last_call(self, method=None):
if method is None:
return self.calls[-1]
else:
return next(call for call in reversed(self.calls)
if call[0] == method)
2014-11-25 08:20:36 +00:00
2014-11-26 05:28:43 +00:00
DEVICES = []
2014-11-25 08:20:36 +00:00
2014-11-26 05:28:43 +00:00
def init(empty=False):
""" (re-)initalizes the platform with devices. """
global DEVICES
2014-11-25 08:20:36 +00:00
2014-11-26 05:28:43 +00:00
DEVICES = [] if empty else [
MockToggleDevice('AC', components.STATE_ON),
MockToggleDevice('AC', components.STATE_OFF),
MockToggleDevice(None, components.STATE_OFF)
]
2014-11-25 08:20:36 +00:00
def get_switches(hass, config):
""" Returns mock devices. """
2014-11-26 05:28:43 +00:00
return DEVICES
get_lights = get_switches