def test_period_parsing(self): """Test the conversion from templates to period.""" today = Template('{{ now().replace(hour=0).replace(minute=0)' '.replace(second=0) }}', self.hass) duration = timedelta(hours=2, minutes=1) sensor1 = HistoryStatsSensor( self.hass, 'test', 'on', today, None, duration, 'time', 'test') sensor2 = HistoryStatsSensor( self.hass, 'test', 'on', None, today, duration, 'time', 'test') sensor1.update_period() sensor1_start, sensor1_end = sensor1._period sensor2.update_period() sensor2_start, sensor2_end = sensor2._period # Start = 00:00:00 self.assertEqual(sensor1_start.hour, 0) self.assertEqual(sensor1_start.minute, 0) self.assertEqual(sensor1_start.second, 0) # End = 02:01:00 self.assertEqual(sensor1_end.hour, 2) self.assertEqual(sensor1_end.minute, 1) self.assertEqual(sensor1_end.second, 0) # Start = 21:59:00 self.assertEqual(sensor2_start.hour, 21) self.assertEqual(sensor2_start.minute, 59) self.assertEqual(sensor2_start.second, 0) # End = 00:00:00 self.assertEqual(sensor2_end.hour, 0) self.assertEqual(sensor2_end.minute, 0) self.assertEqual(sensor2_end.second, 0)
def test_bad_template(self): """Test Exception when the template cannot be parsed.""" bad = Template('{{ x - 12 }}', self.hass) # x is undefined duration = '01:00' sensor1 = HistoryStatsSensor(self.hass, 'test', 'on', bad, None, duration, 'Test') sensor2 = HistoryStatsSensor(self.hass, 'test', 'on', None, bad, duration, 'Test') before_update1 = sensor1._period before_update2 = sensor2._period sensor1.update_period() sensor2.update_period() self.assertEqual(before_update1, sensor1._period) self.assertEqual(before_update2, sensor2._period)
def test_wrong_date(self): """Test when start or end value is not a timestamp or a date.""" good = Template('{{ now() }}', self.hass) bad = Template('{{ TEST }}', self.hass) sensor1 = HistoryStatsSensor(self.hass, 'test', 'on', good, bad, None, 'Test') sensor2 = HistoryStatsSensor(self.hass, 'test', 'on', bad, good, None, 'Test') before_update1 = sensor1._period before_update2 = sensor2._period sensor1.update_period() sensor2.update_period() self.assertEqual(before_update1, sensor1._period) self.assertEqual(before_update2, sensor2._period)
def test_bad_template(self): """Test Exception when the template cannot be parsed.""" bad = Template('{{ x - 12 }}', self.hass) # x is undefined duration = '01:00' sensor1 = HistoryStatsSensor( self.hass, 'test', 'on', bad, None, duration, 'time', 'Test') sensor2 = HistoryStatsSensor( self.hass, 'test', 'on', None, bad, duration, 'time', 'Test') before_update1 = sensor1._period before_update2 = sensor2._period sensor1.update_period() sensor2.update_period() self.assertEqual(before_update1, sensor1._period) self.assertEqual(before_update2, sensor2._period)
def test_wrong_date(self): """Test when start or end value is not a timestamp or a date.""" good = Template('{{ now() }}', self.hass) bad = Template('{{ TEST }}', self.hass) sensor1 = HistoryStatsSensor( self.hass, 'test', 'on', good, bad, None, 'time', 'Test') sensor2 = HistoryStatsSensor( self.hass, 'test', 'on', bad, good, None, 'time', 'Test') before_update1 = sensor1._period before_update2 = sensor2._period sensor1.update_period() sensor2.update_period() self.assertEqual(before_update1, sensor1._period) self.assertEqual(before_update2, sensor2._period)
def test_period_parsing(self): """Test the conversion from templates to period.""" today = Template('{{ now().replace(hour=0).replace(minute=0)' '.replace(second=0) }}', self.hass) duration = timedelta(hours=2, minutes=1) sensor1 = HistoryStatsSensor( self.hass, 'test', 'on', today, None, duration, 'test') sensor2 = HistoryStatsSensor( self.hass, 'test', 'on', None, today, duration, 'test') sensor1.update_period() sensor2.update_period() self.assertEqual( sensor1.device_state_attributes['from'][-8:], '00:00:00') self.assertEqual( sensor1.device_state_attributes['to'][-8:], '02:01:00') self.assertEqual( sensor2.device_state_attributes['from'][-8:], '21:59:00') self.assertEqual( sensor2.device_state_attributes['to'][-8:], '00:00:00')
def test_period_parsing(self): """Test the conversion from templates to period.""" now = datetime(2019, 1, 1, 23, 30, 0, tzinfo=pytz.utc) with patch.dict(template.ENV.globals, {'now': lambda: now}): today = Template( '{{ now().replace(hour=0).replace(minute=0)' '.replace(second=0) }}', self.hass) duration = timedelta(hours=2, minutes=1) sensor1 = HistoryStatsSensor(self.hass, 'test', 'on', today, None, duration, 'time', 'test') sensor2 = HistoryStatsSensor(self.hass, 'test', 'on', None, today, duration, 'time', 'test') sensor1.update_period() sensor1_start, sensor1_end = sensor1._period sensor2.update_period() sensor2_start, sensor2_end = sensor2._period # Start = 00:00:00 assert sensor1_start.hour == 0 assert sensor1_start.minute == 0 assert sensor1_start.second == 0 # End = 02:01:00 assert sensor1_end.hour == 2 assert sensor1_end.minute == 1 assert sensor1_end.second == 0 # Start = 21:59:00 assert sensor2_start.hour == 21 assert sensor2_start.minute == 59 assert sensor2_start.second == 0 # End = 00:00:00 assert sensor2_end.hour == 0 assert sensor2_end.minute == 0 assert sensor2_end.second == 0
def test_period_parsing(self): """Test the conversion from templates to period.""" now = datetime(2019, 1, 1, 23, 30, 0, tzinfo=pytz.utc) with patch.dict(template.ENV.globals, {'now': lambda: now}): print(dt_util.now()) today = Template('{{ now().replace(hour=0).replace(minute=0)' '.replace(second=0) }}', self.hass) duration = timedelta(hours=2, minutes=1) sensor1 = HistoryStatsSensor( self.hass, 'test', 'on', today, None, duration, 'time', 'test') sensor2 = HistoryStatsSensor( self.hass, 'test', 'on', None, today, duration, 'time', 'test') sensor1.update_period() sensor1_start, sensor1_end = sensor1._period sensor2.update_period() sensor2_start, sensor2_end = sensor2._period # Start = 00:00:00 assert sensor1_start.hour == 0 assert sensor1_start.minute == 0 assert sensor1_start.second == 0 # End = 02:01:00 assert sensor1_end.hour == 2 assert sensor1_end.minute == 1 assert sensor1_end.second == 0 # Start = 21:59:00 assert sensor2_start.hour == 21 assert sensor2_start.minute == 59 assert sensor2_start.second == 0 # End = 00:00:00 assert sensor2_end.hour == 0 assert sensor2_end.minute == 0 assert sensor2_end.second == 0
def test_period_parsing(self): """Test the conversion from templates to period.""" today = Template( '{{ now().replace(hour=0).replace(minute=0)' '.replace(second=0) }}', self.hass) duration = timedelta(hours=2, minutes=1) sensor1 = HistoryStatsSensor(self.hass, 'test', 'on', today, None, duration, 'test') sensor2 = HistoryStatsSensor(self.hass, 'test', 'on', None, today, duration, 'test') sensor1.update_period() sensor2.update_period() self.assertEqual(sensor1.device_state_attributes['from'][-8:], '00:00:00') self.assertEqual(sensor1.device_state_attributes['to'][-8:], '02:01:00') self.assertEqual(sensor2.device_state_attributes['from'][-8:], '21:59:00') self.assertEqual(sensor2.device_state_attributes['to'][-8:], '00:00:00')
def test_period_parsing(self): """Test the conversion from templates to period.""" today = Template( '{{ now().replace(hour=0).replace(minute=0)' '.replace(second=0) }}', self.hass) duration = timedelta(hours=2, minutes=1) sensor1 = HistoryStatsSensor(self.hass, 'test', 'on', today, None, duration, 'time', 'test') sensor2 = HistoryStatsSensor(self.hass, 'test', 'on', None, today, duration, 'time', 'test') sensor1.update_period() sensor1_start, sensor1_end = sensor1._period sensor2.update_period() sensor2_start, sensor2_end = sensor2._period # Start = 00:00:00 self.assertEqual(sensor1_start.hour, 0) self.assertEqual(sensor1_start.minute, 0) self.assertEqual(sensor1_start.second, 0) # End = 02:01:00 self.assertEqual(sensor1_end.hour, 2) self.assertEqual(sensor1_end.minute, 1) self.assertEqual(sensor1_end.second, 0) # Start = 21:59:00 self.assertEqual(sensor2_start.hour, 21) self.assertEqual(sensor2_start.minute, 59) self.assertEqual(sensor2_start.second, 0) # End = 00:00:00 self.assertEqual(sensor2_end.hour, 0) self.assertEqual(sensor2_end.minute, 0) self.assertEqual(sensor2_end.second, 0)