Ejemplo 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}

        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
Ejemplo n.º 2
0
def _test_supports_brightness(state: State) -> None:
    """Test if state supports brightness."""
    supported_color_modes = state.attributes.get(ATTR_SUPPORTED_COLOR_MODES)
    if not brightness_supported(supported_color_modes):
        raise intent.IntentHandleError(
            f"Entity {state.name} does not support changing brightness")