class BaseDomoServerTestCase(TestCase): def setUp(self): self.loop = asyncio.get_event_loop() self.server = BaseDomoServer(loop=self.loop) @mock.patch('domo.server.base.BaseDomoServer.handle_ws_client') @mock.patch('aiohttp.web.Application') def test_constructor(self, web, handle): # force patching self.setUp() web.router = mock.Mock() self.assertEqual(self.server.loop, self.loop) self.assertEqual(len(self.server.clients), 0) self.server.web.router.add_route.assert_called_once_with( 'GET', '/ws/domo', handle) def test_constructor_no_loop(self): self.assertRaises(NoEventLoopException, BaseDomoServer, None) def test_init(self): with mock.patch.object(self.server, 'log') as server_log: self.loop.run_until_complete(self.server.init()) server_log.info.assert_called_once_with( "Server started at http://127.0.0.1:8080/index.html") def test_register_client(self): client = "foo" self.server.register_client(client) self.assertIn(client, self.server.clients) self.server.unregister_client(client) self.assertNotIn(client, self.server.clients) @skip("NOT WORKING need rework") @mock.patch('aiohttp.web.WebSocketResponse') def test_handle_client(self, ws): req = mock.Mock() self.loop.run_until_complete(self.server.handle_ws_client(request=req)) ws.start.assert_called_once_with(req) self.assertEqual(len(self.server.clients), 1) def test_handle_request(self): app = mock.Mock() app.handle_request.return_value = [] client = mock.Mock() msg = mock.Mock() self.server.add_app_handler(app) self.loop.run_until_complete(self.server.handle_request(client, msg)) app.handle_request.assert_called_once_with(client, msg) def test_broadcast(self): client = mock.Mock() client.send_message.return_value = [] self.server.clients.append(client) self.loop.run_until_complete(self.server.broadcast({})) client.send_message.assert_called_once_with(msg={})
def setUp(self): self.loop = asyncio.get_event_loop() self.server = BaseDomoServer(loop=self.loop)
class RfxcomBaseTestCase(TestCase): def setUp(self): self.loop = asyncio.get_event_loop() self.server = BaseDomoServer(loop=self.loop) def test_rfxcom_constructor(self): # Add an RFXCOM App to server rfx = RfxcomApplication(server=self.server, dev="foo") self.assertIn(rfx, self.server.apps) def test_rfxcom_constructor_error(self): self.assertRaises(NoDeviceNameProvidedException, RfxcomApplication, self.server) def test_rfxcom_temp_creator(self): rfx = RfxcomApplication(server=self.server, dev="foo") # Add a handler for temperature events temp = RfxcomTemperatureHandler(rfx) rfx.add_rfxcom_handler(temp) self.assertIn(temp, rfx.handlers) rfx.remove_rfxcom_handler(temp) self.assertNotIn(temp, rfx.handlers) def test_rfxcom_client_request(self): rfx = RfxcomApplication(server=self.server, dev="foo") temp = mock.Mock() temp.process_client.return_value = [] rfx.add_rfxcom_handler(temp) msg = { 'app_name': 'rfxcom' } self.loop.run_until_complete(self.server.handle_request(None, msg)) temp.process_client.assert_called_once_with(msg, None) def test_rfxcom_device_message(self): rfx = RfxcomApplication(server=self.server, dev="foo") temp = mock.Mock() temp.process_rfxcom.return_value = [] rfx.add_rfxcom_handler(temp) class DummyMessage(object): def __init__(self, data): self.data = data msg = DummyMessage({ 'packet_length': 10, 'sub_type_name': 'THGN122/123, THGN132, THGR122/228/238/268', 'sub_type': 1, 'battery_signal_level': 137, 'sequence_number': 0, 'signal_strength': 137, 'packet_type_name': 'Temperature and humidity sensors', 'id': '0xE102', 'packet_type': 82, 'humidity_status': 1, 'humidity': 55, 'temperature': 24.7 }) self.loop.run_until_complete(rfx.handle_rfxcom_message(msg)) temp.process_rfxcom.assert_called_once_with(msg.data) def test_rfxcom_broadcast(self): pass
from domo.server.base import BaseDomoServer from domo.apps.rfxcom import RfxcomApplication, RfxcomTemperatureHandler DEBUG = True DEBUG_NO_RFXCOM = False DEV_NAME = '/dev/tty.usbserial-A1YKN6S8' ev_loop = asyncio.get_event_loop() if DEBUG: ev_loop.set_debug(True) logging.basicConfig(level=logging.DEBUG) try: # Create the server server = BaseDomoServer(loop=ev_loop) # Add an RFXCOM App if not DEBUG_NO_RFXCOM: rfx = RfxcomApplication(server=server, dev=DEV_NAME) rfx.add_rfxcom_handler(RfxcomTemperatureHandler( rfx, names={ '0xD704': 'Garden', '0x6001': "Baby's room", '0xE102': "Our room" })) # Add static file handler server.web.router.add_static('/', 'example/static') ev_loop.run_until_complete(server.init()) ev_loop.run_forever()