Beispiel #1
0
    def get_state(self, category):
        """ Returns a dict (state,last_changed, attributes) describing
            the state of the specified category. """

        try:
            req = self._call_api(METHOD_GET,
                                 hah.URL_API_STATES_CATEGORY.format(category))

            if req.status_code == 200:
                data = req.json()

                return ha.create_state(data['state'], data['attributes'],
                                       ha.str_to_datetime(
                                           data['last_changed']))

            elif req.status_code == 422:
                # Category does not exist
                return None

            else:
                raise ha.HomeAssistantException(
                    "Got unexpected result (3): {}.".format(req.text))

        except requests.exceptions.ConnectionError:
            self.logger.exception("StateMachine:Error connecting to server")
            raise ha.HomeAssistantException("Error connecting to server")

        except ValueError:  # If req.json() can't parse the json
            self.logger.exception("StateMachine:Got unexpected result")
            raise ha.HomeAssistantException(
                "Got unexpected result: {}".format(req.text))

        except KeyError:  # If not all expected keys are in the returned JSON
            self.logger.exception("StateMachine:Got unexpected result (2)")
            raise ha.HomeAssistantException(
                "Got unexpected result (2): {}".format(req.text))
Beispiel #2
0
    def _next_sun_setting(self):
        """ Returns the datetime object representing the next sun setting. """
        state = self.statemachine.get_state(STATE_CATEGORY_SUN)

        return ha.str_to_datetime(
            state['attributes'][STATE_ATTRIBUTE_NEXT_SUN_SETTING])
Beispiel #3
0
def next_rising(statemachine):
    """ Returns the datetime object representing the next sun setting. """
    state = statemachine.get_state(STATE_CATEGORY)

    return None if not state else ha.str_to_datetime(
        state['attributes'][STATE_ATTR_NEXT_RISING])