Skip to content
This repository has been archived by the owner on Jul 23, 2021. It is now read-only.

Commit

Permalink
Updates testing
Browse files Browse the repository at this point in the history
  • Loading branch information
robmarkcole committed Oct 13, 2017
1 parent 2248c22 commit acf91e4
Showing 1 changed file with 149 additions and 90 deletions.
239 changes: 149 additions & 90 deletions Testing and usage 13-10-2017.ipynb
Expand Up @@ -38,6 +38,7 @@
"import json\n",
"import pprint\n",
"import requests\n",
"import requests_mock\n",
"from datetime import datetime, timedelta\n",
"import hue_sensors as hs\n",
"\n",
Expand Down Expand Up @@ -87,7 +88,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -183,7 +184,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -202,7 +203,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand All @@ -220,24 +221,49 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Remote bedroom\n",
"Hall motion Sensor\n",
"Bedroom motion sensor\n",
"Robins iPhone\n",
"Living room motion sensor\n",
"Living room remote\n",
"Hall remote\n"
]
}
],
"source": [
"for sensor in sensors:\n",
" print(sensor.name)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Living room remote\n",
"1_click\n",
"{'last updated': ['2017-10-13', '05:57:49'], 'battery': 100}\n"
"{'last updated': ['2017-10-13', '05:34:33'], 'battery': 100}\n"
]
}
],
"source": [
"for sensor in sensors:\n",
" sensor.update()\n",
"\n",
"s = sensors[0]\n",
"\n",
"s = sensors[5]\n",
"print(s.name)\n",
"print(s.state)\n",
"print(s.device_state_attributes)"
Expand All @@ -253,154 +279,187 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"response = requests.get(url)\n",
"data = hs.parse_hue_api_response(response.json())"
"# Load the saved raw API data\n",
"with open('tests/hue_sensors.json', 'r') as fp:\n",
" mock_data = json.load(fp)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Geofence': {'model': 'GEO', 'name': 'Robins iPhone', 'state': 'on'},\n",
" 'RWL_1e-02': {'battery': 100,\n",
" 'last_updated': ['2017-10-13', '05:34:33'],\n",
" 'model': 'RWL',\n",
" 'name': 'Living room remote',\n",
" 'state': '1_click'},\n",
" 'RWL_9c-02': {'battery': 100,\n",
" 'last_updated': ['2017-10-13', '05:36:46'],\n",
" 'model': 'RWL',\n",
" 'name': 'Hall remote',\n",
" 'state': '1_click'},\n",
" 'RWL_dc-02': {'battery': 100,\n",
" 'last_updated': ['2017-10-13', '06:01:04'],\n",
" 'model': 'RWL',\n",
" 'name': 'Remote bedroom',\n",
" 'state': '2_hold'},\n",
" 'SML_28-02': {'battery': 100,\n",
" 'dark': True,\n",
" 'daylight': False,\n",
" 'last_updated': ['2017-10-13', '06:46:19'],\n",
" 'light_level': 0,\n",
" 'lux': 1.0,\n",
" 'model': 'SML',\n",
" 'name': 'Hall motion Sensor',\n",
" 'state': 'off',\n",
" 'temperature': 19.42},\n",
" 'SML_9c-02': {'battery': 100,\n",
" 'dark': True,\n",
" 'daylight': False,\n",
" 'last_updated': ['2017-10-13', '07:04:16'],\n",
" 'light_level': 7325,\n",
" 'lux': 5.4,\n",
" 'model': 'SML',\n",
" 'name': 'Living room motion sensor',\n",
" 'state': 'off',\n",
" 'temperature': 23.08},\n",
" 'SML_ce-02': {'battery': 100,\n",
" 'dark': True,\n",
" 'daylight': False,\n",
" 'last_updated': ['2017-10-13', '06:04:20'],\n",
" 'light_level': 2464,\n",
" 'lux': 1.76,\n",
" 'model': 'SML',\n",
" 'name': 'Bedroom motion sensor',\n",
" 'state': 'off',\n",
" 'temperature': 20.17}}\n"
]
}
],
"source": [
"print_json(hs.parse_hue_api_response(mock_data))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mock object to hold mock data"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"# Don't delete, used to capture data \n",
"#with open('hue_sensors.json', 'w') as fp:\n",
"# json.dump(data, fp)"
"class MockHueSensorData():\n",
" \"\"\"Class to hold mock_data.\"\"\"\n",
"\n",
" def __init__(self):\n",
" \"\"\"Initialize the mock object.\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"# Load the saved data\n",
"with open('tests/hue_sensors.json', 'r') as fp:\n",
" mock_data = json.load(fp)"
"mock_data_object = MockHueSensorData()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"mock_data_object.data = hs.parse_hue_api_response(mock_data)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Geofence': {'model': 'GEO', 'name': 'Robins iPhone', 'state': 'on'},\n",
" 'RWL_1e-02': {'battery': 100,\n",
" 'last_updated': ['2017-10-13', '05:34:33'],\n",
" 'model': 'RWL',\n",
" 'name': 'Living room remote',\n",
" 'state': '1_click'},\n",
" 'RWL_9c-02': {'battery': 100,\n",
" 'last_updated': ['2017-10-13', '05:36:46'],\n",
" 'model': 'RWL',\n",
" 'name': 'Hall remote',\n",
" 'state': '1_click'},\n",
" 'RWL_dc-02': {'battery': 100,\n",
" 'last_updated': ['2017-10-13', '06:01:04'],\n",
" 'model': 'RWL',\n",
" 'name': 'Remote bedroom',\n",
" 'state': '2_hold'},\n",
" 'SML_28-02': {'battery': 100,\n",
" 'dark': True,\n",
" 'daylight': False,\n",
" 'last_updated': ['2017-10-13', '05:57:40'],\n",
" 'light_level': 0,\n",
" 'lux': 1,\n",
" 'model': 'SML',\n",
" 'name': 'Hall motion Sensor',\n",
" 'state': 'off',\n",
" 'temperature': 19.42},\n",
" 'SML_9c-02': {'battery': 100,\n",
" 'dark': True,\n",
" 'daylight': False,\n",
" 'last_updated': ['2017-10-13', '06:01:07'],\n",
" 'light_level': 0,\n",
" 'lux': 1,\n",
" 'model': 'SML',\n",
" 'name': 'Living room motion sensor',\n",
" 'state': 'off',\n",
" 'temperature': 22.8},\n",
" 'SML_ce-02': {'battery': 100,\n",
" 'dark': True,\n",
" 'daylight': False,\n",
" 'last_updated': ['2017-10-13', '06:00:53'],\n",
" 'light_level': 0,\n",
" 'lux': 1,\n",
" 'model': 'SML',\n",
" 'name': 'Bedroom motion sensor',\n",
" 'state': 'on',\n",
" 'temperature': 20.17}}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"RWL_dc-02\n",
"SML_28-02\n",
"SML_ce-02\n",
"Geofence\n",
"SML_9c-02\n",
"RWL_1e-02\n",
"RWL_9c-02\n"
]
}
],
"source": [
"mock_data"
"for key in mock_data_object.data.keys():\n",
" print(key)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
"class TestLondonTubeSensor(unittest.TestCase):\n",
"class TestSensor(unittest.TestCase):\n",
" \n",
" def test_setup_platform(self):\n",
" \"\"\"Set up the Hue sensors.\"\"\"\n",
" import hue_sensors as hs \n",
" #url = hass.data[DOMAIN] + '/sensors'\n",
" hs.parse_hue_api_response = MagicMock(return_value=mock_data) # Mock the return\n",
" data = HueSensorData(url, hs.parse_hue_api_response)\n",
" data.update()\n",
" \n",
" sensors = []\n",
" for key in data.data.keys():\n",
" sensors.append(HueSensor(key, data))\n",
" for sensor in sensors:\n",
" sensor.update()\n",
" for key in mock_data_object.data.keys():\n",
" sensors.append(HueSensor(key, mock_data_object))\n",
"\n",
" hall_motion_sensor = sensors[0]\n",
" assert hall_motion_sensor.name == 'Remote bedroom'\n",
" bedroom_remote = sensors[0]\n",
" assert bedroom_remote.name == 'Remote bedroom'\n",
" assert bedroom_remote.state == '2_hold'\n",
" \n",
" hall_motion_sensor = sensors[1]\n",
" assert hall_motion_sensor.name == 'Hall motion Sensor'"
]
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"test_setup_platform (__main__.TestLondonTubeSensor)\n",
"test_setup_platform (__main__.TestSensor)\n",
"Set up the Hue sensors. ... ok\n",
"\n",
"----------------------------------------------------------------------\n",
"Ran 1 test in 0.317s\n",
"Ran 1 test in 0.001s\n",
"\n",
"OK\n"
]
},
{
"data": {
"text/plain": [
"<unittest.main.TestProgram at 0x111e20f60>"
"<unittest.main.TestProgram at 0x1080381d0>"
]
},
"execution_count": 15,
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
Expand Down

0 comments on commit acf91e4

Please sign in to comment.