예제 #1
0
파일: logbook.py 프로젝트: lumavp/blumate
    def get(self, request, date=None):
        """Retrieve logbook entries."""
        if date:
            start_day = dt_util.start_of_local_day(date)
        else:
            start_day = dt_util.start_of_local_day()

        end_day = start_day + timedelta(days=1)

        events = recorder.query_events(
            QUERY_EVENTS_BETWEEN,
            (dt_util.as_utc(start_day), dt_util.as_utc(end_day)))

        return self.json(humanify(events))
예제 #2
0
파일: logbook.py 프로젝트: bdfoster/blumate
    def get(self, request, date=None):
        """Retrieve logbook entries."""
        if date:
            start_day = dt_util.start_of_local_day(date)
        else:
            start_day = dt_util.start_of_local_day()

        end_day = start_day + timedelta(days=1)

        events = recorder.query_events(
            QUERY_EVENTS_BETWEEN,
            (dt_util.as_utc(start_day), dt_util.as_utc(end_day)))

        return self.json(humanify(events))
예제 #3
0
    def test_notify_file(self, mock_utcnow):
        """Test the notify file output."""
        mock_utcnow.return_value = dt_util.as_utc(dt_util.now())

        with tempfile.TemporaryDirectory() as tempdirname:
            filename = os.path.join(tempdirname, 'notify.txt')
            message = 'one, two, testing, testing'
            self.assertTrue(notify.setup(self.hass, {
                'notify': {
                    'name': 'test',
                    'platform': 'file',
                    'filename': filename,
                    'timestamp': 0
                }
            }))
            title = '{} notifications (Log started: {})\n{}\n'.format(
                ATTR_TITLE_DEFAULT,
                dt_util.utcnow().isoformat(),
                '-' * 80)

            self.hass.services.call('notify', 'test', {'message': message},
                                    blocking=True)

            result = open(filename).read()
            self.assertEqual(result, "{}{}\n".format(title, message))
예제 #4
0
    def test_notify_file(self, mock_utcnow):
        """Test the notify file output."""
        mock_utcnow.return_value = dt_util.as_utc(dt_util.now())

        with tempfile.TemporaryDirectory() as tempdirname:
            filename = os.path.join(tempdirname, 'notify.txt')
            message = 'one, two, testing, testing'
            self.assertTrue(
                notify.setup(
                    self.hass, {
                        'notify': {
                            'name': 'test',
                            'platform': 'file',
                            'filename': filename,
                            'timestamp': 0
                        }
                    }))
            title = '{} notifications (Log started: {})\n{}\n'.format(
                ATTR_TITLE_DEFAULT,
                dt_util.utcnow().isoformat(), '-' * 80)

            self.hass.services.call('notify',
                                    'test', {'message': message},
                                    blocking=True)

            result = open(filename).read()
            self.assertEqual(result, "{}{}\n".format(title, message))
예제 #5
0
파일: test_dt.py 프로젝트: bdfoster/blumate
    def test_as_utc_with_local_object(self):
        """Test the UTC time with local object."""
        dt_util.set_default_time_zone(dt_util.get_time_zone(TEST_TIME_ZONE))
        localnow = dt_util.now()
        utcnow = dt_util.as_utc(localnow)

        self.assertEqual(localnow, utcnow)
        self.assertNotEqual(localnow.tzinfo, utcnow.tzinfo)
예제 #6
0
파일: test_dt.py 프로젝트: bdfoster/blumate
    def test_now(self):
        """Test the now method."""
        dt_util.set_default_time_zone(dt_util.get_time_zone(TEST_TIME_ZONE))

        self.assertAlmostEqual(
            dt_util.as_utc(dt_util.now()).replace(tzinfo=None),
            datetime.utcnow(),
            delta=timedelta(seconds=1))
예제 #7
0
파일: history.py 프로젝트: bdfoster/blumate
    def get(self, request, date=None):
        """Return history over a period of time."""
        one_day = timedelta(days=1)

        if date:
            start_time = dt_util.as_utc(dt_util.start_of_local_day(date))
        else:
            start_time = dt_util.utcnow() - one_day

        end_time = start_time + one_day
        entity_id = request.args.get('filter_entity_id')

        return self.json(
            get_significant_states(start_time, end_time, entity_id).values())
예제 #8
0
파일: history.py 프로젝트: lumavp/blumate
    def get(self, request, date=None):
        """Return history over a period of time."""
        one_day = timedelta(days=1)

        if date:
            start_time = dt_util.as_utc(dt_util.start_of_local_day(date))
        else:
            start_time = dt_util.utcnow() - one_day

        end_time = start_time + one_day
        entity_id = request.args.get('filter_entity_id')

        return self.json(
            get_significant_states(start_time, end_time, entity_id).values())
예제 #9
0
    def test_state_changed_event_sends_message(self, mock_utcnow, mock_pub):
        """"Test the sending of a new message if event changed."""
        now = dt_util.as_utc(dt_util.now())
        e_id = 'fake.entity'
        pub_topic = 'bar'
        mock_utcnow.return_value = now

        # Add the eventstream component for publishing events
        self.assertTrue(self.add_eventstream(pub_topic=pub_topic))
        self.hass.pool.block_till_done()

        # Reset the mock because it will have already gotten calls for the
        # mqtt_eventstream state change on initialization, etc.
        mock_pub.reset_mock()

        # Set a state of an entity
        mock_state_change_event(self.hass, State(e_id, 'on'))
        self.hass.pool.block_till_done()

        # The order of the JSON is indeterminate,
        # so first just check that publish was called
        mock_pub.assert_called_with(self.hass, pub_topic, ANY)
        self.assertTrue(mock_pub.called)

        # Get the actual call to publish and make sure it was the one
        # we were looking for
        msg = mock_pub.call_args[0][2]
        event = {}
        event['event_type'] = EVENT_STATE_CHANGED
        new_state = {
            "last_updated": now.isoformat(),
            "state": "on",
            "entity_id": e_id,
            "attributes": {},
            "last_changed": now.isoformat()
        }
        event['event_data'] = {"new_state": new_state, "entity_id": e_id}

        # Verify that the message received was that expected
        self.assertEqual(json.loads(msg), event)
예제 #10
0
    def test_state_changed_event_sends_message(self, mock_utcnow, mock_pub):
        """"Test the sending of a new message if event changed."""
        now = dt_util.as_utc(dt_util.now())
        e_id = 'fake.entity'
        pub_topic = 'bar'
        mock_utcnow.return_value = now

        # Add the eventstream component for publishing events
        self.assertTrue(self.add_eventstream(pub_topic=pub_topic))
        self.hass.pool.block_till_done()

        # Reset the mock because it will have already gotten calls for the
        # mqtt_eventstream state change on initialization, etc.
        mock_pub.reset_mock()

        # Set a state of an entity
        mock_state_change_event(self.hass, State(e_id, 'on'))
        self.hass.pool.block_till_done()

        # The order of the JSON is indeterminate,
        # so first just check that publish was called
        mock_pub.assert_called_with(self.hass, pub_topic, ANY)
        self.assertTrue(mock_pub.called)

        # Get the actual call to publish and make sure it was the one
        # we were looking for
        msg = mock_pub.call_args[0][2]
        event = {}
        event['event_type'] = EVENT_STATE_CHANGED
        new_state = {
            "last_updated": now.isoformat(),
            "state": "on",
            "entity_id": e_id,
            "attributes": {},
            "last_changed": now.isoformat()
        }
        event['event_data'] = {"new_state": new_state, "entity_id": e_id}

        # Verify that the message received was that expected
        self.assertEqual(json.loads(msg), event)
예제 #11
0
def _adapt_datetime(datetimestamp):
    """Turn a datetime into an integer for in the DB."""
    return dt_util.as_utc(datetimestamp).timestamp()
예제 #12
0
파일: test_dt.py 프로젝트: bdfoster/blumate
    def test_as_utc_with_utc_object(self):
        """Test UTC time with UTC object."""
        utcnow = dt_util.utcnow()

        self.assertEqual(utcnow, dt_util.as_utc(utcnow))
예제 #13
0
파일: test_dt.py 프로젝트: bdfoster/blumate
    def test_as_utc_with_naive_object(self):
        """Test the now method."""
        utcnow = datetime.utcnow()

        self.assertEqual(utcnow,
                         dt_util.as_utc(utcnow).replace(tzinfo=None))
예제 #14
0
파일: recorder.py 프로젝트: lumavp/blumate
def _adapt_datetime(datetimestamp):
    """Turn a datetime into an integer for in the DB."""
    return dt_util.as_utc(datetimestamp).timestamp()