Beispiel #1
0
def next_rising(hass):
    """ Returns the datetime object representing the next sun rising. """
    state = hass.states.get(ENTITY_ID)

    try:
        return util.str_to_datetime(state.attributes[STATE_ATTR_NEXT_RISING])
    except (AttributeError, KeyError):
        # AttributeError if state is None
        # KeyError if STATE_ATTR_NEXT_RISING does not exist
        return None
Beispiel #2
0
def next_rising(hass):
    """ Returns the datetime object representing the next sun rising. """
    state = hass.states.get(ENTITY_ID)

    try:
        return util.str_to_datetime(state.attributes[STATE_ATTR_NEXT_RISING])
    except (AttributeError, KeyError):
        # AttributeError if state is None
        # KeyError if STATE_ATTR_NEXT_RISING does not exist
        return None
Beispiel #3
0
    def from_dict(cls, json_dict):
        """ Static method to create a state from a dict.
        Ensures: state == State.from_json_dict(state.to_json_dict()) """

        if not (json_dict and 'entity_id' in json_dict
                and 'state' in json_dict):
            return None

        last_changed = json_dict.get('last_changed')

        if last_changed:
            last_changed = util.str_to_datetime(last_changed)

        last_updated = json_dict.get('last_updated')

        if last_updated:
            last_updated = util.str_to_datetime(last_updated)

        return cls(json_dict['entity_id'], json_dict['state'],
                   json_dict.get('attributes'), last_changed, last_updated)
Beispiel #4
0
def next_setting(hass, entity_id=None):
    """ Returns the datetime object representing the next sun setting. """
    entity_id = entity_id or ENTITY_ID

    state = hass.states.get(ENTITY_ID)

    try:
        return util.str_to_datetime(state.attributes[STATE_ATTR_NEXT_SETTING])
    except (AttributeError, KeyError):
        # AttributeError if state is None
        # KeyError if STATE_ATTR_NEXT_SETTING does not exist
        return None
Beispiel #5
0
    def from_dict(cls, json_dict):
        """ Static method to create a state from a dict.
        Ensures: state == State.from_json_dict(state.to_json_dict()) """

        if not (json_dict and
                'entity_id' in json_dict and
                'state' in json_dict):
            return None

        last_changed = json_dict.get('last_changed')

        if last_changed:
            last_changed = util.str_to_datetime(last_changed)

        last_updated = json_dict.get('last_updated')

        if last_updated:
            last_updated = util.str_to_datetime(last_updated)

        return cls(json_dict['entity_id'], json_dict['state'],
                   json_dict.get('attributes'), last_changed, last_updated)
Beispiel #6
0
def next_setting(hass, entity_id=None):
    """ Returns the datetime object representing the next sun setting. """
    entity_id = entity_id or ENTITY_ID

    state = hass.states.get(ENTITY_ID)

    try:
        return str_to_datetime(state.attributes[STATE_ATTR_NEXT_SETTING])
    except (AttributeError, KeyError):
        # AttributeError if state is None
        # KeyError if STATE_ATTR_NEXT_SETTING does not exist
        return None
 def test_str_to_datetime(self):
     """ Test str_to_datetime. """
     self.assertEqual(datetime(1986, 7, 9, 12, 0, 0),
                      util.str_to_datetime("12:00:00 09-07-1986"))
     self.assertIsNone(util.str_to_datetime("not a datetime string"))
Beispiel #8
0
 def test_str_to_datetime(self):
     """ Test str_to_datetime. """
     self.assertEqual(datetime(1986, 7, 9, 12, 0, 0),
                      util.str_to_datetime("12:00:00 09-07-1986"))
     self.assertIsNone(util.str_to_datetime("not a datetime string"))