Exemplo n.º 1
0
    async def async_handle(self,
                           intent_obj: intent.Intent) -> intent.IntentResponse:
        """Handle the hass intent."""
        hass = intent_obj.hass
        slots = self.async_validate_slots(intent_obj.slots)
        state = intent.async_match_state(hass, slots["name"]["value"],
                                         hass.states.async_all(DOMAIN))

        service_data = {ATTR_ENTITY_ID: state.entity_id}

        humidity = slots["humidity"]["value"]

        if state.state == STATE_OFF:
            await hass.services.async_call(DOMAIN,
                                           SERVICE_TURN_ON,
                                           service_data,
                                           context=intent_obj.context)
            speech = f"Turned {state.name} on and set humidity to {humidity}%"
        else:
            speech = f"The {state.name} is set to {humidity}%"

        service_data[ATTR_HUMIDITY] = humidity
        await hass.services.async_call(
            DOMAIN,
            SERVICE_SET_HUMIDITY,
            service_data,
            context=intent_obj.context,
            blocking=True,
        )

        response = intent_obj.create_response()

        response.async_set_speech(speech)
        return response
Exemplo n.º 2
0
    async def async_handle(self,
                           intent_obj: intent.Intent) -> intent.IntentResponse:
        """Handle the hass intent."""
        hass = intent_obj.hass
        slots = self.async_validate_slots(intent_obj.slots)
        state = hass.helpers.intent.async_match_state(
            slots["name"]["value"],
            [
                state
                for state in hass.states.async_all() if state.domain == DOMAIN
            ],
        )

        service_data = {ATTR_ENTITY_ID: state.entity_id}
        speech_parts = []

        if "color" in slots:
            intent.async_test_feature(state, SUPPORT_COLOR, "changing colors")
            service_data[ATTR_RGB_COLOR] = slots["color"]["value"]
            # Use original passed in value of the color because we don't have
            # human readable names for that internally.
            speech_parts.append("the color {}".format(
                intent_obj.slots["color"]["value"]))

        if "brightness" in slots:
            intent.async_test_feature(state, SUPPORT_BRIGHTNESS,
                                      "changing brightness")
            service_data[ATTR_BRIGHTNESS_PCT] = slots["brightness"]["value"]
            speech_parts.append("{}% brightness".format(
                slots["brightness"]["value"]))

        await hass.services.async_call(DOMAIN,
                                       SERVICE_TURN_ON,
                                       service_data,
                                       context=intent_obj.context)

        response = intent_obj.create_response()

        if not speech_parts:  # No attributes changed
            speech = f"Turned on {state.name}"
        else:
            parts = [f"Changed {state.name} to"]
            for index, part in enumerate(speech_parts):
                if index == 0:
                    parts.append(f" {part}")
                elif index != len(speech_parts) - 1:
                    parts.append(f", {part}")
                else:
                    parts.append(f" and {part}")
            speech = "".join(parts)

        response.async_set_speech(speech)
        return response
Exemplo n.º 3
0
    async def async_handle(self, intent: Intent) -> IntentResponse:
        self.request_id = intent.context.id
        self.response_text = None
        self.response_waiter.clear()

        await intent.hass.services.async_call('media_player', 'play_media', {
            'entity_id': self.intent_type,
            'media_content_id': intent.text_input,
            'media_content_type': 'question:' + self.request_id
        })

        await asyncio.wait_for(self.response_waiter.wait(), 2.0)

        response = intent.create_response()
        if self.response_text:
            response.async_set_speech(self.response_text)
        return response
Exemplo n.º 4
0
    async def async_handle(self,
                           intent_obj: intent.Intent) -> intent.IntentResponse:
        """Handle the hass intent."""
        hass = intent_obj.hass
        slots = self.async_validate_slots(intent_obj.slots)
        state = intent.async_match_state(
            hass,
            slots["name"]["value"],
            hass.states.async_all(DOMAIN),
        )

        service_data = {ATTR_ENTITY_ID: state.entity_id}

        intent.async_test_feature(state, HumidifierEntityFeature.MODES,
                                  "modes")
        mode = slots["mode"]["value"]

        if mode not in state.attributes.get(ATTR_AVAILABLE_MODES, []):
            raise intent.IntentHandleError(
                f"Entity {state.name} does not support {mode} mode")

        if state.state == STATE_OFF:
            await hass.services.async_call(
                DOMAIN,
                SERVICE_TURN_ON,
                service_data,
                context=intent_obj.context,
                blocking=True,
            )
            speech = f"Turned {state.name} on and set {mode} mode"
        else:
            speech = f"The mode for {state.name} is set to {mode}"

        service_data[ATTR_MODE] = mode
        await hass.services.async_call(
            DOMAIN,
            SERVICE_SET_MODE,
            service_data,
            context=intent_obj.context,
            blocking=True,
        )

        response = intent_obj.create_response()

        response.async_set_speech(speech)
        return response
Exemplo n.º 5
0
    async def async_handle(self,
                           intent_obj: intent.Intent) -> intent.IntentResponse:
        """Handle the hass intent."""
        hass = intent_obj.hass
        slots = self.async_validate_slots(intent_obj.slots)
        state = intent.async_match_state(hass, slots["name"]["value"],
                                         hass.states.async_all(DOMAIN))

        service_data = {ATTR_ENTITY_ID: state.entity_id}
        speech_parts = []

        if "color" in slots:
            _test_supports_color(state)
            service_data[ATTR_RGB_COLOR] = slots["color"]["value"]
            # Use original passed in value of the color because we don't have
            # human readable names for that internally.
            speech_parts.append(
                f"the color {intent_obj.slots['color']['value']}")

        if "brightness" in slots:
            _test_supports_brightness(state)
            service_data[ATTR_BRIGHTNESS_PCT] = slots["brightness"]["value"]
            speech_parts.append(f"{slots['brightness']['value']}% brightness")

        await hass.services.async_call(DOMAIN,
                                       SERVICE_TURN_ON,
                                       service_data,
                                       context=intent_obj.context)

        response = intent_obj.create_response()

        if not speech_parts:  # No attributes changed
            speech = f"Turned on {state.name}"
        else:
            parts = [f"Changed {state.name} to"]
            for index, part in enumerate(speech_parts):
                if index == 0:
                    parts.append(f" {part}")
                elif index != len(speech_parts) - 1:
                    parts.append(f", {part}")
                else:
                    parts.append(f" and {part}")
            speech = "".join(parts)

        response.async_set_speech(speech)
        return response