import datetime
import unittest
from unittest.mock import MagicMock, patch
from spx import Smartplug


class SmartplugTestCase(unittest.TestCase):
    @patch('spx.Smartplug._get_timestamp', return_value=datetime.datetime(2015,9,17,14,28,1))
    @patch('requests.post', return_value=MagicMock())
    def test_get_usage(self, p, d):
        p.return_value.status_code = 200
        p.return_value.content = '<?xml version="1.0" encoding="UTF8"?><SMARTPLUG id="edimax">\n<CMD id="get">\n    <NOW_POWER><Device.System.Power.LastToggleTime>20150912160852</Device.System.Power.LastToggleTime><Device.System.Power.NowCurrent>0.0450</Device.System.Power.NowCurrent><Device.System.Power.NowPower>5.29</Device.System.Power.NowPower><Device.System.Power.NowEnergy.Day>0.080</Device.System.Power.NowEnergy.Day><Device.System.Power.NowEnergy.Week>0.080</Device.System.Power.NowEnergy.Week><Device.System.Power.NowEnergy.Month>1.214</Device.System.Power.NowEnergy.Month>\n    </NOW_POWER>\n</CMD>\n</SMARTPLUG>'
        plug = Smartplug('240.0.0.1')

        self.assertDictEqual(
            plug.get_usage(),
            {
                't': datetime.datetime(2015, 9, 17, 14, 28, 1),
                'current': 0.045,
                'day': 0.080,
                'last_toggle_time': datetime.datetime(2015, 9, 12, 16, 8, 52),
                'month': 1.214,
                'power': 5.29,
                'week': 0.080,
            }
        )
        p.assert_called_once()
        self.assertEquals(p.call_args[0][0], 'http://admin:1234@240.0.0.1:10000/smartplug.cgi')
        self.assertEquals(p.call_args[1]['data'], '<?xml version="1.0" encoding="UTF8"?><SMARTPLUG id="edimax"><CMD id="get"><NOW_POWER></NOW_POWER></CMD></SMARTPLUG>')

    @patch('requests.post', return_value=MagicMock())
    def test_set_state(self, p):
        p.return_value.status_code = 200
        p.return_value.content = '<?xml version="1.0" encoding="UTF8"?><SMARTPLUG id="edimax"></SMARTPLUG>'
        self.fail('Invalid Response')
        plug = Smartplug('240.0.0.1')

        self.assertTrue(plug.set_state('on'))

    @patch('requests.post', return_value=MagicMock())
    def test_get_state(self, p):
        p.return_value.status_code = 200
        p.return_value.content = '<?xml version="1.0" encoding="UTF8"?><SMARTPLUG id="edimax"></SMARTPLUG>'
        self.fail('Invalid Response')

@@ -68,16 +68,17 @@
 , patch%0A
+%0A
 from spx
@@ -183,16 +183,27 @@
 estamp',
+%0A          
  return_
@@ -235,19 +235,24 @@
 015,
+ 
 9,
+ 
 17,
+ 
 14,
+ 
 28,
+ 
 1))%0A
@@ -1474,18 +1474,22 @@
 led_once
+_with
 (
-)
 %0A       
@@ -1493,44 +1493,11 @@
     
-self.assertEquals(p.call_args%5B0%5D%5B0%5D,
+   
  'ht
@@ -1542,17 +1542,17 @@
 lug.cgi'
-)
+,
 %0A       
@@ -1556,51 +1556,20 @@
     
-self.assertEquals(p.call_args%5B1%5D%5B'data'%5D, '
+    data='%5Cn
 %3C?xm
@@ -1593,32 +1593,34 @@
 ncoding=%22UTF8%22?%3E
+%5Cn
 %3CSMARTPLUG id=%22e
@@ -1626,16 +1626,18 @@
 edimax%22%3E
+%5Cn
 %3CCMD id=
@@ -1642,16 +1642,22 @@
 d=%22get%22%3E
+%5Cn    
 %3CNOW_POW
@@ -1659,16 +1659,22 @@
 W_POWER%3E
+%5Cn    
 %3C/NOW_PO
@@ -1677,22 +1677,26 @@
 W_POWER%3E
+%5Cn
 %3C/CMD%3E
+%5Cn
 %3C/SMARTP
@@ -1700,17 +1700,26 @@
 RTPLUG%3E'
-)
+        )%0A
 %0A%0A    @p
@@ -1938,59 +1938,49 @@
 ax%22%3E
-%3C/SMARTPLUG%3E'%0A        self.fail('Invalid Response')
+%5Cn%3CCMD id=%22setup%22%3EOK%3C/CMD%3E%5Cn%3C/SMARTPLUG%3E'
 %0A   
@@ -2288,56 +2288,197 @@
 ax%22%3E
-%3C/SMARTPLUG%3E'%0A        self.fail('Invalid Response
+%5Cn%3CCMD id=%22get%22%3E%5Cn    %3CDevice.System.Power.State%3EOFF%3C/Device.System.Power.State%3E%5Cn%3C/CMD%3E%5Cn%3C/SMARTPLUG%3E'%0A        plug = Smartplug('240.0.0.1')%0A        self.assertEquals(plug.get_state(), 'OFF
 ')%0A

Fix tests